Skip to content

Commit

Permalink
engine/gRPC: Add update account info to grpc interface (thrasher-corp…
Browse files Browse the repository at this point in the history
…#602)

* Add update account info to grpc interface

* Fix lbank tests

* Review corrections

* Review corrections

* Fix linter
  • Loading branch information
mshogin authored Nov 30, 2020
1 parent b7d99f7 commit ba4ac4f
Show file tree
Hide file tree
Showing 13 changed files with 416 additions and 100 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ CRON = $(TRAVIS_EVENT_TYPE)
DRIVER ?= psql
RACE_FLAG := $(if $(NO_RACE_TEST),,-race)

.PHONY: get linter check test build install update_deps

all: check build

get:
GO111MODULE=on go get $(GCTPKG)

Expand Down
49 changes: 49 additions & 0 deletions cmd/gctcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,55 @@ func getAccountInfoStream(c *cli.Context) error {
}
}

var updateAccountInfoCommand = cli.Command{
Name: "updateaccountinfo",
Usage: "updates the exchange account balance info",
ArgsUsage: "<exchange>",
Action: updateAccountInfo,
Flags: []cli.Flag{
cli.StringFlag{
Name: "exchange",
Usage: "the exchange to get the account info for",
},
},
}

func updateAccountInfo(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
return cli.ShowCommandHelp(c, "updateaccountinfo")
}

var exchange string
if c.IsSet("exchange") {
exchange = c.String("exchange")
} else {
exchange = c.Args().First()
}

if !validExchange(exchange) {
return errInvalidExchange
}

conn, err := setupClient()
if err != nil {
return err
}
defer conn.Close()

client := gctrpc.NewGoCryptoTraderClient(conn)
result, err := client.UpdateAccountInfo(context.Background(),
&gctrpc.GetAccountInfoRequest{
Exchange: exchange,
},
)
if err != nil {
return err
}

jsonOutput(result)
return nil
}

