mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
engine/gRPC: Add update account info to grpc interface (#602)
* Add update account info to grpc interface * Fix lbank tests * Review corrections * Review corrections * Fix linter
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
@@ -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
|
||||
@@ -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,
|
||||
|
||||
@@ -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'")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user