Skip to content

Commit

Permalink
Merge pull request #235 from coinbase/patrick/fix-utils
Browse files Browse the repository at this point in the history
[utils] Update `FetcherHelper`
  • Loading branch information
patrick-ogrady committed Nov 10, 2020
2 parents d86a156 + 99bcd55 commit e6be00e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 54 deletions.
41 changes: 16 additions & 25 deletions mocks/utils/fetcher_helper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ type FetcherHelper interface {
network *types.NetworkIdentifier,
account *types.AccountIdentifier,
block *types.PartialBlockIdentifier,
) (*types.BlockIdentifier, []*types.Amount, []*types.Coin, map[string]interface{}, *fetcher.Error)
currencies []*types.Currency,
) (*types.BlockIdentifier, []*types.Amount, map[string]interface{}, *fetcher.Error)
}

// CheckNetworkSupported checks if a Rosetta implementation supports a given
Expand Down Expand Up @@ -312,22 +313,23 @@ func CurrencyBalance(
account *types.AccountIdentifier,
currency *types.Currency,
index int64,
) (*types.Amount, *types.BlockIdentifier, []*types.Coin, error) {
) (*types.Amount, *types.BlockIdentifier, error) {
var lookupBlock *types.PartialBlockIdentifier
if index >= 0 {
lookupBlock = &types.PartialBlockIdentifier{
Index: &index,
}
}

liveBlock, liveBalances, liveCoins, _, fetchErr := helper.AccountBalanceRetry(
liveBlock, liveBalances, _, fetchErr := helper.AccountBalanceRetry(
ctx,
network,
account,
lookupBlock,
[]*types.Currency{currency},
)
if fetchErr != nil {
return nil, nil, nil, fetchErr.Err
return nil, nil, fetchErr.Err
}

liveAmount, err := types.ExtractAmount(liveBalances, currency)
Expand All @@ -339,10 +341,10 @@ func CurrencyBalance(
types.PrettyPrintStruct(account),
)

return nil, nil, nil, formattedError
return nil, nil, formattedError
}

return liveAmount, liveBlock, liveCoins, nil
return liveAmount, liveBlock, nil
}

// AccountBalanceRequest defines the required information
Expand Down Expand Up @@ -372,7 +374,7 @@ func GetAccountBalances(
) ([]*AccountBalance, error) {
var accountBalances []*AccountBalance
for _, balanceRequest := range balanceRequests {
amount, block, coins, err := CurrencyBalance(
amount, block, err := CurrencyBalance(
ctx,
balanceRequest.Network,
fetcher,
Expand All @@ -388,7 +390,6 @@ func GetAccountBalances(
accountBalance := &AccountBalance{
Account: balanceRequest.Account,
Amount: amount,
Coins: coins,
Block: block,
}

Expand Down
24 changes: 3 additions & 21 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,23 +270,6 @@ var (
Currency: currency,
}

accountCoins = []*types.Coin{
{
CoinIdentifier: &types.CoinIdentifier{Identifier: "coin1"},
Amount: &types.Amount{
Value: "30",
Currency: currency,
},
},
{
CoinIdentifier: &types.CoinIdentifier{Identifier: "coin2"},
Amount: &types.Amount{
Value: "30",
Currency: currency,
},
},
}

accountBalance = &types.AccountIdentifier{
Address: "test2",
}
Expand All @@ -305,7 +288,6 @@ var (
accBalanceResp1 = &AccountBalance{
Account: accountCoin,
Amount: amountCoins,
Coins: accountCoins,
Block: blockIdentifier,
}

Expand Down Expand Up @@ -333,10 +315,10 @@ func TestGetAccountBalances(t *testing.T) {
network,
accountCoin,
(*types.PartialBlockIdentifier)(nil),
[]*types.Currency{currency},
).Return(
blockIdentifier,
[]*types.Amount{amountCoins},
accountCoins,
nil,
nil,
).Once()
Expand All @@ -347,12 +329,12 @@ func TestGetAccountBalances(t *testing.T) {
network,
accountBalance,
(*types.PartialBlockIdentifier)(nil),
[]*types.Currency{currency},
).Return(
blockIdentifier,
[]*types.Amount{amountBalance},
nil,
nil,
nil,
).Once()

accBalances, err := GetAccountBalances(
Expand All @@ -372,11 +354,11 @@ func TestGetAccountBalances(t *testing.T) {
network,
accountBalance,
(*types.PartialBlockIdentifier)(nil),
[]*types.Currency{currency},
).Return(
nil,
nil,
nil,
nil,
&fetcher.Error{
Err: fmt.Errorf("invalid account balance"),
},
Expand Down

0 comments on commit e6be00e

Please sign in to comment.