var getConfigCommand = cli.Command{
Name: "getconfig",
Usage: "gets the config",
Expand Down
1 change: 1 addition & 0 deletions cmd/gctcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func main() {
getOrderbooksCommand,
getAccountInfoCommand,
getAccountInfoStreamCommand,
updateAccountInfoCommand,
getConfigCommand,
getPortfolioCommand,
getPortfolioSummaryCommand,
Expand Down
32 changes: 30 additions & 2 deletions engine/fake_exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,41 @@ func (h *FakePassingExchange) GetEnabledPairs(_ asset.Item) (currency.Pairs, err
func (h *FakePassingExchange) GetAvailablePairs(_ asset.Item) (currency.Pairs, error) {
return currency.Pairs{}, nil
}

func (h *FakePassingExchange) FetchAccountInfo() (account.Holdings, error) {
return account.Holdings{}, nil
return account.Holdings{
Exchange: h.Name,
Accounts: []account.SubAccount{
{
Currencies: []account.Balance{
{
CurrencyName: currency.BTC,
TotalValue: 10.,
Hold: 0,
},
},
},
},
}, nil
}

func (h *FakePassingExchange) UpdateAccountInfo() (account.Holdings, error) {
return account.Holdings{}, nil
return account.Holdings{
Exchange: h.Name,
Accounts: []account.SubAccount{
{
Currencies: []account.Balance{
{
CurrencyName: currency.BTC,
TotalValue: 20.,
Hold: 0,
},
},
},
},
}, nil
}

func (h *FakePassingExchange) GetAuthenticatedAPISupport(_ uint8) bool { return true }
func (h *FakePassingExchange) SetPairs(_ currency.Pairs, _ asset.Item, _ bool) error {
return nil
Expand Down
28 changes: 23 additions & 5 deletions engine/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,30 @@ func (s *RPCServer) GetAccountInfo(_ context.Context, r *gctrpc.GetAccountInfoRe
return nil, err
}

return createAccountInfoRequest(resp)
}

// UpdateAccountInfo forces an update of the account info
func (s *RPCServer) UpdateAccountInfo(ctx context.Context, r *gctrpc.GetAccountInfoRequest) (*gctrpc.GetAccountInfoResponse, error) {
exch := s.GetExchangeByName(r.Exchange)
if exch == nil {
return nil, errExchangeNotLoaded
}

resp, err := exch.UpdateAccountInfo()
if err != nil {
return nil, err
}

return createAccountInfoRequest(resp)
}

func createAccountInfoRequest(h account.Holdings) (*gctrpc.GetAccountInfoResponse, error) {
var accounts []*gctrpc.Account
for x := range resp.Accounts {
for x := range h.Accounts {
var a gctrpc.Account
a.Id = resp.Accounts[x].ID
for _, y := range resp.Accounts[x].Currencies {
a.Id = h.Accounts[x].ID
for _, y := range h.Accounts[x].Currencies {
a.Currencies = append(a.Currencies, &gctrpc.AccountCurrencyInfo{
Currency: y.CurrencyName.String(),
Hold: y.Hold,
Expand All @@ -521,7 +540,7 @@ func (s *RPCServer) GetAccountInfo(_ context.Context, r *gctrpc.GetAccountInfoRe
accounts = append(accounts, &a)
}

return &gctrpc.GetAccountInfoResponse{Exchange: r.Exchange, Accounts: accounts}, nil
return &gctrpc.GetAccountInfoResponse{Exchange: h.Exchange, Accounts: accounts}, nil
}

// GetAccountInfoStream streams an account balance for a specific exchange
Expand Down Expand Up @@ -2843,7 +2862,6 @@ func (s *RPCServer) GetHistoricTrades(r *gctrpc.GetSavedTradesRequest, stream gc
if err != nil {
return err
}

resp := &gctrpc.SavedTradesResponse{
ExchangeName: r.Exchange,
Asset: r.AssetType,
Expand Down
33 changes: 33 additions & 0 deletions engine/rpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,3 +785,36 @@ func TestGetHistoricTrades(t *testing.T) {
t.Error(err)
}
}

func TestGetAccountInfo(t *testing.T) {
bot := SetupTestHelpers(t)
s := RPCServer{Engine: bot}

r, err := s.GetAccountInfo(context.Background(), &gctrpc.GetAccountInfoRequest{Exchange: fakePassExchange})
if err != nil {
t.Fatalf("TestGetAccountInfo: Failed to get account info: %s", err)
}

if r.Accounts[0].Currencies[0].TotalValue != 10 {
t.Fatal("TestGetAccountInfo: Unexpected value of the 'TotalValue'")
}
}

func TestUpdateAccountInfo(t *testing.T) {
bot := SetupTestHelpers(t)
s := RPCServer{Engine: bot}

getResponse, err := s.GetAccountInfo(context.Background(), &gctrpc.GetAccountInfoRequest{Exchange: fakePassExchange})
if err != nil {
t.Fatalf("TestGetAccountInfo: Failed to get account info: %s", err)
}

updateResponse, err := s.UpdateAccountInfo(context.Background(), &gctrpc.GetAccountInfoRequest{Exchange: fakePassExchange})
if err != nil {
t.Fatalf("TestGetAccountInfo: Failed to update account info: %s", err)
}

if getResponse.Accounts[0].Currencies[0].TotalValue == updateResponse.Accounts[0].Currencies[0].TotalValue {
t.Fatalf("TestGetAccountInfo: Unexpected value of the 'TotalValue'")
}
}
6 changes: 3 additions & 3 deletions exchanges/lbank/lbank_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type TickerResponse struct {
// MarketDepthResponse stores arrays for asks, bids and a timestamp for a currecy pair
type MarketDepthResponse struct {
ErrCapture `json:",omitempty"`
Asks [][]float64 `json:"asks"`
Bids [][]float64 `json:"bids"`
Timestamp int64 `json:"timestamp"`
Asks [][]string `json:"asks"`
Bids [][]string `json:"bids"`
Timestamp int64 `json:"timestamp"`
}

// TradeResponse stores date_ms, amount, price, type, tid for a currency pair
Expand Down
27 changes: 23 additions & 4 deletions exchanges/lbank/lbank_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,38 @@ func (l *Lbank) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbo
if err != nil {
return nil, err
}

a, err := l.GetMarketDepths(fpair.String(), "60", "1")
if err != nil {
return orderBook, err
}
for i := range a.Asks {
price, convErr := strconv.ParseFloat(a.Asks[i][0], 64)
if convErr != nil {
return orderBook, convErr
}
amount, convErr := strconv.ParseFloat(a.Asks[i][1], 64)
if convErr != nil {
return orderBook, convErr
}
orderBook.Asks = append(orderBook.Asks, orderbook.Item{
Price: a.Asks[i][0],
Amount: a.Asks[i][1]})
Price: price,
Amount: amount,
})
}
for i := range a.Bids {
price, convErr := strconv.ParseFloat(a.Bids[i][0], 64)
if convErr != nil {
return orderBook, convErr
}
amount, convErr := strconv.ParseFloat(a.Bids[i][1], 64)
if convErr != nil {
return orderBook, convErr
}
orderBook.Bids = append(orderBook.Bids, orderbook.Item{
Price: a.Bids[i][0],
Amount: a.Bids[i][1]})
Price: price,
Amount: amount,
})
}
orderBook.Pair = p
orderBook.ExchangeName = l.Name
Expand Down
Loading

0 comments on commit ba4ac4f

Please sign in to comment.