exchanges: Initial context propagation (#744)

* gct: phase one context awareness pass

* exchanges: context propagation pass

* common/requester: force context requirement

* gctcli/exchanges: linter fix

* rpcserver: fix test using dummy rpc server

* backtester: fix comments

* grpc: add correct cancel and timeout for commands

* rpcserver_test: add comment on dummy server

* common: deprecated SendHTTPGetRequest

* linter: fix

* linter: turn on no context check

* apichecker: fix context linter issue

* binance: use param context

* common: remove checks as this gets executed before main

* common: change mutex to RW as clients can be used by multiple go routines.

* common: remove init and JIT default client. Unexport global variables and add protection.

* common: Add comments

* bithumb: after dinner mints fix
This commit is contained in:
Ryan O'Hara-Reid
2021-09-11 13:52:07 +10:00
committed by GitHub
parent 72516f7268
commit d636049fb2
168 changed files with 8085 additions and 6996 deletions

View File

@@ -1,6 +1,7 @@
package bithumb
import (
"context"
"errors"
"log"
"os"
@@ -54,7 +55,7 @@ func TestMain(m *testing.M) {
func TestGetTradablePairs(t *testing.T) {
t.Parallel()
_, err := b.GetTradablePairs()
_, err := b.GetTradablePairs(context.Background())
if err != nil {
t.Error("Bithumb GetTradablePairs() error", err)
}
@@ -62,7 +63,7 @@ func TestGetTradablePairs(t *testing.T) {
func TestGetTicker(t *testing.T) {
t.Parallel()
_, err := b.GetTicker(testCurrency)
_, err := b.GetTicker(context.Background(), testCurrency)
if err != nil {
t.Error("Bithumb GetTicker() error", err)
}
@@ -70,7 +71,7 @@ func TestGetTicker(t *testing.T) {
func TestGetAllTickers(t *testing.T) {
t.Parallel()
_, err := b.GetAllTickers()
_, err := b.GetAllTickers(context.Background())
if err != nil {
t.Error("Bithumb GetAllTickers() error", err)
}
@@ -78,7 +79,7 @@ func TestGetAllTickers(t *testing.T) {
func TestGetOrderBook(t *testing.T) {
t.Parallel()
_, err := b.GetOrderBook(testCurrency)
_, err := b.GetOrderBook(context.Background(), testCurrency)
if err != nil {
t.Error("Bithumb GetOrderBook() error", err)
}
@@ -86,7 +87,7 @@ func TestGetOrderBook(t *testing.T) {
func TestGetTransactionHistory(t *testing.T) {
t.Parallel()
_, err := b.GetTransactionHistory(testCurrency)
_, err := b.GetTransactionHistory(context.Background(), testCurrency)
if err != nil {
t.Error("Bithumb GetTransactionHistory() error", err)
}
@@ -96,7 +97,7 @@ func TestGetAccountInformation(t *testing.T) {
t.Parallel()
// Offline test
_, err := b.GetAccountInformation("", "")
_, err := b.GetAccountInformation(context.Background(), "", "")
if err == nil {
t.Error("expected error when no order currency is specified")
}
@@ -105,7 +106,9 @@ func TestGetAccountInformation(t *testing.T) {
t.Skip()
}
_, err = b.GetAccountInformation(testCurrency, currency.KRW.String())
_, err = b.GetAccountInformation(context.Background(),
testCurrency,
currency.KRW.String())
if err != nil {
t.Error(err)
}
@@ -117,7 +120,7 @@ func TestGetAccountBalance(t *testing.T) {
t.Skip()
}
_, err := b.GetAccountBalance(testCurrency)
_, err := b.GetAccountBalance(context.Background(), testCurrency)
if err == nil {
t.Error("Bithumb GetAccountBalance() Expected error")
}
@@ -129,7 +132,7 @@ func TestGetWalletAddress(t *testing.T) {
t.Skip()
}
_, err := b.GetWalletAddress("")
_, err := b.GetWalletAddress(context.Background(), "")
if err == nil {
t.Error("Bithumb GetWalletAddress() Expected error")
}
@@ -137,7 +140,7 @@ func TestGetWalletAddress(t *testing.T) {
func TestGetLastTransaction(t *testing.T) {
t.Parallel()
_, err := b.GetLastTransaction()
_, err := b.GetLastTransaction(context.Background())
if err == nil {
t.Error("Bithumb GetLastTransaction() Expected error")
}
@@ -145,7 +148,8 @@ func TestGetLastTransaction(t *testing.T) {
func TestGetOrders(t *testing.T) {
t.Parallel()
_, err := b.GetOrders("1337", order.Bid.Lower(), "100", "", testCurrency)
_, err := b.GetOrders(context.Background(),
"1337", order.Bid.Lower(), "100", "", testCurrency)
if err == nil {
t.Error("Bithumb GetOrders() Expected error")
}
@@ -153,7 +157,7 @@ func TestGetOrders(t *testing.T) {
func TestGetUserTransactions(t *testing.T) {
t.Parallel()
_, err := b.GetUserTransactions()
_, err := b.GetUserTransactions(context.Background())
if err == nil {
t.Error("Bithumb GetUserTransactions() Expected error")
}
@@ -161,7 +165,8 @@ func TestGetUserTransactions(t *testing.T) {
func TestPlaceTrade(t *testing.T) {
t.Parallel()
_, err := b.PlaceTrade(testCurrency, order.Bid.Lower(), 0, 0)
_, err := b.PlaceTrade(context.Background(),
testCurrency, order.Bid.Lower(), 0, 0)
if err == nil {
t.Error("Bithumb PlaceTrade() Expected error")
}
@@ -169,7 +174,8 @@ func TestPlaceTrade(t *testing.T) {
func TestGetOrderDetails(t *testing.T) {
t.Parallel()
_, err := b.GetOrderDetails("1337", order.Bid.Lower(), testCurrency)
_, err := b.GetOrderDetails(context.Background(),
"1337", order.Bid.Lower(), testCurrency)
if err == nil {
t.Error("Bithumb GetOrderDetails() Expected error")
}
@@ -177,7 +183,7 @@ func TestGetOrderDetails(t *testing.T) {
func TestCancelTrade(t *testing.T) {
t.Parallel()
_, err := b.CancelTrade("", "", "")
_, err := b.CancelTrade(context.Background(), "", "", "")
if err == nil {
t.Error("Bithumb CancelTrade() Expected error")
}
@@ -185,7 +191,8 @@ func TestCancelTrade(t *testing.T) {
func TestWithdrawCrypto(t *testing.T) {
t.Parallel()
_, err := b.WithdrawCrypto("LQxiDhKU7idKiWQhx4ALKYkBx8xKEQVxJR", "", "ltc", 0)
_, err := b.WithdrawCrypto(context.Background(),
"LQxiDhKU7idKiWQhx4ALKYkBx8xKEQVxJR", "", "ltc", 0)
if err == nil {
t.Error("Bithumb WithdrawCrypto() Expected error")
}
@@ -196,7 +203,7 @@ func TestRequestKRWDepositDetails(t *testing.T) {
if !areTestAPIKeysSet() {
t.Skip()
}
_, err := b.RequestKRWDepositDetails()
_, err := b.RequestKRWDepositDetails(context.Background())
if err == nil {
t.Error("Bithumb RequestKRWDepositDetails() Expected error")
}
@@ -204,7 +211,8 @@ func TestRequestKRWDepositDetails(t *testing.T) {
func TestRequestKRWWithdraw(t *testing.T) {
t.Parallel()
_, err := b.RequestKRWWithdraw("102_bank", "1337", 1000)
_, err := b.RequestKRWWithdraw(context.Background(),
"102_bank", "1337", 1000)
if err == nil {
t.Error("Bithumb RequestKRWWithdraw() Expected error")
}
@@ -213,7 +221,7 @@ func TestRequestKRWWithdraw(t *testing.T) {
func TestMarketBuyOrder(t *testing.T) {
t.Parallel()
p := currency.NewPair(currency.BTC, currency.KRW)
_, err := b.MarketBuyOrder(p, 0)
_, err := b.MarketBuyOrder(context.Background(), p, 0)
if err == nil {
t.Error("Bithumb MarketBuyOrder() Expected error")
}
@@ -222,7 +230,7 @@ func TestMarketBuyOrder(t *testing.T) {
func TestMarketSellOrder(t *testing.T) {
t.Parallel()
p := currency.NewPair(currency.BTC, currency.KRW)
_, err := b.MarketSellOrder(p, 0)
_, err := b.MarketSellOrder(context.Background(), p, 0)
if err == nil {
t.Error("Bithumb MarketSellOrder() Expected error")
}
@@ -231,13 +239,13 @@ func TestMarketSellOrder(t *testing.T) {
func TestUpdateTicker(t *testing.T) {
t.Parallel()
cp := currency.NewPair(currency.QTUM, currency.KRW)
_, err := b.UpdateTicker(cp, asset.Spot)
_, err := b.UpdateTicker(context.Background(), cp, asset.Spot)
if err != nil {
t.Fatal(err)
}
cp = currency.NewPair(currency.BTC, currency.KRW)
_, err = b.UpdateTicker(cp, asset.Spot)
_, err = b.UpdateTicker(context.Background(), cp, asset.Spot)
if err != nil {
t.Fatal(err)
}
@@ -245,7 +253,7 @@ func TestUpdateTicker(t *testing.T) {
func TestUpdateTickers(t *testing.T) {
t.Parallel()
err := b.UpdateTickers(asset.Spot)
err := b.UpdateTickers(context.Background(), asset.Spot)
if err != nil {
t.Fatal(err)
}
@@ -264,7 +272,7 @@ func setFeeBuilder() *exchange.FeeBuilder {
func TestGetFeeByTypeOfflineTradeFee(t *testing.T) {
t.Parallel()
var feeBuilder = setFeeBuilder()
_, err := b.GetFeeByType(feeBuilder)
_, err := b.GetFeeByType(context.Background(), feeBuilder)
if err != nil {
t.Fatal(err)
}
@@ -357,7 +365,7 @@ func TestGetActiveOrders(t *testing.T) {
AssetType: asset.Spot,
}
_, err := b.GetActiveOrders(&getOrdersRequest)
_, err := b.GetActiveOrders(context.Background(), &getOrdersRequest)
if areTestAPIKeysSet() && err != nil {
t.Errorf("Could not get open orders: %s", err)
} else if !areTestAPIKeysSet() && err == nil {
@@ -372,7 +380,7 @@ func TestGetOrderHistory(t *testing.T) {
AssetType: asset.Spot,
}
_, err := b.GetOrderHistory(&getOrdersRequest)
_, err := b.GetOrderHistory(context.Background(), &getOrdersRequest)
if areTestAPIKeysSet() && err != nil {
t.Errorf("Could not get order history: %s", err)
} else if !areTestAPIKeysSet() && err == nil {
@@ -404,7 +412,7 @@ func TestSubmitOrder(t *testing.T) {
ClientID: "meowOrder",
AssetType: asset.Spot,
}
response, err := b.SubmitOrder(orderSubmission)
response, err := b.SubmitOrder(context.Background(), orderSubmission)
if areTestAPIKeysSet() && (err != nil || !response.IsOrderPlaced) {
t.Errorf("Order failed to be placed: %v", err)
} else if !areTestAPIKeysSet() && err == nil {
@@ -427,7 +435,7 @@ func TestCancelExchangeOrder(t *testing.T) {
AssetType: asset.Spot,
}
err := b.CancelOrder(orderCancellation)
err := b.CancelOrder(context.Background(), orderCancellation)
if !areTestAPIKeysSet() && err == nil {
t.Error("Expecting an error when no keys are set")
}
@@ -451,7 +459,7 @@ func TestCancelAllExchangeOrders(t *testing.T) {
AssetType: asset.Spot,
}
resp, err := b.CancelAllOrders(orderCancellation)
resp, err := b.CancelAllOrders(context.Background(), orderCancellation)
if !areTestAPIKeysSet() && err == nil {
t.Error("Expecting an error when no keys are set")
@@ -468,12 +476,12 @@ func TestCancelAllExchangeOrders(t *testing.T) {
func TestGetAccountInfo(t *testing.T) {
t.Parallel()
if areTestAPIKeysSet() {
_, err := b.UpdateAccountInfo(asset.Spot)
_, err := b.UpdateAccountInfo(context.Background(), asset.Spot)
if err != nil {
t.Error("Bithumb GetAccountInfo() error", err)
}
} else {
_, err := b.UpdateAccountInfo(asset.Spot)
_, err := b.UpdateAccountInfo(context.Background(), asset.Spot)
if err == nil {
t.Error("Bithumb GetAccountInfo() Expected error")
}
@@ -486,7 +494,7 @@ func TestModifyOrder(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = b.ModifyOrder(&order.Modify{
_, err = b.ModifyOrder(context.Background(), &order.Modify{
ID: "1337",
Price: 100,
Amount: 1000,
@@ -514,7 +522,8 @@ func TestWithdraw(t *testing.T) {
},
}
_, err := b.WithdrawCryptocurrencyFunds(&withdrawCryptoRequest)
_, err := b.WithdrawCryptocurrencyFunds(context.Background(),
&withdrawCryptoRequest)
if !areTestAPIKeysSet() && err == nil {
t.Error("Expecting an error when no keys are set")
}
@@ -540,7 +549,7 @@ func TestWithdrawFiat(t *testing.T) {
Description: "WITHDRAW IT ALL",
}
_, err := b.WithdrawFiatFunds(&withdrawFiatRequest)
_, err := b.WithdrawFiatFunds(context.Background(), &withdrawFiatRequest)
if !areTestAPIKeysSet() && err == nil {
t.Error("Expecting an error when no keys are set")
}
@@ -556,7 +565,8 @@ func TestWithdrawInternationalBank(t *testing.T) {
}
var withdrawFiatRequest = withdraw.Request{}
_, err := b.WithdrawFiatFundsToInternationalBank(&withdrawFiatRequest)
_, err := b.WithdrawFiatFundsToInternationalBank(context.Background(),
&withdrawFiatRequest)
if err != common.ErrFunctionNotSupported {
t.Errorf("Expected '%v', received: '%v'", common.ErrFunctionNotSupported, err)
}
@@ -565,12 +575,12 @@ func TestWithdrawInternationalBank(t *testing.T) {
func TestGetDepositAddress(t *testing.T) {
t.Parallel()
if areTestAPIKeysSet() {
_, err := b.GetDepositAddress(currency.BTC, "")
_, err := b.GetDepositAddress(context.Background(), currency.BTC, "")
if err != nil {
t.Error("GetDepositAddress() error", err)
}
} else {
_, err := b.GetDepositAddress(currency.BTC, "")
_, err := b.GetDepositAddress(context.Background(), currency.BTC, "")
if err == nil {
t.Error("GetDepositAddress() error cannot be nil")
}
@@ -579,7 +589,7 @@ func TestGetDepositAddress(t *testing.T) {
func TestGetCandleStick(t *testing.T) {
t.Parallel()
_, err := b.GetCandleStick("BTC_KRW", "1m")
_, err := b.GetCandleStick(context.Background(), "BTC_KRW", "1m")
if err != nil {
t.Fatal(err)
}
@@ -592,7 +602,8 @@ func TestGetHistoricCandles(t *testing.T) {
t.Fatal(err)
}
startTime := time.Now().Add(-time.Hour * 24)
_, err = b.GetHistoricCandles(currencyPair, asset.Spot, startTime, time.Now(), kline.OneDay)
_, err = b.GetHistoricCandles(context.Background(),
currencyPair, asset.Spot, startTime, time.Now(), kline.OneDay)
if err != nil {
t.Fatal(err)
}
@@ -605,7 +616,8 @@ func TestGetHistoricCandlesExtended(t *testing.T) {
t.Fatal(err)
}
startTime := time.Now().Add(-time.Hour * 24)
_, err = b.GetHistoricCandlesExtended(currencyPair, asset.Spot, startTime, time.Now(), kline.OneDay)
_, err = b.GetHistoricCandlesExtended(context.Background(),
currencyPair, asset.Spot, startTime, time.Now(), kline.OneDay)
if err != nil {
t.Fatal(err)
}
@@ -617,7 +629,7 @@ func TestGetRecentTrades(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = b.GetRecentTrades(currencyPair, asset.Spot)
_, err = b.GetRecentTrades(context.Background(), currencyPair, asset.Spot)
if err != nil {
t.Error(err)
}
@@ -629,7 +641,8 @@ func TestGetHistoricTrades(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = b.GetHistoricTrades(currencyPair, asset.Spot, time.Now().Add(-time.Minute*15), time.Now())
_, err = b.GetHistoricTrades(context.Background(),
currencyPair, asset.Spot, time.Now().Add(-time.Minute*15), time.Now())
if err != nil && err != common.ErrFunctionNotSupported {
t.Error(err)
}
@@ -637,7 +650,7 @@ func TestGetHistoricTrades(t *testing.T) {
func TestUpdateOrderExecutionLimits(t *testing.T) {
t.Parallel()
err := b.UpdateOrderExecutionLimits("")
err := b.UpdateOrderExecutionLimits(context.Background(), "")
if err != nil {
t.Fatal(err)
}
@@ -718,12 +731,12 @@ func TestGetAmountMinimum(t *testing.T) {
func TestGetAssetStatus(t *testing.T) {
t.Parallel()
_, err := b.GetAssetStatus("")
_, err := b.GetAssetStatus(context.Background(), "")
if !errors.Is(err, errSymbolIsEmpty) {
t.Fatalf("received: %v but expected: %v", err, errSymbolIsEmpty)
}
_, err = b.GetAssetStatus("sol")
_, err = b.GetAssetStatus(context.Background(), "sol")
if !errors.Is(err, nil) {
t.Fatalf("received: %v but expected: %v", err, nil)
}