Skip to content

Commit

Permalink
Bugfix: Bitstamp race condition and ANX format directive issue (thras…
Browse files Browse the repository at this point in the history
…her-corp#342)

* Fix issue for ANX live testing, incorrect format directive

* Used cpy of Balances as param for Bitstamp function, added space for ANX fatal error.

* Fix linter issue

* Drop struct field for balance and localised a variable in each individual getfee call as needed
  • Loading branch information
shazbert authored and thrasher- committed Aug 26, 2019
1 parent 6d8ba0a commit c191dfc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion exchanges/anx/anx_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestMain(m *testing.M) {
cfg.LoadConfig("../../testdata/configtest.json")
anxConfig, err := cfg.GetExchangeConfig("ANX")
if err != nil {
log.Fatalf("Test Failed - ANX Setup() init error", err)
log.Fatal("Test Failed - ANX Setup() init error ", err)
}
anxConfig.AuthenticatedAPISupport = true
anxConfig.APIKey = apiKey
Expand Down
23 changes: 11 additions & 12 deletions exchanges/bitstamp/bitstamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ const (
// Bitstamp is the overarching type across the bitstamp package
type Bitstamp struct {
exchange.Base
Balance Balances
WebsocketConn *wshandler.WebsocketConnection
}

Expand Down Expand Up @@ -176,15 +175,15 @@ func (b *Bitstamp) GetFee(feeBuilder *exchange.FeeBuilder) (float64, error) {

switch feeBuilder.FeeType {
case exchange.CryptocurrencyTradeFee:
var err error
b.Balance, err = b.GetBalance()
balance, err := b.GetBalance()
if err != nil {
return 0, err
}
fee = b.CalculateTradingFee(feeBuilder.Pair.Base,
feeBuilder.Pair.Quote,
feeBuilder.PurchasePrice,
feeBuilder.Amount)
feeBuilder.Amount,
balance)
case exchange.CyptocurrencyDepositFee:
fee = 0
case exchange.InternationalBankDepositFee:
Expand Down Expand Up @@ -229,20 +228,20 @@ func getInternationalBankDepositFee(amount float64) float64 {
}

// CalculateTradingFee returns fee on a currency pair
func (b *Bitstamp) CalculateTradingFee(base, quote currency.Code, purchasePrice, amount float64) float64 {
func (b *Bitstamp) CalculateTradingFee(base, quote currency.Code, purchasePrice, amount float64, balances *Balances) float64 {
var fee float64

switch base.String() + quote.String() {
case currency.BTC.String() + currency.USD.String():
fee = b.Balance.BTCUSDFee
fee = balances.BTCUSDFee
case currency.BTC.String() + currency.EUR.String():
fee = b.Balance.BTCEURFee
fee = balances.BTCEURFee
case currency.XRP.String() + currency.EUR.String():
fee = b.Balance.XRPEURFee
fee = balances.XRPEURFee
case currency.XRP.String() + currency.USD.String():
fee = b.Balance.XRPUSDFee
fee = balances.XRPUSDFee
case currency.EUR.String() + currency.USD.String():
fee = b.Balance.EURUSDFee
fee = balances.EURUSDFee
default:
fee = 0
}
Expand Down Expand Up @@ -367,9 +366,9 @@ func (b *Bitstamp) GetEURUSDConversionRate() (EURUSDConversionRate, error) {
}

// GetBalance returns full balance of currency held on the exchange
func (b *Bitstamp) GetBalance() (Balances, error) {
func (b *Bitstamp) GetBalance() (*Balances, error) {
var balance Balances
return balance,
return &balance,
b.SendAuthenticatedHTTPRequest(bitstampAPIBalance, true, nil, &balance)
}

Expand Down
14 changes: 8 additions & 6 deletions exchanges/bitstamp/bitstamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,23 @@ func TestGetFee(t *testing.T) {

func TestCalculateTradingFee(t *testing.T) {
t.Parallel()
b.Balance.BTCUSDFee = 1
b.Balance.BTCEURFee = 0

if resp := b.CalculateTradingFee(currency.BTC, currency.USD, 0, 0); resp != 0 {
var newBalance = new(Balances)
newBalance.BTCUSDFee = 1
newBalance.BTCEURFee = 0

if resp := b.CalculateTradingFee(currency.BTC, currency.USD, 0, 0, newBalance); resp != 0 {
t.Error("Test Failed - GetFee() error")
}
if resp := b.CalculateTradingFee(currency.BTC, currency.USD, 2, 2); resp != float64(4) {
if resp := b.CalculateTradingFee(currency.BTC, currency.USD, 2, 2, newBalance); resp != float64(4) {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Received: %f", float64(4), resp)
}
if resp := b.CalculateTradingFee(currency.BTC, currency.EUR, 2, 2); resp != float64(0) {
if resp := b.CalculateTradingFee(currency.BTC, currency.EUR, 2, 2, newBalance); resp != float64(0) {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Received: %f", float64(0), resp)
}

dummy1, dummy2 := currency.NewCode(""), currency.NewCode("")
if resp := b.CalculateTradingFee(dummy1, dummy2, 0, 0); resp != 0 {
if resp := b.CalculateTradingFee(dummy1, dummy2, 0, 0, newBalance); resp != 0 {
t.Error("Test Failed - GetFee() error")
}
}
Expand Down

0 comments on commit c191dfc

Please sign in to comment.