mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-06 07:26:47 +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 gemini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -42,7 +43,7 @@ func (g *Gemini) 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
|
||||
}
|
||||
@@ -256,7 +257,7 @@ func (g *Gemini) Run() {
|
||||
if !g.GetEnabledFeatures().AutoPairUpdates && !forceUpdate {
|
||||
return
|
||||
}
|
||||
err = g.UpdateTradablePairs(forceUpdate)
|
||||
err = g.UpdateTradablePairs(context.TODO(), forceUpdate)
|
||||
if err != nil {
|
||||
log.Errorf(log.ExchangeSys,
|
||||
"%s failed to update tradable pairs. Err: %s",
|
||||
@@ -266,8 +267,8 @@ func (g *Gemini) Run() {
|
||||
}
|
||||
|
||||
// FetchTradablePairs returns a list of the exchanges tradable pairs
|
||||
func (g *Gemini) FetchTradablePairs(asset asset.Item) ([]string, error) {
|
||||
pairs, err := g.GetSymbols()
|
||||
func (g *Gemini) FetchTradablePairs(ctx context.Context, asset asset.Item) ([]string, error) {
|
||||
pairs, err := g.GetSymbols(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -288,8 +289,8 @@ func (g *Gemini) FetchTradablePairs(asset asset.Item) ([]string, error) {
|
||||
|
||||
// UpdateTradablePairs updates the exchanges available pairs and stores
|
||||
// them in the exchanges config
|
||||
func (g *Gemini) UpdateTradablePairs(forceUpdate bool) error {
|
||||
pairs, err := g.FetchTradablePairs(asset.Spot)
|
||||
func (g *Gemini) UpdateTradablePairs(ctx context.Context, forceUpdate bool) error {
|
||||
pairs, err := g.FetchTradablePairs(ctx, asset.Spot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -304,10 +305,10 @@ func (g *Gemini) UpdateTradablePairs(forceUpdate bool) error {
|
||||
|
||||
// UpdateAccountInfo Retrieves balances for all enabled currencies for the
|
||||
// Gemini exchange
|
||||
func (g *Gemini) UpdateAccountInfo(assetType asset.Item) (account.Holdings, error) {
|
||||
func (g *Gemini) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
||||
var response account.Holdings
|
||||
response.Exchange = g.Name
|
||||
accountBalance, err := g.GetBalances()
|
||||
accountBalance, err := g.GetBalances(ctx)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
@@ -334,28 +335,28 @@ func (g *Gemini) UpdateAccountInfo(assetType asset.Item) (account.Holdings, erro
|
||||
}
|
||||
|
||||
// FetchAccountInfo retrieves balances for all enabled currencies
|
||||
func (g *Gemini) FetchAccountInfo(assetType asset.Item) (account.Holdings, error) {
|
||||
func (g *Gemini) 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
|
||||
}
|
||||
|
||||
// UpdateTickers updates the ticker for all currency pairs of a given asset type
|
||||
func (g *Gemini) UpdateTickers(a asset.Item) error {
|
||||
func (g *Gemini) UpdateTickers(ctx context.Context, a asset.Item) error {
|
||||
return common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// UpdateTicker updates and returns the ticker for a currency pair
|
||||
func (g *Gemini) UpdateTicker(p currency.Pair, a asset.Item) (*ticker.Price, error) {
|
||||
func (g *Gemini) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) (*ticker.Price, error) {
|
||||
fPair, err := g.FormatExchangeCurrency(p, a)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tick, err := g.GetTicker(fPair.String())
|
||||
tick, err := g.GetTicker(ctx, fPair.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -378,7 +379,7 @@ func (g *Gemini) UpdateTicker(p currency.Pair, a asset.Item) (*ticker.Price, err
|
||||
}
|
||||
|
||||
// FetchTicker returns the ticker for a currency pair
|
||||
func (g *Gemini) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
|
||||
func (g *Gemini) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
|
||||
fPair, err := g.FormatExchangeCurrency(p, assetType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -386,13 +387,13 @@ func (g *Gemini) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Pri
|
||||
|
||||
tickerNew, err := ticker.GetTicker(g.Name, fPair, assetType)
|
||||
if err != nil {
|
||||
return g.UpdateTicker(fPair, assetType)
|
||||
return g.UpdateTicker(ctx, fPair, assetType)
|
||||
}
|
||||
return tickerNew, nil
|
||||
}
|
||||
|
||||
// FetchOrderbook returns orderbook base on the currency pair
|
||||
func (g *Gemini) FetchOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
func (g *Gemini) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
fPair, err := g.FormatExchangeCurrency(p, assetType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -400,13 +401,13 @@ func (g *Gemini) FetchOrderbook(p currency.Pair, assetType asset.Item) (*orderbo
|
||||
|
||||
ob, err := orderbook.Get(g.Name, fPair, assetType)
|
||||
if err != nil {
|
||||
return g.UpdateOrderbook(fPair, assetType)
|
||||
return g.UpdateOrderbook(ctx, fPair, assetType)
|
||||
}
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
||||
func (g *Gemini) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
func (g *Gemini) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
book := &orderbook.Base{
|
||||
Exchange: g.Name,
|
||||
Pair: p,
|
||||
@@ -418,7 +419,7 @@ func (g *Gemini) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderb
|
||||
return book, err
|
||||
}
|
||||
|
||||
orderbookNew, err := g.GetOrderbook(fPair.String(), url.Values{})
|
||||
orderbookNew, err := g.GetOrderbook(ctx, fPair.String(), url.Values{})
|
||||
if err != nil {
|
||||
return book, err
|
||||
}
|
||||
@@ -443,22 +444,22 @@ func (g *Gemini) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderb
|
||||
|
||||
// GetFundingHistory returns funding history, deposits and
|
||||
// withdrawals
|
||||
func (g *Gemini) GetFundingHistory() ([]exchange.FundHistory, error) {
|
||||
func (g *Gemini) GetFundingHistory(ctx context.Context) ([]exchange.FundHistory, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// GetWithdrawalsHistory returns previous withdrawals data
|
||||
func (g *Gemini) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
|
||||
func (g *Gemini) 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 *Gemini) GetRecentTrades(currencyPair currency.Pair, assetType asset.Item) ([]trade.Data, error) {
|
||||
return g.GetHistoricTrades(currencyPair, assetType, time.Time{}, time.Time{})
|
||||
func (g *Gemini) GetRecentTrades(ctx context.Context, pair currency.Pair, assetType asset.Item) ([]trade.Data, error) {
|
||||
return g.GetHistoricTrades(ctx, pair, assetType, time.Time{}, time.Time{})
|
||||
}
|
||||
|
||||
// GetHistoricTrades returns historic trade data within the timeframe provided
|
||||
func (g *Gemini) GetHistoricTrades(p currency.Pair, assetType asset.Item, timestampStart, timestampEnd time.Time) ([]trade.Data, error) {
|
||||
func (g *Gemini) GetHistoricTrades(ctx context.Context, p currency.Pair, assetType asset.Item, timestampStart, timestampEnd time.Time) ([]trade.Data, error) {
|
||||
if err := common.StartEndTimeCheck(timestampStart, timestampEnd); err != nil && !errors.Is(err, common.ErrDateUnset) {
|
||||
return nil, fmt.Errorf("invalid time range supplied. Start: %v End %v %w", timestampStart, timestampEnd, err)
|
||||
}
|
||||
@@ -473,7 +474,11 @@ func (g *Gemini) GetHistoricTrades(p currency.Pair, assetType asset.Item, timest
|
||||
allTrades:
|
||||
for {
|
||||
var tradeData []Trade
|
||||
tradeData, err = g.GetTrades(p.String(), ts.Unix(), int64(limit), false)
|
||||
tradeData, err = g.GetTrades(ctx,
|
||||
p.String(),
|
||||
ts.Unix(),
|
||||
int64(limit),
|
||||
false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -521,7 +526,7 @@ allTrades:
|
||||
}
|
||||
|
||||
// SubmitOrder submits a new order
|
||||
func (g *Gemini) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
|
||||
func (g *Gemini) SubmitOrder(ctx context.Context, s *order.Submit) (order.SubmitResponse, error) {
|
||||
var submitOrderResponse order.SubmitResponse
|
||||
if err := s.Validate(); err != nil {
|
||||
return submitOrderResponse, err
|
||||
@@ -537,7 +542,8 @@ func (g *Gemini) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
|
||||
return submitOrderResponse, err
|
||||
}
|
||||
|
||||
response, err := g.NewOrder(fpair.String(),
|
||||
response, err := g.NewOrder(ctx,
|
||||
fpair.String(),
|
||||
s.Amount,
|
||||
s.Price,
|
||||
s.Side.String(),
|
||||
@@ -556,12 +562,12 @@ func (g *Gemini) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
|
||||
|
||||
// ModifyOrder will allow of changing orderbook placement and limit to
|
||||
// market conversion
|
||||
func (g *Gemini) ModifyOrder(action *order.Modify) (order.Modify, error) {
|
||||
func (g *Gemini) 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 *Gemini) CancelOrder(o *order.Cancel) error {
|
||||
func (g *Gemini) CancelOrder(ctx context.Context, o *order.Cancel) error {
|
||||
if err := o.Validate(o.StandardCancel()); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -571,21 +577,21 @@ func (g *Gemini) CancelOrder(o *order.Cancel) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = g.CancelExistingOrder(orderIDInt)
|
||||
_, err = g.CancelExistingOrder(ctx, orderIDInt)
|
||||
return err
|
||||
}
|
||||
|
||||
// CancelBatchOrders cancels an orders by their corresponding ID numbers
|
||||
func (g *Gemini) CancelBatchOrders(o []order.Cancel) (order.CancelBatchResponse, error) {
|
||||
func (g *Gemini) 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 *Gemini) CancelAllOrders(_ *order.Cancel) (order.CancelAllResponse, error) {
|
||||
func (g *Gemini) CancelAllOrders(ctx context.Context, _ *order.Cancel) (order.CancelAllResponse, error) {
|
||||
cancelAllOrdersResponse := order.CancelAllResponse{
|
||||
Status: make(map[string]string),
|
||||
}
|
||||
resp, err := g.CancelExistingOrders(false)
|
||||
resp, err := g.CancelExistingOrders(ctx, false)
|
||||
if err != nil {
|
||||
return cancelAllOrdersResponse, err
|
||||
}
|
||||
@@ -598,14 +604,14 @@ func (g *Gemini) CancelAllOrders(_ *order.Cancel) (order.CancelAllResponse, erro
|
||||
}
|
||||
|
||||
// GetOrderInfo returns order information based on order ID
|
||||
func (g *Gemini) GetOrderInfo(orderID string, pair currency.Pair, assetType asset.Item) (order.Detail, error) {
|
||||
func (g *Gemini) GetOrderInfo(ctx context.Context, orderID string, pair currency.Pair, assetType asset.Item) (order.Detail, error) {
|
||||
var orderDetail order.Detail
|
||||
return orderDetail, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
// GetDepositAddress returns a deposit address for a specified currency
|
||||
func (g *Gemini) GetDepositAddress(cryptocurrency currency.Code, _ string) (string, error) {
|
||||
addr, err := g.GetCryptoDepositAddress("", cryptocurrency.String())
|
||||
func (g *Gemini) GetDepositAddress(ctx context.Context, cryptocurrency currency.Code, _ string) (string, error) {
|
||||
addr, err := g.GetCryptoDepositAddress(ctx, "", cryptocurrency.String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -614,11 +620,14 @@ func (g *Gemini) GetDepositAddress(cryptocurrency currency.Code, _ string) (stri
|
||||
|
||||
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
|
||||
// submitted
|
||||
func (g *Gemini) WithdrawCryptocurrencyFunds(withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
func (g *Gemini) WithdrawCryptocurrencyFunds(ctx context.Context, withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
if err := withdrawRequest.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := g.WithdrawCrypto(withdrawRequest.Crypto.Address, withdrawRequest.Currency.String(), withdrawRequest.Amount)
|
||||
resp, err := g.WithdrawCrypto(ctx,
|
||||
withdrawRequest.Crypto.Address,
|
||||
withdrawRequest.Currency.String(),
|
||||
withdrawRequest.Amount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -633,32 +642,32 @@ func (g *Gemini) WithdrawCryptocurrencyFunds(withdrawRequest *withdraw.Request)
|
||||
|
||||
// WithdrawFiatFunds returns a withdrawal ID when a
|
||||
// withdrawal is submitted
|
||||
func (g *Gemini) WithdrawFiatFunds(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
func (g *Gemini) WithdrawFiatFunds(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
|
||||
// withdrawal is submitted
|
||||
func (g *Gemini) WithdrawFiatFundsToInternationalBank(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
func (g *Gemini) 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 *Gemini) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
|
||||
func (g *Gemini) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) {
|
||||
if (!g.AllowAuthenticatedRequest() || g.SkipAuthCheck) && // 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 *Gemini) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
func (g *Gemini) GetActiveOrders(ctx context.Context, req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := g.GetOrders()
|
||||
resp, err := g.GetOrders(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -713,7 +722,7 @@ func (g *Gemini) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
|
||||
// GetOrderHistory retrieves account order information
|
||||
// Can Limit response to specific order status
|
||||
func (g *Gemini) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
func (g *Gemini) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -729,7 +738,9 @@ func (g *Gemini) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := g.GetTradeHistory(fpair.String(), req.StartTime.Unix())
|
||||
resp, err := g.GetTradeHistory(ctx,
|
||||
fpair.String(),
|
||||
req.StartTime.Unix())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -772,17 +783,17 @@ func (g *Gemini) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
|
||||
// ValidateCredentials validates current credentials used for wrapper
|
||||
// functionality
|
||||
func (g *Gemini) ValidateCredentials(assetType asset.Item) error {
|
||||
_, err := g.UpdateAccountInfo(assetType)
|
||||
func (g *Gemini) ValidateCredentials(ctx context.Context, assetType asset.Item) error {
|
||||
_, err := g.UpdateAccountInfo(ctx, assetType)
|
||||
return g.CheckTransientError(err)
|
||||
}
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (g *Gemini) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
func (g *Gemini) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
return kline.Item{}, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
||||
func (g *Gemini) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
func (g *Gemini) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
return kline.Item{}, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user