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 exchange
import (
"context"
"errors"
"sort"
"strconv"
@@ -43,18 +44,18 @@ func (e Exchange) IsEnabled(exch string) bool {
}
// Orderbook returns current orderbook requested exchange, pair and asset
func (e Exchange) Orderbook(exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
return engine.Bot.GetSpecificOrderbook(pair, exch, item)
func (e Exchange) Orderbook(ctx context.Context, exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
return engine.Bot.GetSpecificOrderbook(ctx, pair, exch, item)
}
// Ticker returns ticker for provided currency pair & asset type
func (e Exchange) Ticker(exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
func (e Exchange) Ticker(ctx context.Context, exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
ex, err := e.GetExchange(exch)
if err != nil {
return nil, err
}
return ex.FetchTicker(pair, item)
return ex.FetchTicker(ctx, pair, item)
}
// Pairs returns either all or enabled currency pairs
@@ -76,8 +77,8 @@ func (e Exchange) Pairs(exch string, enabledOnly bool, item asset.Item) (*curren
}
// QueryOrder returns details of a valid exchange order
func (e Exchange) QueryOrder(exch, orderID string, pair currency.Pair, assetType asset.Item) (*order.Detail, error) {
o, err := engine.Bot.OrderManager.GetOrderInfo(exch, orderID, pair, assetType)
func (e Exchange) QueryOrder(ctx context.Context, exch, orderID string, pair currency.Pair, assetType asset.Item) (*order.Detail, error) {
o, err := engine.Bot.OrderManager.GetOrderInfo(ctx, exch, orderID, pair, assetType)
if err != nil {
return nil, err
}
@@ -86,8 +87,8 @@ func (e Exchange) QueryOrder(exch, orderID string, pair currency.Pair, assetType
}
// SubmitOrder submit new order on exchange
func (e Exchange) SubmitOrder(submit *order.Submit) (*order.SubmitResponse, error) {
r, err := engine.Bot.OrderManager.Submit(submit)
func (e Exchange) SubmitOrder(ctx context.Context, submit *order.Submit) (*order.SubmitResponse, error) {
r, err := engine.Bot.OrderManager.Submit(ctx, submit)
if err != nil {
return nil, err
}
@@ -96,8 +97,8 @@ func (e Exchange) SubmitOrder(submit *order.Submit) (*order.SubmitResponse, erro
}
// CancelOrder wrapper to cancel order on exchange
func (e Exchange) CancelOrder(exch, orderID string, cp currency.Pair, a asset.Item) (bool, error) {
orderDetails, err := e.QueryOrder(exch, orderID, cp, a)
func (e Exchange) CancelOrder(ctx context.Context, exch, orderID string, cp currency.Pair, a asset.Item) (bool, error) {
orderDetails, err := e.QueryOrder(ctx, exch, orderID, cp, a)
if err != nil {
return false, err
}
@@ -111,7 +112,7 @@ func (e Exchange) CancelOrder(exch, orderID string, cp currency.Pair, a asset.It
Exchange: exch,
}
err = engine.Bot.OrderManager.Cancel(cancel)
err = engine.Bot.OrderManager.Cancel(ctx, cancel)
if err != nil {
return false, err
}
@@ -119,13 +120,13 @@ func (e Exchange) CancelOrder(exch, orderID string, cp currency.Pair, a asset.It
}
// AccountInformation returns account information (balance etc) for requested exchange
func (e Exchange) AccountInformation(exch string, assetType asset.Item) (account.Holdings, error) {
func (e Exchange) AccountInformation(ctx context.Context, exch string, assetType asset.Item) (account.Holdings, error) {
ex, err := e.GetExchange(exch)
if err != nil {
return account.Holdings{}, err
}
accountInfo, err := ex.FetchAccountInfo(assetType)
accountInfo, err := ex.FetchAccountInfo(ctx, assetType)
if err != nil {
return account.Holdings{}, err
}
@@ -136,14 +137,13 @@ func (e Exchange) AccountInformation(exch string, assetType asset.Item) (account
// DepositAddress gets the address required to deposit funds for currency type
func (e Exchange) DepositAddress(exch string, currencyCode currency.Code) (out string, err error) {
if currencyCode.IsEmpty() {
err = errors.New("currency code is empty")
return
return "", errors.New("currency code is empty")
}
return engine.Bot.DepositAddressManager.GetDepositAddressByExchangeAndCurrency(exch, currencyCode)
}
// WithdrawalFiatFunds withdraw funds from exchange to requested fiat source
func (e Exchange) WithdrawalFiatFunds(bankAccountID string, request *withdraw.Request) (string, error) {
func (e Exchange) WithdrawalFiatFunds(ctx context.Context, bankAccountID string, request *withdraw.Request) (string, error) {
ex, err := e.GetExchange(request.Exchange)
if err != nil {
return "", err
@@ -176,7 +176,7 @@ func (e Exchange) WithdrawalFiatFunds(bankAccountID string, request *withdraw.Re
request.Fiat.Bank.SWIFTCode = v.SWIFTCode
request.Fiat.Bank.IBAN = v.IBAN
resp, err := engine.Bot.WithdrawManager.SubmitWithdrawal(request)
resp, err := engine.Bot.WithdrawManager.SubmitWithdrawal(ctx, request)
if err != nil {
return "", err
}
@@ -184,7 +184,7 @@ func (e Exchange) WithdrawalFiatFunds(bankAccountID string, request *withdraw.Re
}
// WithdrawalCryptoFunds withdraw funds from exchange to requested Crypto source
func (e Exchange) WithdrawalCryptoFunds(request *withdraw.Request) (string, error) {
func (e Exchange) WithdrawalCryptoFunds(ctx context.Context, request *withdraw.Request) (string, error) {
// Checks if exchange is enabled or not so we don't call OTP generation
_, err := e.GetExchange(request.Exchange)
if err != nil {
@@ -199,7 +199,7 @@ func (e Exchange) WithdrawalCryptoFunds(request *withdraw.Request) (string, erro
request.OneTimePassword = v
}
resp, err := engine.Bot.WithdrawManager.SubmitWithdrawal(request)
resp, err := engine.Bot.WithdrawManager.SubmitWithdrawal(ctx, request)
if err != nil {
return "", err
}
@@ -207,12 +207,12 @@ func (e Exchange) WithdrawalCryptoFunds(request *withdraw.Request) (string, erro
}
// OHLCV returns open high low close volume candles for requested exchange/pair/asset/start & end time
func (e Exchange) OHLCV(exch string, pair currency.Pair, item asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
func (e Exchange) OHLCV(ctx context.Context, exch string, pair currency.Pair, item asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
ex, err := e.GetExchange(exch)
if err != nil {
return kline.Item{}, err
}
ret, err := ex.GetHistoricCandlesExtended(pair, item, start, end, interval)
ret, err := ex.GetHistoricCandlesExtended(ctx, pair, item, start, end, interval)
if err != nil {
return kline.Item{}, err
}

View File

@@ -1,6 +1,7 @@
package exchange
import (
"context"
"fmt"
"os"
"path/filepath"
@@ -95,7 +96,7 @@ func TestExchange_Ticker(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = exchangeTest.Ticker(exchName, c, assetType)
_, err = exchangeTest.Ticker(context.Background(), exchName, c, assetType)
if err != nil {
t.Fatal(err)
}
@@ -107,7 +108,7 @@ func TestExchange_Orderbook(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = exchangeTest.Orderbook(exchName, c, assetType)
_, err = exchangeTest.Orderbook(context.Background(), exchName, c, assetType)
if err != nil {
t.Fatal(err)
}
@@ -129,7 +130,8 @@ func TestExchange_AccountInformation(t *testing.T) {
if !configureExchangeKeys() {
t.Skip("no exchange configured test skipped")
}
_, err := exchangeTest.AccountInformation(exchName, asset.Spot)
_, err := exchangeTest.AccountInformation(context.Background(),
exchName, asset.Spot)
if err != nil {
t.Fatal(err)
}
@@ -140,7 +142,8 @@ func TestExchange_QueryOrder(t *testing.T) {
t.Skip("no exchange configured test skipped")
}
t.Parallel()
_, err := exchangeTest.QueryOrder(exchName, orderID, currency.Pair{}, assetType)
_, err := exchangeTest.QueryOrder(context.Background(),
exchName, orderID, currency.Pair{}, assetType)
if err != nil {
t.Fatal(err)
}
@@ -168,7 +171,7 @@ func TestExchange_SubmitOrder(t *testing.T) {
Exchange: exchName,
AssetType: asset.Spot,
}
_, err = exchangeTest.SubmitOrder(tempOrder)
_, err = exchangeTest.SubmitOrder(context.Background(), tempOrder)
if err != nil {
t.Fatal(err)
}
@@ -181,7 +184,8 @@ func TestExchange_CancelOrder(t *testing.T) {
t.Parallel()
cp := currency.NewPair(currency.BTC, currency.USD)
a := asset.Spot
_, err := exchangeTest.CancelOrder(exchName, orderID, cp, a)
_, err := exchangeTest.CancelOrder(context.Background(),
exchName, orderID, cp, a)
if err != nil {
t.Fatal(err)
}
@@ -191,7 +195,7 @@ func TestOHLCV(t *testing.T) {
t.Parallel()
cp := currency.NewPair(currency.BTC, currency.AUD)
cp.Delimiter = currency.DashDelimiter
calvinKline, err := exchangeTest.OHLCV(exchName, cp, assetType, time.Now().Add(-time.Hour*24).UTC(), time.Now().UTC(), kline.OneHour)
calvinKline, err := exchangeTest.OHLCV(context.Background(), exchName, cp, assetType, time.Now().Add(-time.Hour*24).UTC(), time.Now().UTC(), kline.OneHour)
if err != nil {
t.Error(err)
}

View File

@@ -1,6 +1,7 @@
package validator
import (
"context"
"math/rand"
"time"
@@ -43,7 +44,7 @@ func (w Wrapper) IsEnabled(exch string) (v bool) {
}
// Orderbook validator for test execution/scripts
func (w Wrapper) Orderbook(exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
func (w Wrapper) Orderbook(ctx context.Context, exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
if exch == exchError.String() {
return nil, errTestFailed
}
@@ -68,7 +69,7 @@ func (w Wrapper) Orderbook(exch string, pair currency.Pair, item asset.Item) (*o
}
// Ticker validator for test execution/scripts
func (w Wrapper) Ticker(exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
func (w Wrapper) Ticker(ctx context.Context, exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
if exch == exchError.String() {
return nil, errTestFailed
}
@@ -106,7 +107,7 @@ func (w Wrapper) Pairs(exch string, _ bool, _ asset.Item) (*currency.Pairs, erro
}
// QueryOrder validator for test execution/scripts
func (w Wrapper) QueryOrder(exch, _ string, _ currency.Pair, _ asset.Item) (*order.Detail, error) {
func (w Wrapper) QueryOrder(ctx context.Context, exch, _ string, _ currency.Pair, _ asset.Item) (*order.Detail, error) {
if exch == exchError.String() {
return nil, errTestFailed
}
@@ -146,7 +147,7 @@ func (w Wrapper) QueryOrder(exch, _ string, _ currency.Pair, _ asset.Item) (*ord
}
// SubmitOrder validator for test execution/scripts
func (w Wrapper) SubmitOrder(o *order.Submit) (*order.SubmitResponse, error) {
func (w Wrapper) SubmitOrder(ctx context.Context, o *order.Submit) (*order.SubmitResponse, error) {
if o == nil {
return nil, errTestFailed
}
@@ -167,7 +168,7 @@ func (w Wrapper) SubmitOrder(o *order.Submit) (*order.SubmitResponse, error) {
}
// CancelOrder validator for test execution/scripts
func (w Wrapper) CancelOrder(exch, orderid string, cp currency.Pair, a asset.Item) (bool, error) {
func (w Wrapper) CancelOrder(ctx context.Context, exch, orderid string, cp currency.Pair, a asset.Item) (bool, error) {
if exch == exchError.String() {
return false, errTestFailed
}
@@ -184,7 +185,7 @@ func (w Wrapper) CancelOrder(exch, orderid string, cp currency.Pair, a asset.Ite
}
// AccountInformation validator for test execution/scripts
func (w Wrapper) AccountInformation(exch string, assetType asset.Item) (account.Holdings, error) {
func (w Wrapper) AccountInformation(ctx context.Context, exch string, assetType asset.Item) (account.Holdings, error) {
if exch == exchError.String() {
return account.Holdings{}, errTestFailed
}
@@ -224,7 +225,7 @@ func (w Wrapper) DepositAddress(exch string, _ currency.Code) (string, error) {
}
// WithdrawalCryptoFunds validator for test execution/scripts
func (w Wrapper) WithdrawalCryptoFunds(r *withdraw.Request) (out string, err error) {
func (w Wrapper) WithdrawalCryptoFunds(ctx context.Context, r *withdraw.Request) (out string, err error) {
if r.Exchange == exchError.String() {
return r.Exchange, errTestFailed
}
@@ -233,7 +234,7 @@ func (w Wrapper) WithdrawalCryptoFunds(r *withdraw.Request) (out string, err err
}
// WithdrawalFiatFunds validator for test execution/scripts
func (w Wrapper) WithdrawalFiatFunds(_ string, r *withdraw.Request) (out string, err error) {
func (w Wrapper) WithdrawalFiatFunds(ctx context.Context, _ string, r *withdraw.Request) (out string, err error) {
if r.Exchange == exchError.String() {
return r.Exchange, errTestFailed
}
@@ -242,7 +243,7 @@ func (w Wrapper) WithdrawalFiatFunds(_ string, r *withdraw.Request) (out string,
}
// OHLCV returns open high low close volume candles for requested exchange/pair/asset/start & end time
func (w Wrapper) OHLCV(exch string, p currency.Pair, a asset.Item, start, end time.Time, i kline.Interval) (kline.Item, error) {
func (w Wrapper) OHLCV(ctx context.Context, exch string, p currency.Pair, a asset.Item, start, end time.Time, i kline.Interval) (kline.Item, error) {
if exch == exchError.String() {
return kline.Item{}, errTestFailed
}

View File

@@ -1,6 +1,7 @@
package validator
import (
"context"
"testing"
"time"
@@ -63,12 +64,14 @@ func TestWrapper_IsEnabled(t *testing.T) {
func TestWrapper_AccountInformation(t *testing.T) {
t.Parallel()
_, err := testWrapper.AccountInformation(exchName, asset.Spot)
_, err := testWrapper.AccountInformation(context.Background(),
exchName, asset.Spot)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.AccountInformation(exchError.String(), asset.Spot)
_, err = testWrapper.AccountInformation(context.Background(),
exchError.String(), asset.Spot)
if err == nil {
t.Fatal("expected AccountInformation to return error on invalid name")
}
@@ -77,27 +80,32 @@ func TestWrapper_AccountInformation(t *testing.T) {
func TestWrapper_CancelOrder(t *testing.T) {
t.Parallel()
cp := currency.NewPair(currency.BTC, currency.USD)
_, err := testWrapper.CancelOrder(exchName, orderID, cp, assetType)
_, err := testWrapper.CancelOrder(context.Background(),
exchName, orderID, cp, assetType)
if err != nil {
t.Error(err)
}
_, err = testWrapper.CancelOrder(exchError.String(), orderID, cp, assetType)
_, err = testWrapper.CancelOrder(context.Background(),
exchError.String(), orderID, cp, assetType)
if err == nil {
t.Error("expected CancelOrder to return error on invalid name")
}
_, err = testWrapper.CancelOrder(exchName, "", cp, assetType)
_, err = testWrapper.CancelOrder(context.Background(),
exchName, "", cp, assetType)
if err == nil {
t.Error("expected CancelOrder to return error on invalid name")
}
_, err = testWrapper.CancelOrder(exchName, orderID, currency.Pair{}, assetType)
_, err = testWrapper.CancelOrder(context.Background(),
exchName, orderID, currency.Pair{}, assetType)
if err != nil {
t.Error(err)
}
_, err = testWrapper.CancelOrder(exchName, orderID, cp, "")
_, err = testWrapper.CancelOrder(context.Background(),
exchName, orderID, cp, "")
if err != nil {
t.Error(err)
}
@@ -121,12 +129,14 @@ func TestWrapper_Orderbook(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.Orderbook(exchName, c, assetType)
_, err = testWrapper.Orderbook(context.Background(),
exchName, c, assetType)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.Orderbook(exchError.String(), currencyPair, asset.Spot)
_, err = testWrapper.Orderbook(context.Background(),
exchError.String(), currencyPair, asset.Spot)
if err == nil {
t.Fatal("expected Orderbook to return error with invalid name")
}
@@ -152,12 +162,14 @@ func TestWrapper_Pairs(t *testing.T) {
func TestWrapper_QueryOrder(t *testing.T) {
t.Parallel()
_, err := testWrapper.QueryOrder(exchName, orderID, currency.Pair{}, assetType)
_, err := testWrapper.QueryOrder(context.Background(),
exchName, orderID, currency.Pair{}, assetType)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.QueryOrder(exchError.String(), "", currency.Pair{}, assetType)
_, err = testWrapper.QueryOrder(context.Background(),
exchError.String(), "", currency.Pair{}, assetType)
if err == nil {
t.Fatal("expected QueryOrder to return error on invalid name")
}
@@ -181,12 +193,12 @@ func TestWrapper_SubmitOrder(t *testing.T) {
Exchange: "true",
AssetType: asset.Spot,
}
_, err = testWrapper.SubmitOrder(tempOrder)
_, err = testWrapper.SubmitOrder(context.Background(), tempOrder)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.SubmitOrder(nil)
_, err = testWrapper.SubmitOrder(context.Background(), nil)
if err == nil {
t.Fatal("expected SubmitOrder to return error with invalid name")
}
@@ -198,36 +210,40 @@ func TestWrapper_Ticker(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.Ticker(exchName, c, assetType)
_, err = testWrapper.Ticker(context.Background(), exchName, c, assetType)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.Ticker(exchError.String(), currencyPair, asset.Spot)
_, err = testWrapper.Ticker(context.Background(), exchError.String(), currencyPair, asset.Spot)
if err == nil {
t.Fatal("expected Ticker to return error with invalid name")
}
}
func TestWrapper_WithdrawalCryptoFunds(t *testing.T) {
_, err := testWrapper.WithdrawalCryptoFunds(&withdraw.Request{Exchange: exchError.String()})
_, err := testWrapper.WithdrawalCryptoFunds(context.Background(),
&withdraw.Request{Exchange: exchError.String()})
if err == nil {
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
}
_, err = testWrapper.WithdrawalCryptoFunds(&withdraw.Request{Exchange: exchName})
_, err = testWrapper.WithdrawalCryptoFunds(context.Background(),
&withdraw.Request{Exchange: exchName})
if err != nil {
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
}
}
func TestWrapper_WithdrawalFiatFunds(t *testing.T) {
_, err := testWrapper.WithdrawalFiatFunds("", &withdraw.Request{Exchange: exchError.String()})
_, err := testWrapper.WithdrawalFiatFunds(context.Background(),
"", &withdraw.Request{Exchange: exchError.String()})
if err == nil {
t.Fatal("expected WithdrawalFiatFunds to return error with invalid name")
}
_, err = testWrapper.WithdrawalFiatFunds("", &withdraw.Request{Exchange: exchName})
_, err = testWrapper.WithdrawalFiatFunds(context.Background(),
"", &withdraw.Request{Exchange: exchName})
if err != nil {
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
}
@@ -238,11 +254,15 @@ func TestWrapper_OHLCV(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.OHLCV("test", c, asset.Spot, time.Now().Add(-24*time.Hour), time.Now(), kline.OneDay)
_, err = testWrapper.OHLCV(context.Background(),
"test", c, asset.Spot, time.Now().Add(-24*time.Hour), time.Now(), kline.OneDay)
if err != nil {
t.Fatal(err)
}
_, err = testWrapper.OHLCV(exchError.String(), c, asset.Spot, time.Now().Add(-24*time.Hour), time.Now(), kline.OneDay)
_, err = testWrapper.OHLCV(context.Background(),
exchError.String(), c, asset.Spot,
time.Now().Add(-24*time.Hour),
time.Now(), kline.OneDay)
if err == nil {
t.Fatal("expected OHLCV to return error with invalid name")
}