Fix account_service
This commit is contained in:
parent
be42005441
commit
d9fb294826
2 changed files with 74 additions and 62 deletions
|
@ -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(
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue