diff --git a/exchanges/bitstamp/bitstamp.go b/exchanges/bitstamp/bitstamp.go index bccfad5e..fd48f277 100644 --- a/exchanges/bitstamp/bitstamp.go +++ b/exchanges/bitstamp/bitstamp.go @@ -226,39 +226,43 @@ func (b *Bitstamp) GetBalance(ctx context.Context) (Balances, error) { if err != nil { return nil, err } - balances := make(map[string]Balance) + currs := []string{} for k := range balance { - curr := k[0:3] - _, ok := balances[strings.ToUpper(curr)] - if !ok { - avail, _ := strconv.ParseFloat(balance[curr+"_available"], 64) - bal, _ := strconv.ParseFloat(balance[curr+"_balance"], 64) - reserved, _ := strconv.ParseFloat(balance[curr+"_reserved"], 64) - withdrawalFee, _ := strconv.ParseFloat(balance[curr+"_withdrawal_fee"], 64) - currBalance := Balance{ - Available: avail, - Balance: bal, - Reserved: reserved, - WithdrawalFee: withdrawalFee, - } - switch strings.ToUpper(curr) { - case currency.USD.String(): - eurFee, _ := strconv.ParseFloat(balance[curr+"eur_fee"], 64) - currBalance.EURFee = eurFee - case currency.EUR.String(): - usdFee, _ := strconv.ParseFloat(balance[curr+"usd_fee"], 64) - currBalance.USDFee = usdFee - default: - btcFee, _ := strconv.ParseFloat(balance[curr+"btc_fee"], 64) - currBalance.BTCFee = btcFee - eurFee, _ := strconv.ParseFloat(balance[curr+"eur_fee"], 64) - currBalance.EURFee = eurFee - usdFee, _ := strconv.ParseFloat(balance[curr+"usd_fee"], 64) - currBalance.USDFee = usdFee - } - balances[strings.ToUpper(curr)] = currBalance + if strings.HasSuffix(k, "_balance") { + curr, _, _ := strings.Cut(k, "_") + currs = append(currs, curr) } } + + balances := make(map[string]Balance) + for _, curr := range currs { + avail, _ := strconv.ParseFloat(balance[curr+"_available"], 64) + bal, _ := strconv.ParseFloat(balance[curr+"_balance"], 64) + reserved, _ := strconv.ParseFloat(balance[curr+"_reserved"], 64) + withdrawalFee, _ := strconv.ParseFloat(balance[curr+"_withdrawal_fee"], 64) + currBalance := Balance{ + Available: avail, + Balance: bal, + Reserved: reserved, + WithdrawalFee: withdrawalFee, + } + switch strings.ToUpper(curr) { + case currency.USD.String(): + eurFee, _ := strconv.ParseFloat(balance[curr+"eur_fee"], 64) + currBalance.EURFee = eurFee + case currency.EUR.String(): + usdFee, _ := strconv.ParseFloat(balance[curr+"usd_fee"], 64) + currBalance.USDFee = usdFee + default: + btcFee, _ := strconv.ParseFloat(balance[curr+"btc_fee"], 64) + currBalance.BTCFee = btcFee + eurFee, _ := strconv.ParseFloat(balance[curr+"eur_fee"], 64) + currBalance.EURFee = eurFee + usdFee, _ := strconv.ParseFloat(balance[curr+"usd_fee"], 64) + currBalance.USDFee = usdFee + } + balances[strings.ToUpper(curr)] = currBalance + } return balances, nil } diff --git a/exchanges/bitstamp/bitstamp_test.go b/exchanges/bitstamp/bitstamp_test.go index fc42ad3c..6d20f709 100644 --- a/exchanges/bitstamp/bitstamp_test.go +++ b/exchanges/bitstamp/bitstamp_test.go @@ -230,7 +230,7 @@ func TestGetEURUSDConversionRate(t *testing.T) { func TestGetBalance(t *testing.T) { t.Parallel() - _, err := b.GetBalance(context.Background()) + bal, err := b.GetBalance(context.Background()) switch { case sharedtestvalues.AreAPICredentialsSet(b) && err != nil && !mockTests: t.Error("GetBalance() error", err) @@ -238,6 +238,43 @@ func TestGetBalance(t *testing.T) { t.Error("Expecting an error when no keys are set") case mockTests && err != nil: t.Error("GetBalance() error", err) + case mockTests: + for k, e := range map[string]Balance{ + "USDT": { + Available: 42.42, + Balance: 1337.42, + Reserved: 1295.00, + WithdrawalFee: 5.0, + USDFee: 0, + }, + "BTC": { + Available: 9.1, + Balance: 11.2, + Reserved: 2.1, + WithdrawalFee: 0.00050000, + USDFee: 0.25, + }, + } { + if got, ok := bal[k]; !ok { + t.Error("Expected to find USDT balance") + } else { + if got.Available != e.Available { + t.Errorf("Incorrect Available balance for %s; Expected: %v Got: %v", k, e.Available, got.Available) + } + if got.Balance != e.Balance { + t.Errorf("Incorrect Balance for %s; Expected: %v Got: %v", k, e.Balance, got.Balance) + } + if got.Reserved != e.Reserved { + t.Errorf("Incorrect Reserved balance for %s; Expected: %v Got: %v", k, e.Reserved, got.Reserved) + } + if got.WithdrawalFee != e.WithdrawalFee { + t.Errorf("Incorrect WithdrawalFee for %s; Expected: %v Got: %v", k, e.WithdrawalFee, got.WithdrawalFee) + } + if got.USDFee != e.USDFee { + t.Errorf("Incorrect USDFee for %s; Expected: %v Got: %v", k, e.USDFee, got.USDFee) + } + } + } } } diff --git a/testdata/http_mock/bitstamp/bitstamp.json b/testdata/http_mock/bitstamp/bitstamp.json index 6e802c4f..2155c855 100644 --- a/testdata/http_mock/bitstamp/bitstamp.json +++ b/testdata/http_mock/bitstamp/bitstamp.json @@ -105,39 +105,38 @@ "bch_available": "0.00000000", "bch_balance": "0.00000000", "bch_reserved": "0.00000000", - "bchbtc_fee": "0.25", - "bcheur_fee": "0.25", - "bchusd_fee": "0.25", - "btc_available": "0.00000000", - "btc_balance": "0.00000000", - "btc_reserved": "0.00000000", - "btceur_fee": "0.25", + "bch_withdrawal_fee": "0.00000000", + "bch_withdrawal_fee": "0.25", + "btc_available": "9.1000000", + "btc_balance": "11.20000000", + "btc_reserved": "2.10000000", + "btc_withdrawal_fee": "0.00050000", + "btceur_fee": "10.25", "btcusd_fee": "0.25", "eth_available": "0.00000000", "eth_balance": "0.00000000", "eth_reserved": "0.00000000", - "ethbtc_fee": "0.25", - "etheur_fee": "0.25", - "ethusd_fee": "0.25", + "eth_withdrawal_fee": "0.25", "eur_available": "0.00", "eur_balance": "0.00", "eur_reserved": "0.00", - "eurusd_fee": "0.25", + "eur_withdrawal_fee": "0.25", "ltc_available": "0.00000000", "ltc_balance": "0.00000000", "ltc_reserved": "0.00000000", - "ltcbtc_fee": "0.25", - "ltceur_fee": "0.25", - "ltcusd_fee": "0.25", + "ltc_withdrawal_fee": "0.25", "usd_available": "0.00", "usd_balance": "0.00", "usd_reserved": "0.00", + "usd_withdrawal_fee": "5.00", + "usdt_available": "42.42", + "usdt_balance": "1337.42", + "usdt_reserved": "1295.00", + "usdt_withdrawal_fee": "5.00", "xrp_available": "0.00000000", "xrp_balance": "0.00000000", "xrp_reserved": "0.00000000", - "xrpbtc_fee": "0.25", - "xrpeur_fee": "0.25", - "xrpusd_fee": "0.25" + "xrp_withdrawal_fee": "0.25" }, "queryString": "", "bodyParams": "key=\u0026nonce=1560481519007838128\u0026signature=C7558B2B2E75E9057994271CCC0200835887CDC63EC4103220489C52F749BFFA",