Skip to content

Commit

Permalink
set balance to 0 if coin not found
Browse files Browse the repository at this point in the history
  • Loading branch information
fkneeland-figure committed Nov 30, 2021
1 parent 005f42e commit 77089e2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
10 changes: 4 additions & 6 deletions types/utils.go
Expand Up @@ -305,19 +305,17 @@ func UnmarshalMap(metadata map[string]interface{}, output interface{}) error {
func ExtractAmount(
balances []*Amount,
currency *Currency,
) (*Amount, error) {
) *Amount {
for _, b := range balances {
if Hash(b.Currency) != Hash(currency) {
continue
}

return b, nil
return b
}

return nil, fmt.Errorf(
"account balance response does not contain currency %s",
PrettyPrintStruct(currency),
)
// return a 0 amount if currency isn't found in balances
return &Amount{Value: "0", Currency: currency}
}

// String returns a pointer to the
Expand Down
19 changes: 6 additions & 13 deletions types/utils_test.go
Expand Up @@ -17,7 +17,6 @@ package types
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -621,27 +620,21 @@ func TestExtractAmount(t *testing.T) {
)

t.Run("Non-existent currency", func(t *testing.T) {
result, err := ExtractAmount(balances, badCurr)
assert.Nil(t, result)
assert.EqualError(
result := ExtractAmount(balances, badCurr)
assert.Equal(
t,
err,
fmt.Errorf(
"account balance response does not contain currency %s",
PrettyPrintStruct(badCurr),
).Error(),
result.Value,
"0",
)
})

t.Run("Simple account", func(t *testing.T) {
result, err := ExtractAmount(balances, currency1)
result := ExtractAmount(balances, currency1)
assert.Equal(t, amount1, result)
assert.NoError(t, err)
})

t.Run("SubAccount", func(t *testing.T) {
result, err := ExtractAmount(balances, currency2)
result := ExtractAmount(balances, currency2)
assert.Equal(t, amount2, result)
assert.NoError(t, err)
})
}
13 changes: 1 addition & 12 deletions utils/utils.go
Expand Up @@ -429,18 +429,7 @@ func CurrencyBalance(
return nil, nil, fetchErr.Err
}

liveAmount, err := types.ExtractAmount(liveBalances, currency)
if err != nil {
formattedError := fmt.Errorf(
"%w: could not get %s currency balance for %s",
err,
types.PrettyPrintStruct(currency),
types.PrettyPrintStruct(account),
)

return nil, nil, formattedError
}

liveAmount := types.ExtractAmount(liveBalances, currency)
return liveAmount, liveBlock, nil
}

Expand Down

0 comments on commit 77089e2

Please sign in to comment.