From d9fb29482634d9f00e36a603da030fd2745295f3 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Fri, 13 Nov 2020 11:42:33 -0800 Subject: [PATCH] Fix account_service --- services/account_service.go | 28 -------- services/account_service_test.go | 108 +++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 62 deletions(-) diff --git a/services/account_service.go b/services/account_service.go index 5edcfa4..748fcad 100644 --- a/services/account_service.go +++ b/services/account_service.go @@ -51,34 +51,6 @@ func (s *AccountAPIService) AccountBalance( // TODO: filter balances by request currencies - // If we are fetching the current balance, - // return all coins for an address and calculate - // the balance from those coins. - if request.BlockIdentifier == nil { - coins, block, err := s.i.GetCoins(ctx, request.AccountIdentifier) - if err != nil { - return nil, wrapErr(ErrUnableToGetCoins, err) - } - - balance := "0" - for _, coin := range coins { - balance, err = types.AddValues(balance, coin.Amount.Value) - if err != nil { - return nil, wrapErr(ErrUnableToParseIntermediateResult, err) - } - } - - return &types.AccountBalanceResponse{ - BlockIdentifier: block, - Balances: []*types.Amount{ - { - Value: balance, - Currency: s.config.Currency, - }, - }, - }, nil - } - // If we are fetching a historical balance, // use balance storage and don't return coins. amount, block, err := s.i.GetBalance( diff --git a/services/account_service_test.go b/services/account_service_test.go index 323e6c5..172661d 100644 --- a/services/account_service_test.go +++ b/services/account_service_test.go @@ -38,6 +38,10 @@ func TestAccountBalance_Offline(t *testing.T) { assert.Nil(t, bal) assert.Equal(t, ErrUnavailableOffline.Code, err.Code) + coins, err := servicer.AccountCoins(ctx, &types.AccountCoinsRequest{}) + assert.Nil(t, coins) + assert.Equal(t, ErrUnavailableOffline.Code, err.Code) + mockIndexer.AssertExpectations(t) } @@ -49,55 +53,33 @@ func TestAccountBalance_Online_Current(t *testing.T) { mockIndexer := &mocks.Indexer{} servicer := NewAccountAPIService(cfg, mockIndexer) ctx := context.Background() - account := &types.AccountIdentifier{ Address: "hello", } - - coins := []*types.Coin{ - { - Amount: &types.Amount{ - Value: "10", - }, - CoinIdentifier: &types.CoinIdentifier{ - Identifier: "coin 1", - }, - }, - { - Amount: &types.Amount{ - Value: "15", - }, - CoinIdentifier: &types.CoinIdentifier{ - Identifier: "coin 2", - }, - }, - { - Amount: &types.Amount{ - Value: "0", - }, - CoinIdentifier: &types.CoinIdentifier{ - Identifier: "coin 3", - }, - }, - } block := &types.BlockIdentifier{ Index: 1000, Hash: "block 1000", } - mockIndexer.On("GetCoins", ctx, account).Return(coins, block, nil).Once() + amount := &types.Amount{ + Value: "25", + Currency: bitcoin.MainnetCurrency, + } + mockIndexer.On( + "GetBalance", + ctx, + account, + bitcoin.MainnetCurrency, + (*types.PartialBlockIdentifier)(nil), + ).Return(amount, block, nil).Once() bal, err := servicer.AccountBalance(ctx, &types.AccountBalanceRequest{ AccountIdentifier: account, }) assert.Nil(t, err) - assert.Equal(t, &types.AccountBalanceResponse{ BlockIdentifier: block, Balances: []*types.Amount{ - { - Value: "25", - Currency: bitcoin.MainnetCurrency, - }, + amount, }, }, bal) @@ -148,3 +130,61 @@ func TestAccountBalance_Online_Historical(t *testing.T) { mockIndexer.AssertExpectations(t) } + +func TestAccountCoins_Online(t *testing.T) { + cfg := &configuration.Configuration{ + Mode: configuration.Online, + Currency: bitcoin.MainnetCurrency, + } + mockIndexer := &mocks.Indexer{} + servicer := NewAccountAPIService(cfg, mockIndexer) + ctx := context.Background() + + account := &types.AccountIdentifier{ + Address: "hello", + } + + coins := []*types.Coin{ + { + Amount: &types.Amount{ + Value: "10", + }, + CoinIdentifier: &types.CoinIdentifier{ + Identifier: "coin 1", + }, + }, + { + Amount: &types.Amount{ + Value: "15", + }, + CoinIdentifier: &types.CoinIdentifier{ + Identifier: "coin 2", + }, + }, + { + Amount: &types.Amount{ + Value: "0", + }, + CoinIdentifier: &types.CoinIdentifier{ + Identifier: "coin 3", + }, + }, + } + block := &types.BlockIdentifier{ + Index: 1000, + Hash: "block 1000", + } + mockIndexer.On("GetCoins", ctx, account).Return(coins, block, nil).Once() + + bal, err := servicer.AccountCoins(ctx, &types.AccountCoinsRequest{ + AccountIdentifier: account, + }) + assert.Nil(t, err) + + assert.Equal(t, &types.AccountCoinsResponse{ + BlockIdentifier: block, + Coins: coins, + }, bal) + + mockIndexer.AssertExpectations(t) +}