mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-08 15:11:07 +00:00
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:
@@ -1,6 +1,7 @@
|
||||
package gateio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
@@ -42,7 +43,7 @@ func (g *Gateio) GetDefaultConfig() (*config.ExchangeConfig, error) {
|
||||
}
|
||||
|
||||
if g.Features.Supports.RESTCapabilities.AutoPairUpdates {
|
||||
err = g.UpdateTradablePairs(true)
|
||||
err = g.UpdateTradablePairs(context.TODO(), true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -205,21 +206,21 @@ func (g *Gateio) Run() {
|
||||
return
|
||||
}
|
||||
|
||||
err := g.UpdateTradablePairs(false)
|
||||
err := g.UpdateTradablePairs(context.TODO(), false)
|
||||
if err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s failed to update tradable pairs. Err: %s", g.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// FetchTradablePairs returns a list of the exchanges tradable pairs
|
||||
func (g *Gateio) FetchTradablePairs(asset asset.Item) ([]string, error) {
|
||||
return g.GetSymbols()
|
||||
func (g *Gateio) FetchTradablePairs(ctx context.Context, asset asset.Item) ([]string, error) {
|
||||
return g.GetSymbols(ctx)
|
||||
}
|
||||
|
||||
// UpdateTradablePairs updates the exchanges available pairs and stores
|
||||
// them in the exchanges config
|
||||
func (g *Gateio) UpdateTradablePairs(forceUpdate bool) error {
|
||||
pairs, err := g.FetchTradablePairs(asset.Spot)
|
||||
func (g *Gateio) UpdateTradablePairs(ctx context.Context, forceUpdate bool) error {
|
||||
pairs, err := g.FetchTradablePairs(ctx, asset.Spot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -232,8 +233,8 @@ func (g *Gateio) UpdateTradablePairs(forceUpdate bool) error {
|
||||
}
|
||||
|
||||
// UpdateTickers updates the ticker for all currency pairs of a given asset type
|
||||
func (g *Gateio) UpdateTickers(a asset.Item) error {
|
||||
result, err := g.GetTickers()
|
||||
func (g *Gateio) UpdateTickers(ctx context.Context, a asset.Item) error {
|
||||
result, err := g.GetTickers(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -268,8 +269,8 @@ func (g *Gateio) UpdateTickers(a asset.Item) error {
|
||||
}
|
||||
|
||||
// UpdateTicker updates and returns the ticker for a currency pair
|
||||
func (g *Gateio) UpdateTicker(p currency.Pair, a asset.Item) (*ticker.Price, error) {
|
||||
err := g.UpdateTickers(a)
|
||||
func (g *Gateio) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) (*ticker.Price, error) {
|
||||
err := g.UpdateTickers(ctx, a)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -277,25 +278,25 @@ func (g *Gateio) UpdateTicker(p currency.Pair, a asset.Item) (*ticker.Price, err
|
||||
}
|
||||
|
||||
// FetchTicker returns the ticker for a currency pair
|
||||
func (g *Gateio) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
|
||||
func (g *Gateio) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
|
||||
tickerNew, err := ticker.GetTicker(g.Name, p, assetType)
|
||||
if err != nil {
|
||||
return g.UpdateTicker(p, assetType)
|
||||
return g.UpdateTicker(ctx, p, assetType)
|
||||
}
|
||||
return tickerNew, nil
|
||||
}
|
||||
|
||||
// FetchOrderbook returns orderbook base on the currency pair
|
||||
func (g *Gateio) FetchOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
func (g *Gateio) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
ob, err := orderbook.Get(g.Name, p, assetType)
|
||||
if err != nil {
|
||||
return g.UpdateOrderbook(p, assetType)
|
||||
return g.UpdateOrderbook(ctx, p, assetType)
|
||||
}
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
||||
func (g *Gateio) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
func (g *Gateio) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
book := &orderbook.Base{
|
||||
Exchange: g.Name,
|
||||
Pair: p,
|
||||
@@ -307,7 +308,7 @@ func (g *Gateio) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderb
|
||||
return book, err
|
||||
}
|
||||
|
||||
orderbookNew, err := g.GetOrderbook(curr.String())
|
||||
orderbookNew, err := g.GetOrderbook(ctx, curr.String())
|
||||
if err != nil {
|
||||
return book, err
|
||||
}
|
||||
@@ -334,7 +335,7 @@ func (g *Gateio) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderb
|
||||
|
||||
// UpdateAccountInfo retrieves balances for all enabled currencies for the
|
||||
// ZB exchange
|
||||
func (g *Gateio) UpdateAccountInfo(assetType asset.Item) (account.Holdings, error) {
|
||||
func (g *Gateio) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
||||
var info account.Holdings
|
||||
var balances []account.Balance
|
||||
|
||||
@@ -355,7 +356,7 @@ func (g *Gateio) UpdateAccountInfo(assetType asset.Item) (account.Holdings, erro
|
||||
Currencies: currData,
|
||||
})
|
||||
} else {
|
||||
balance, err := g.GetBalances()
|
||||
balance, err := g.GetBalances(ctx)
|
||||
if err != nil {
|
||||
return info, err
|
||||
}
|
||||
@@ -419,10 +420,10 @@ func (g *Gateio) UpdateAccountInfo(assetType asset.Item) (account.Holdings, erro
|
||||
}
|
||||
|
||||
// FetchAccountInfo retrieves balances for all enabled currencies
|
||||
func (g *Gateio) FetchAccountInfo(assetType asset.Item) (account.Holdings, error) {
|
||||
func (g *Gateio) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
||||
acc, err := account.GetHoldings(g.Name, assetType)
|
||||
if err != nil {
|
||||
return g.UpdateAccountInfo(assetType)
|
||||
return g.UpdateAccountInfo(ctx, assetType)
|
||||
}
|
||||
|
||||
return acc, nil
|
||||
@@ -430,24 +431,24 @@ func (g *Gateio) FetchAccountInfo(assetType asset.Item) (account.Holdings, error
|
||||
|
||||
// GetFundingHistory returns funding history, deposits and
|
||||
// withdrawals
|
||||
func (g *Gateio) GetFundingHistory() ([]exchange.FundHistory, error) {
|
||||
func (g *Gateio) GetFundingHistory(ctx context.Context) ([]exchange.FundHistory, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// GetWithdrawalsHistory returns previous withdrawals data
|
||||
func (g *Gateio) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
|
||||
func (g *Gateio) GetWithdrawalsHistory(ctx context.Context, c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
|
||||
return nil, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
// GetRecentTrades returns the most recent trades for a currency and asset
|
||||
func (g *Gateio) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
|
||||
func (g *Gateio) GetRecentTrades(ctx context.Context, p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
|
||||
var err error
|
||||
p, err = g.FormatExchangeCurrency(p, assetType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tradeData TradeHistory
|
||||
tradeData, err = g.GetTrades(p.String())
|
||||
tradeData, err = g.GetTrades(ctx, p.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -480,13 +481,13 @@ func (g *Gateio) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade
|
||||
}
|
||||
|
||||
// GetHistoricTrades returns historic trade data within the timeframe provided
|
||||
func (g *Gateio) GetHistoricTrades(_ currency.Pair, _ asset.Item, _, _ time.Time) ([]trade.Data, error) {
|
||||
func (g *Gateio) GetHistoricTrades(_ context.Context, _ currency.Pair, _ asset.Item, _, _ time.Time) ([]trade.Data, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// SubmitOrder submits a new order
|
||||
// TODO: support multiple order types (IOC)
|
||||
func (g *Gateio) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
|
||||
func (g *Gateio) SubmitOrder(ctx context.Context, s *order.Submit) (order.SubmitResponse, error) {
|
||||
var submitOrderResponse order.SubmitResponse
|
||||
if err := s.Validate(); err != nil {
|
||||
return submitOrderResponse, err
|
||||
@@ -511,7 +512,7 @@ func (g *Gateio) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
|
||||
Type: orderTypeFormat,
|
||||
}
|
||||
|
||||
response, err := g.SpotNewOrder(spotNewOrderRequestParams)
|
||||
response, err := g.SpotNewOrder(ctx, spotNewOrderRequestParams)
|
||||
if err != nil {
|
||||
return submitOrderResponse, err
|
||||
}
|
||||
@@ -528,12 +529,12 @@ func (g *Gateio) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
|
||||
|
||||
// ModifyOrder will allow of changing orderbook placement and limit to
|
||||
// market conversion
|
||||
func (g *Gateio) ModifyOrder(action *order.Modify) (order.Modify, error) {
|
||||
func (g *Gateio) ModifyOrder(ctx context.Context, action *order.Modify) (order.Modify, error) {
|
||||
return order.Modify{}, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// CancelOrder cancels an order by its corresponding ID number
|
||||
func (g *Gateio) CancelOrder(o *order.Cancel) error {
|
||||
func (g *Gateio) CancelOrder(ctx context.Context, o *order.Cancel) error {
|
||||
if err := o.Validate(o.StandardCancel()); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -548,21 +549,21 @@ func (g *Gateio) CancelOrder(o *order.Cancel) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = g.CancelExistingOrder(orderIDInt, fpair.String())
|
||||
_, err = g.CancelExistingOrder(ctx, orderIDInt, fpair.String())
|
||||
return err
|
||||
}
|
||||
|
||||
// CancelBatchOrders cancels an orders by their corresponding ID numbers
|
||||
func (g *Gateio) CancelBatchOrders(o []order.Cancel) (order.CancelBatchResponse, error) {
|
||||
func (g *Gateio) CancelBatchOrders(ctx context.Context, o []order.Cancel) (order.CancelBatchResponse, error) {
|
||||
return order.CancelBatchResponse{}, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
// CancelAllOrders cancels all orders associated with a currency pair
|
||||
func (g *Gateio) CancelAllOrders(_ *order.Cancel) (order.CancelAllResponse, error) {
|
||||
func (g *Gateio) CancelAllOrders(ctx context.Context, _ *order.Cancel) (order.CancelAllResponse, error) {
|
||||
cancelAllOrdersResponse := order.CancelAllResponse{
|
||||
Status: make(map[string]string),
|
||||
}
|
||||
openOrders, err := g.GetOpenOrders("")
|
||||
openOrders, err := g.GetOpenOrders(ctx, "")
|
||||
if err != nil {
|
||||
return cancelAllOrdersResponse, err
|
||||
}
|
||||
@@ -573,7 +574,7 @@ func (g *Gateio) CancelAllOrders(_ *order.Cancel) (order.CancelAllResponse, erro
|
||||
}
|
||||
|
||||
for unique := range uniqueSymbols {
|
||||
err = g.CancelAllExistingOrders(-1, unique)
|
||||
err = g.CancelAllExistingOrders(ctx, -1, unique)
|
||||
if err != nil {
|
||||
cancelAllOrdersResponse.Status[unique] = err.Error()
|
||||
}
|
||||
@@ -583,9 +584,9 @@ func (g *Gateio) CancelAllOrders(_ *order.Cancel) (order.CancelAllResponse, erro
|
||||
}
|
||||
|
||||
// GetOrderInfo returns order information based on order ID
|
||||
func (g *Gateio) GetOrderInfo(orderID string, pair currency.Pair, assetType asset.Item) (order.Detail, error) {
|
||||
func (g *Gateio) GetOrderInfo(ctx context.Context, orderID string, pair currency.Pair, assetType asset.Item) (order.Detail, error) {
|
||||
var orderDetail order.Detail
|
||||
orders, err := g.GetOpenOrders("")
|
||||
orders, err := g.GetOpenOrders(ctx, "")
|
||||
if err != nil {
|
||||
return orderDetail, errors.New("failed to get open orders")
|
||||
}
|
||||
@@ -627,8 +628,8 @@ func (g *Gateio) GetOrderInfo(orderID string, pair currency.Pair, assetType asse
|
||||
}
|
||||
|
||||
// GetDepositAddress returns a deposit address for a specified currency
|
||||
func (g *Gateio) GetDepositAddress(cryptocurrency currency.Code, _ string) (string, error) {
|
||||
addr, err := g.GetCryptoDepositAddress(cryptocurrency.String())
|
||||
func (g *Gateio) GetDepositAddress(ctx context.Context, cryptocurrency currency.Code, _ string) (string, error) {
|
||||
addr, err := g.GetCryptoDepositAddress(ctx, cryptocurrency.String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -642,38 +643,39 @@ func (g *Gateio) GetDepositAddress(cryptocurrency currency.Code, _ string) (stri
|
||||
|
||||
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
|
||||
// submitted
|
||||
func (g *Gateio) WithdrawCryptocurrencyFunds(withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
func (g *Gateio) WithdrawCryptocurrencyFunds(ctx context.Context, withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
if err := withdrawRequest.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return g.WithdrawCrypto(withdrawRequest.Currency.String(),
|
||||
return g.WithdrawCrypto(ctx,
|
||||
withdrawRequest.Currency.String(),
|
||||
withdrawRequest.Crypto.Address,
|
||||
withdrawRequest.Amount)
|
||||
}
|
||||
|
||||
// WithdrawFiatFunds returns a withdrawal ID when a
|
||||
// withdrawal is submitted
|
||||
func (g *Gateio) WithdrawFiatFunds(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
func (g *Gateio) WithdrawFiatFunds(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
|
||||
// withdrawal is submitted
|
||||
func (g *Gateio) WithdrawFiatFundsToInternationalBank(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
func (g *Gateio) WithdrawFiatFundsToInternationalBank(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// GetFeeByType returns an estimate of fee based on type of transaction
|
||||
func (g *Gateio) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
|
||||
func (g *Gateio) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) {
|
||||
if !g.AllowAuthenticatedRequest() && // Todo check connection status
|
||||
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
|
||||
feeBuilder.FeeType = exchange.OfflineTradeFee
|
||||
}
|
||||
return g.GetFee(feeBuilder)
|
||||
return g.GetFee(ctx, feeBuilder)
|
||||
}
|
||||
|
||||
// GetActiveOrders retrieves any orders that are active/open
|
||||
func (g *Gateio) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
func (g *Gateio) GetActiveOrders(ctx context.Context, req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -727,7 +729,7 @@ func (g *Gateio) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resp, err := g.GetOpenOrders(currPair)
|
||||
resp, err := g.GetOpenOrders(ctx, currPair)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -769,14 +771,14 @@ func (g *Gateio) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
|
||||
// GetOrderHistory retrieves account order information
|
||||
// Can Limit response to specific order status
|
||||
func (g *Gateio) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
func (g *Gateio) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var trades []TradesResponse
|
||||
for i := range req.Pairs {
|
||||
resp, err := g.GetTradeHistory(req.Pairs[i].String())
|
||||
resp, err := g.GetTradeHistory(ctx, req.Pairs[i].String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -814,14 +816,14 @@ func (g *Gateio) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
}
|
||||
|
||||
// AuthenticateWebsocket sends an authentication message to the websocket
|
||||
func (g *Gateio) AuthenticateWebsocket() error {
|
||||
func (g *Gateio) AuthenticateWebsocket(_ context.Context) error {
|
||||
return g.wsServerSignIn()
|
||||
}
|
||||
|
||||
// ValidateCredentials validates current credentials used for wrapper
|
||||
// functionality
|
||||
func (g *Gateio) ValidateCredentials(assetType asset.Item) error {
|
||||
_, err := g.UpdateAccountInfo(assetType)
|
||||
func (g *Gateio) ValidateCredentials(ctx context.Context, assetType asset.Item) error {
|
||||
_, err := g.UpdateAccountInfo(ctx, assetType)
|
||||
return g.CheckTransientError(err)
|
||||
}
|
||||
|
||||
@@ -831,7 +833,7 @@ func (g *Gateio) FormatExchangeKlineInterval(in kline.Interval) string {
|
||||
}
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (g *Gateio) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
func (g *Gateio) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
if err := g.ValidateKline(pair, a, interval); err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
@@ -848,7 +850,7 @@ func (g *Gateio) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end
|
||||
HourSize: int(hours),
|
||||
}
|
||||
|
||||
klineData, err := g.GetSpotKline(params)
|
||||
klineData, err := g.GetSpotKline(ctx, params)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
@@ -862,6 +864,6 @@ func (g *Gateio) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end
|
||||
}
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (g *Gateio) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
return g.GetHistoricCandles(pair, a, start, end, interval)
|
||||
func (g *Gateio) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
return g.GetHistoricCandles(ctx, pair, a, start, end, interval)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user