Skip to content

Commit 77089e2

Browse files
set balance to 0 if coin not found
1 parent 005f42e commit 77089e2

File tree

3 files changed

+11
-31
lines changed

3 files changed

+11
-31
lines changed

types/utils.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,17 @@ func UnmarshalMap(metadata map[string]interface{}, output interface{}) error {
305305
func ExtractAmount(
306306
balances []*Amount,
307307
currency *Currency,
308-
) (*Amount, error) {
308+
) *Amount {
309309
for _, b := range balances {
310310
if Hash(b.Currency) != Hash(currency) {
311311
continue
312312
}
313313

314-
return b, nil
314+
return b
315315
}
316316

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

323321
// String returns a pointer to the

types/utils_test.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package types
1717
import (
1818
"encoding/json"
1919
"errors"
20-
"fmt"
2120
"math/big"
2221
"testing"
2322

@@ -621,27 +620,21 @@ func TestExtractAmount(t *testing.T) {
621620
)
622621

623622
t.Run("Non-existent currency", func(t *testing.T) {
624-
result, err := ExtractAmount(balances, badCurr)
625-
assert.Nil(t, result)
626-
assert.EqualError(
623+
result := ExtractAmount(balances, badCurr)
624+
assert.Equal(
627625
t,
628-
err,
629-
fmt.Errorf(
630-
"account balance response does not contain currency %s",
631-
PrettyPrintStruct(badCurr),
632-
).Error(),
626+
result.Value,
627+
"0",
633628
)
634629
})
635630

636631
t.Run("Simple account", func(t *testing.T) {
637-
result, err := ExtractAmount(balances, currency1)
632+
result := ExtractAmount(balances, currency1)
638633
assert.Equal(t, amount1, result)
639-
assert.NoError(t, err)
640634
})
641635

642636
t.Run("SubAccount", func(t *testing.T) {
643-
result, err := ExtractAmount(balances, currency2)
637+
result := ExtractAmount(balances, currency2)
644638
assert.Equal(t, amount2, result)
645-
assert.NoError(t, err)
646639
})
647640
}

utils/utils.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,7 @@ func CurrencyBalance(
429429
return nil, nil, fetchErr.Err
430430
}
431431

432-
liveAmount, err := types.ExtractAmount(liveBalances, currency)
433-
if err != nil {
434-
formattedError := fmt.Errorf(
435-
"%w: could not get %s currency balance for %s",
436-
err,
437-
types.PrettyPrintStruct(currency),
438-
types.PrettyPrintStruct(account),
439-
)
440-
441-
return nil, nil, formattedError
442-
}
443-
432+
liveAmount := types.ExtractAmount(liveBalances, currency)
444433
return liveAmount, liveBlock, nil
445434
}
446435

0 commit comments

Comments
 (0)