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:
@@ -53,8 +53,8 @@ type COINUT struct {
|
||||
}
|
||||
|
||||
// SeedInstruments seeds the instrument map
|
||||
func (c *COINUT) SeedInstruments() error {
|
||||
i, err := c.GetInstruments()
|
||||
func (c *COINUT) SeedInstruments(ctx context.Context) error {
|
||||
i, err := c.GetInstruments(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -66,23 +66,23 @@ func (c *COINUT) SeedInstruments() error {
|
||||
}
|
||||
|
||||
// GetInstruments returns instruments
|
||||
func (c *COINUT) GetInstruments() (Instruments, error) {
|
||||
func (c *COINUT) GetInstruments(ctx context.Context) (Instruments, error) {
|
||||
var result Instruments
|
||||
params := make(map[string]interface{})
|
||||
params["sec_type"] = strings.ToUpper(asset.Spot.String())
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutInstruments, params, false, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutInstruments, params, false, &result)
|
||||
}
|
||||
|
||||
// GetInstrumentTicker returns a ticker for a specific instrument
|
||||
func (c *COINUT) GetInstrumentTicker(instrumentID int64) (Ticker, error) {
|
||||
func (c *COINUT) GetInstrumentTicker(ctx context.Context, instrumentID int64) (Ticker, error) {
|
||||
var result Ticker
|
||||
params := make(map[string]interface{})
|
||||
params["inst_id"] = instrumentID
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutTicker, params, false, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutTicker, params, false, &result)
|
||||
}
|
||||
|
||||
// GetInstrumentOrderbook returns the orderbooks for a specific instrument
|
||||
func (c *COINUT) GetInstrumentOrderbook(instrumentID, limit int64) (Orderbook, error) {
|
||||
func (c *COINUT) GetInstrumentOrderbook(ctx context.Context, instrumentID, limit int64) (Orderbook, error) {
|
||||
var result Orderbook
|
||||
params := make(map[string]interface{})
|
||||
params["inst_id"] = instrumentID
|
||||
@@ -90,26 +90,26 @@ func (c *COINUT) GetInstrumentOrderbook(instrumentID, limit int64) (Orderbook, e
|
||||
params["top_n"] = limit
|
||||
}
|
||||
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutOrderbook, params, false, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutOrderbook, params, false, &result)
|
||||
}
|
||||
|
||||
// GetTrades returns trade information
|
||||
func (c *COINUT) GetTrades(instrumentID int64) (Trades, error) {
|
||||
func (c *COINUT) GetTrades(ctx context.Context, instrumentID int64) (Trades, error) {
|
||||
var result Trades
|
||||
params := make(map[string]interface{})
|
||||
params["inst_id"] = instrumentID
|
||||
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutTrades, params, false, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutTrades, params, false, &result)
|
||||
}
|
||||
|
||||
// GetUserBalance returns the full user balance
|
||||
func (c *COINUT) GetUserBalance() (*UserBalance, error) {
|
||||
func (c *COINUT) GetUserBalance(ctx context.Context) (*UserBalance, error) {
|
||||
var result *UserBalance
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutBalance, nil, true, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutBalance, nil, true, &result)
|
||||
}
|
||||
|
||||
// NewOrder places a new order on the exchange
|
||||
func (c *COINUT) NewOrder(instrumentID int64, quantity, price float64, buy bool, orderID uint32) (interface{}, error) {
|
||||
func (c *COINUT) NewOrder(ctx context.Context, instrumentID int64, quantity, price float64, buy bool, orderID uint32) (interface{}, error) {
|
||||
var result interface{}
|
||||
params := make(map[string]interface{})
|
||||
params["inst_id"] = instrumentID
|
||||
@@ -123,28 +123,28 @@ func (c *COINUT) NewOrder(instrumentID int64, quantity, price float64, buy bool,
|
||||
}
|
||||
params["client_ord_id"] = orderID
|
||||
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutOrder, params, true, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutOrder, params, true, &result)
|
||||
}
|
||||
|
||||
// NewOrders places multiple orders on the exchange
|
||||
func (c *COINUT) NewOrders(orders []Order) ([]OrdersBase, error) {
|
||||
func (c *COINUT) NewOrders(ctx context.Context, orders []Order) ([]OrdersBase, error) {
|
||||
var result OrdersResponse
|
||||
params := make(map[string]interface{})
|
||||
params["orders"] = orders
|
||||
|
||||
return result.Data, c.SendHTTPRequest(exchange.RestSpot, coinutOrders, params, true, &result.Data)
|
||||
return result.Data, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutOrders, params, true, &result.Data)
|
||||
}
|
||||
|
||||
// GetOpenOrders returns a list of open order and relevant information
|
||||
func (c *COINUT) GetOpenOrders(instrumentID int64) (GetOpenOrdersResponse, error) {
|
||||
func (c *COINUT) GetOpenOrders(ctx context.Context, instrumentID int64) (GetOpenOrdersResponse, error) {
|
||||
var result GetOpenOrdersResponse
|
||||
params := make(map[string]interface{})
|
||||
params["inst_id"] = instrumentID
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutOrdersOpen, params, true, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutOrdersOpen, params, true, &result)
|
||||
}
|
||||
|
||||
// CancelExistingOrder cancels a specific order and returns if it was actioned
|
||||
func (c *COINUT) CancelExistingOrder(instrumentID, orderID int64) (bool, error) {
|
||||
func (c *COINUT) CancelExistingOrder(ctx context.Context, instrumentID, orderID int64) (bool, error) {
|
||||
var result GenericResponse
|
||||
params := make(map[string]interface{})
|
||||
type Request struct {
|
||||
@@ -160,7 +160,7 @@ func (c *COINUT) CancelExistingOrder(instrumentID, orderID int64) (bool, error)
|
||||
entries := []Request{entry}
|
||||
params["entries"] = entries
|
||||
|
||||
err := c.SendHTTPRequest(exchange.RestSpot, coinutOrdersCancel, params, true, &result)
|
||||
err := c.SendHTTPRequest(ctx, exchange.RestSpot, coinutOrdersCancel, params, true, &result)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -168,18 +168,18 @@ func (c *COINUT) CancelExistingOrder(instrumentID, orderID int64) (bool, error)
|
||||
}
|
||||
|
||||
// CancelOrders cancels multiple orders
|
||||
func (c *COINUT) CancelOrders(orders []CancelOrders) (CancelOrdersResponse, error) {
|
||||
func (c *COINUT) CancelOrders(ctx context.Context, orders []CancelOrders) (CancelOrdersResponse, error) {
|
||||
var result CancelOrdersResponse
|
||||
params := make(map[string]interface{})
|
||||
var entries []CancelOrders
|
||||
entries = append(entries, orders...)
|
||||
params["entries"] = entries
|
||||
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutOrdersCancel, params, true, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutOrdersCancel, params, true, &result)
|
||||
}
|
||||
|
||||
// GetTradeHistory returns trade history for a specific instrument.
|
||||
func (c *COINUT) GetTradeHistory(instrumentID, start, limit int64) (TradeHistory, error) {
|
||||
func (c *COINUT) GetTradeHistory(ctx context.Context, instrumentID, start, limit int64) (TradeHistory, error) {
|
||||
var result TradeHistory
|
||||
params := make(map[string]interface{})
|
||||
params["inst_id"] = instrumentID
|
||||
@@ -190,39 +190,39 @@ func (c *COINUT) GetTradeHistory(instrumentID, start, limit int64) (TradeHistory
|
||||
params["limit"] = limit
|
||||
}
|
||||
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutTradeHistory, params, true, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutTradeHistory, params, true, &result)
|
||||
}
|
||||
|
||||
// GetIndexTicker returns the index ticker for an asset
|
||||
func (c *COINUT) GetIndexTicker(asset string) (IndexTicker, error) {
|
||||
func (c *COINUT) GetIndexTicker(ctx context.Context, asset string) (IndexTicker, error) {
|
||||
var result IndexTicker
|
||||
params := make(map[string]interface{})
|
||||
params["asset"] = asset
|
||||
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutIndexTicker, params, false, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutIndexTicker, params, false, &result)
|
||||
}
|
||||
|
||||
// GetDerivativeInstruments returns a list of derivative instruments
|
||||
func (c *COINUT) GetDerivativeInstruments(secType string) (interface{}, error) {
|
||||
func (c *COINUT) GetDerivativeInstruments(ctx context.Context, secType string) (interface{}, error) {
|
||||
var result interface{} // to-do
|
||||
params := make(map[string]interface{})
|
||||
params["sec_type"] = secType
|
||||
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutInstruments, params, false, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutInstruments, params, false, &result)
|
||||
}
|
||||
|
||||
// GetOptionChain returns option chain
|
||||
func (c *COINUT) GetOptionChain(asset, secType string) (OptionChainResponse, error) {
|
||||
func (c *COINUT) GetOptionChain(ctx context.Context, asset, secType string) (OptionChainResponse, error) {
|
||||
var result OptionChainResponse
|
||||
params := make(map[string]interface{})
|
||||
params["asset"] = asset
|
||||
params["sec_type"] = secType
|
||||
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutOptionChain, params, false, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutOptionChain, params, false, &result)
|
||||
}
|
||||
|
||||
// GetPositionHistory returns position history
|
||||
func (c *COINUT) GetPositionHistory(secType string, start, limit int) (PositionHistory, error) {
|
||||
func (c *COINUT) GetPositionHistory(ctx context.Context, secType string, start, limit int) (PositionHistory, error) {
|
||||
var result PositionHistory
|
||||
params := make(map[string]interface{})
|
||||
params["sec_type"] = secType
|
||||
@@ -233,11 +233,11 @@ func (c *COINUT) GetPositionHistory(secType string, start, limit int) (PositionH
|
||||
params["limit"] = limit
|
||||
}
|
||||
|
||||
return result, c.SendHTTPRequest(exchange.RestSpot, coinutPositionHistory, params, true, &result)
|
||||
return result, c.SendHTTPRequest(ctx, exchange.RestSpot, coinutPositionHistory, params, true, &result)
|
||||
}
|
||||
|
||||
// GetOpenPositions returns all your current opened positions
|
||||
func (c *COINUT) GetOpenPositions(instrumentID int) ([]OpenPosition, error) {
|
||||
func (c *COINUT) GetOpenPositions(ctx context.Context, instrumentID int) ([]OpenPosition, error) {
|
||||
type Response struct {
|
||||
Positions []OpenPosition `json:"positions"`
|
||||
}
|
||||
@@ -246,13 +246,13 @@ func (c *COINUT) GetOpenPositions(instrumentID int) ([]OpenPosition, error) {
|
||||
params["inst_id"] = instrumentID
|
||||
|
||||
return result.Positions,
|
||||
c.SendHTTPRequest(exchange.RestSpot, coinutPositionOpen, params, true, &result)
|
||||
c.SendHTTPRequest(ctx, exchange.RestSpot, coinutPositionOpen, params, true, &result)
|
||||
}
|
||||
|
||||
// to-do: user position update via websocket
|
||||
|
||||
// SendHTTPRequest sends either an authenticated or unauthenticated HTTP request
|
||||
func (c *COINUT) SendHTTPRequest(ep exchange.URL, apiRequest string, params map[string]interface{}, authenticated bool, result interface{}) (err error) {
|
||||
func (c *COINUT) SendHTTPRequest(ctx context.Context, ep exchange.URL, apiRequest string, params map[string]interface{}, authenticated bool, result interface{}) (err error) {
|
||||
if !c.API.AuthenticatedSupport && authenticated {
|
||||
return fmt.Errorf("%s %w", c.Name, exchange.ErrAuthenticatedRequestWithoutCredentialsSet)
|
||||
}
|
||||
@@ -267,7 +267,7 @@ func (c *COINUT) SendHTTPRequest(ep exchange.URL, apiRequest string, params map[
|
||||
}
|
||||
|
||||
var rawMsg json.RawMessage
|
||||
err = c.SendPayload(context.Background(), request.Unset, func() (*request.Item, error) {
|
||||
err = c.SendPayload(ctx, request.Unset, func() (*request.Item, error) {
|
||||
params["nonce"] = getNonce()
|
||||
params["request"] = apiRequest
|
||||
|
||||
@@ -315,7 +315,8 @@ func (c *COINUT) SendHTTPRequest(ep exchange.URL, apiRequest string, params map[
|
||||
}
|
||||
|
||||
if genResp.Status[0] != coinutStatusOK {
|
||||
return fmt.Errorf("%s SendHTTPRequest error: %s", c.Name,
|
||||
return fmt.Errorf("%s SendHTTPRequest error: %s",
|
||||
c.Name,
|
||||
genResp.Status[0])
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package coinut
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -50,7 +51,7 @@ func TestMain(m *testing.M) {
|
||||
if err != nil {
|
||||
log.Fatal("Coinut setup error", err)
|
||||
}
|
||||
err = c.SeedInstruments()
|
||||
err = c.SeedInstruments(context.Background())
|
||||
if err != nil {
|
||||
log.Fatal("Coinut setup error ", err)
|
||||
}
|
||||
@@ -87,14 +88,14 @@ func setupWSTestAuth(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetInstruments(t *testing.T) {
|
||||
_, err := c.GetInstruments()
|
||||
_, err := c.GetInstruments(context.Background())
|
||||
if err != nil {
|
||||
t.Error("GetInstruments() error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedInstruments(t *testing.T) {
|
||||
err := c.SeedInstruments()
|
||||
err := c.SeedInstruments(context.Background())
|
||||
if err != nil {
|
||||
// No point checking the next condition
|
||||
t.Fatal(err)
|
||||
@@ -117,7 +118,7 @@ func setFeeBuilder() *exchange.FeeBuilder {
|
||||
// TestGetFeeByTypeOfflineTradeFee logic test
|
||||
func TestGetFeeByTypeOfflineTradeFee(t *testing.T) {
|
||||
var feeBuilder = setFeeBuilder()
|
||||
_, err := c.GetFeeByType(feeBuilder)
|
||||
_, err := c.GetFeeByType(context.Background(), feeBuilder)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -246,7 +247,7 @@ func TestGetActiveOrders(t *testing.T) {
|
||||
Type: order.AnyType,
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
_, err := c.GetActiveOrders(&getOrdersRequest)
|
||||
_, err := c.GetActiveOrders(context.Background(), &getOrdersRequest)
|
||||
if areTestAPIKeysSet() && err != nil {
|
||||
t.Errorf("Could not get open orders: %s", err)
|
||||
}
|
||||
@@ -261,7 +262,7 @@ func TestGetOrderHistoryWrapper(t *testing.T) {
|
||||
currency.USD)},
|
||||
}
|
||||
|
||||
_, err := c.GetOrderHistory(&getOrdersRequest)
|
||||
_, err := c.GetOrderHistory(context.Background(), &getOrdersRequest)
|
||||
if areTestAPIKeysSet() && err != nil {
|
||||
t.Errorf("Could not get order history: %s", err)
|
||||
}
|
||||
@@ -290,7 +291,7 @@ func TestSubmitOrder(t *testing.T) {
|
||||
ClientID: "123",
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
response, err := c.SubmitOrder(orderSubmission)
|
||||
response, err := c.SubmitOrder(context.Background(), orderSubmission)
|
||||
if areTestAPIKeysSet() && (err != nil || !response.IsOrderPlaced) {
|
||||
t.Errorf("Order failed to be placed: %v", err)
|
||||
} else if !areTestAPIKeysSet() && err == nil {
|
||||
@@ -311,7 +312,7 @@ func TestCancelExchangeOrder(t *testing.T) {
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
err := c.CancelOrder(orderCancellation)
|
||||
err := c.CancelOrder(context.Background(), orderCancellation)
|
||||
if !areTestAPIKeysSet() && err == nil {
|
||||
t.Error("Expecting an error when no keys are set")
|
||||
}
|
||||
@@ -334,7 +335,7 @@ func TestCancelAllExchangeOrders(t *testing.T) {
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
resp, err := c.CancelAllOrders(orderCancellation)
|
||||
resp, err := c.CancelAllOrders(context.Background(), orderCancellation)
|
||||
|
||||
if !areTestAPIKeysSet() && err == nil {
|
||||
t.Error("Expecting an error when no keys are set")
|
||||
@@ -350,12 +351,12 @@ func TestCancelAllExchangeOrders(t *testing.T) {
|
||||
|
||||
func TestGetAccountInfo(t *testing.T) {
|
||||
if apiKey != "" || clientID != "" {
|
||||
_, err := c.UpdateAccountInfo(asset.Spot)
|
||||
_, err := c.UpdateAccountInfo(context.Background(), asset.Spot)
|
||||
if err != nil {
|
||||
t.Error("GetAccountInfo() error", err)
|
||||
}
|
||||
} else {
|
||||
_, err := c.UpdateAccountInfo(asset.Spot)
|
||||
_, err := c.UpdateAccountInfo(context.Background(), asset.Spot)
|
||||
if err == nil {
|
||||
t.Error("GetAccountInfo() Expected error")
|
||||
}
|
||||
@@ -366,7 +367,8 @@ func TestModifyOrder(t *testing.T) {
|
||||
if areTestAPIKeysSet() && !canManipulateRealOrders {
|
||||
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
||||
}
|
||||
_, err := c.ModifyOrder(&order.Modify{AssetType: asset.Spot})
|
||||
_, err := c.ModifyOrder(context.Background(),
|
||||
&order.Modify{AssetType: asset.Spot})
|
||||
if err == nil {
|
||||
t.Error("ModifyOrder() Expected error")
|
||||
}
|
||||
@@ -386,7 +388,8 @@ func TestWithdraw(t *testing.T) {
|
||||
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
||||
}
|
||||
|
||||
_, err := c.WithdrawCryptocurrencyFunds(&withdrawCryptoRequest)
|
||||
_, err := c.WithdrawCryptocurrencyFunds(context.Background(),
|
||||
&withdrawCryptoRequest)
|
||||
if err != common.ErrFunctionNotSupported {
|
||||
t.Errorf("Expected 'Not supported', received %v", err)
|
||||
}
|
||||
@@ -398,7 +401,7 @@ func TestWithdrawFiat(t *testing.T) {
|
||||
}
|
||||
|
||||
var withdrawFiatRequest = withdraw.Request{}
|
||||
_, err := c.WithdrawFiatFunds(&withdrawFiatRequest)
|
||||
_, err := c.WithdrawFiatFunds(context.Background(), &withdrawFiatRequest)
|
||||
if err != common.ErrFunctionNotSupported {
|
||||
t.Errorf("Expected '%v', received: '%v'", common.ErrFunctionNotSupported, err)
|
||||
}
|
||||
@@ -410,14 +413,15 @@ func TestWithdrawInternationalBank(t *testing.T) {
|
||||
}
|
||||
|
||||
var withdrawFiatRequest = withdraw.Request{}
|
||||
_, err := c.WithdrawFiatFundsToInternationalBank(&withdrawFiatRequest)
|
||||
_, err := c.WithdrawFiatFundsToInternationalBank(context.Background(),
|
||||
&withdrawFiatRequest)
|
||||
if err != common.ErrFunctionNotSupported {
|
||||
t.Errorf("Expected '%v', received: '%v'", common.ErrFunctionNotSupported, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDepositAddress(t *testing.T) {
|
||||
_, err := c.GetDepositAddress(currency.BTC, "")
|
||||
_, err := c.GetDepositAddress(context.Background(), currency.BTC, "")
|
||||
if err == nil {
|
||||
t.Error("GetDepositAddress() function unsupported cannot be nil")
|
||||
}
|
||||
@@ -511,7 +515,7 @@ func TestWsAuthCancelOrdersWrapper(t *testing.T) {
|
||||
orderDetails := order.Cancel{
|
||||
Pair: currency.NewPair(currency.LTC, currency.BTC),
|
||||
}
|
||||
_, err := c.CancelAllOrders(&orderDetails)
|
||||
_, err := c.CancelAllOrders(context.Background(), &orderDetails)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -1122,7 +1126,7 @@ func TestGetRecentTrades(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = c.GetRecentTrades(currencyPair, asset.Spot)
|
||||
_, err = c.GetRecentTrades(context.Background(), currencyPair, asset.Spot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -1134,7 +1138,8 @@ func TestGetHistoricTrades(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = c.GetHistoricTrades(currencyPair, asset.Spot, time.Now().Add(-time.Minute*15), time.Now())
|
||||
_, err = c.GetHistoricTrades(context.Background(),
|
||||
currencyPair, asset.Spot, time.Now().Add(-time.Minute*15), time.Now())
|
||||
if err != nil && err != common.ErrFunctionNotSupported {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package coinut
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
@@ -42,7 +43,7 @@ func (c *COINUT) GetDefaultConfig() (*config.ExchangeConfig, error) {
|
||||
}
|
||||
|
||||
if c.Features.Supports.RESTCapabilities.AutoPairUpdates {
|
||||
err = c.UpdateTradablePairs(true)
|
||||
err = c.UpdateTradablePairs(context.TODO(), true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -247,14 +248,14 @@ func (c *COINUT) Run() {
|
||||
return
|
||||
}
|
||||
|
||||
err = c.UpdateTradablePairs(forceUpdate)
|
||||
err = c.UpdateTradablePairs(context.TODO(), forceUpdate)
|
||||
if err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s failed to update tradable pairs. Err: %s", c.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// FetchTradablePairs returns a list of the exchanges tradable pairs
|
||||
func (c *COINUT) FetchTradablePairs(asset asset.Item) ([]string, error) {
|
||||
func (c *COINUT) FetchTradablePairs(ctx context.Context, asset asset.Item) ([]string, error) {
|
||||
var instruments map[string][]InstrumentBase
|
||||
var resp Instruments
|
||||
var err error
|
||||
@@ -264,7 +265,7 @@ func (c *COINUT) FetchTradablePairs(asset asset.Item) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
resp, err = c.GetInstruments()
|
||||
resp, err = c.GetInstruments(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -288,8 +289,8 @@ func (c *COINUT) FetchTradablePairs(asset asset.Item) ([]string, error) {
|
||||
|
||||
// UpdateTradablePairs updates the exchanges available pairs and stores
|
||||
// them in the exchanges config
|
||||
func (c *COINUT) UpdateTradablePairs(forceUpdate bool) error {
|
||||
pairs, err := c.FetchTradablePairs(asset.Spot)
|
||||
func (c *COINUT) UpdateTradablePairs(ctx context.Context, forceUpdate bool) error {
|
||||
pairs, err := c.FetchTradablePairs(ctx, asset.Spot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -303,7 +304,7 @@ func (c *COINUT) UpdateTradablePairs(forceUpdate bool) error {
|
||||
|
||||
// UpdateAccountInfo retrieves balances for all enabled currencies for the
|
||||
// COINUT exchange
|
||||
func (c *COINUT) UpdateAccountInfo(assetType asset.Item) (account.Holdings, error) {
|
||||
func (c *COINUT) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
||||
var info account.Holdings
|
||||
var bal *UserBalance
|
||||
var err error
|
||||
@@ -315,7 +316,7 @@ func (c *COINUT) UpdateAccountInfo(assetType asset.Item) (account.Holdings, erro
|
||||
}
|
||||
bal = resp
|
||||
} else {
|
||||
bal, err = c.GetUserBalance()
|
||||
bal, err = c.GetUserBalance(ctx)
|
||||
if err != nil {
|
||||
return info, err
|
||||
}
|
||||
@@ -393,22 +394,22 @@ func (c *COINUT) UpdateAccountInfo(assetType asset.Item) (account.Holdings, erro
|
||||
}
|
||||
|
||||
// FetchAccountInfo retrieves balances for all enabled currencies
|
||||
func (c *COINUT) FetchAccountInfo(assetType asset.Item) (account.Holdings, error) {
|
||||
func (c *COINUT) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
||||
acc, err := account.GetHoldings(c.Name, assetType)
|
||||
if err != nil {
|
||||
return c.UpdateAccountInfo(assetType)
|
||||
return c.UpdateAccountInfo(ctx, assetType)
|
||||
}
|
||||
|
||||
return acc, nil
|
||||
}
|
||||
|
||||
// UpdateTickers updates the ticker for all currency pairs of a given asset type
|
||||
func (c *COINUT) UpdateTickers(a asset.Item) error {
|
||||
func (c *COINUT) UpdateTickers(ctx context.Context, a asset.Item) error {
|
||||
return common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// UpdateTicker updates and returns the ticker for a currency pair
|
||||
func (c *COINUT) UpdateTicker(p currency.Pair, a asset.Item) (*ticker.Price, error) {
|
||||
func (c *COINUT) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) (*ticker.Price, error) {
|
||||
err := c.loadInstrumentsIfNotLoaded()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -424,7 +425,7 @@ func (c *COINUT) UpdateTicker(p currency.Pair, a asset.Item) (*ticker.Price, err
|
||||
return nil, errors.New("unable to lookup instrument ID")
|
||||
}
|
||||
var tick Ticker
|
||||
tick, err = c.GetInstrumentTicker(instID)
|
||||
tick, err = c.GetInstrumentTicker(ctx, instID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -448,25 +449,25 @@ func (c *COINUT) UpdateTicker(p currency.Pair, a asset.Item) (*ticker.Price, err
|
||||
}
|
||||
|
||||
// FetchTicker returns the ticker for a currency pair
|
||||
func (c *COINUT) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
|
||||
func (c *COINUT) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
|
||||
tickerNew, err := ticker.GetTicker(c.Name, p, assetType)
|
||||
if err != nil {
|
||||
return c.UpdateTicker(p, assetType)
|
||||
return c.UpdateTicker(ctx, p, assetType)
|
||||
}
|
||||
return tickerNew, nil
|
||||
}
|
||||
|
||||
// FetchOrderbook returns orderbook base on the currency pair
|
||||
func (c *COINUT) FetchOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
func (c *COINUT) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
ob, err := orderbook.Get(c.Name, p, assetType)
|
||||
if err != nil {
|
||||
return c.UpdateOrderbook(p, assetType)
|
||||
return c.UpdateOrderbook(ctx, p, assetType)
|
||||
}
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
||||
func (c *COINUT) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
func (c *COINUT) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
book := &orderbook.Base{
|
||||
Exchange: c.Name,
|
||||
Pair: p,
|
||||
@@ -488,7 +489,7 @@ func (c *COINUT) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderb
|
||||
return book, errLookupInstrumentID
|
||||
}
|
||||
|
||||
orderbookNew, err := c.GetInstrumentOrderbook(instID, 200)
|
||||
orderbookNew, err := c.GetInstrumentOrderbook(ctx, instID, 200)
|
||||
if err != nil {
|
||||
return book, err
|
||||
}
|
||||
@@ -513,17 +514,17 @@ func (c *COINUT) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderb
|
||||
|
||||
// GetFundingHistory returns funding history, deposits and
|
||||
// withdrawals
|
||||
func (c *COINUT) GetFundingHistory() ([]exchange.FundHistory, error) {
|
||||
func (c *COINUT) GetFundingHistory(ctx context.Context) ([]exchange.FundHistory, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// GetWithdrawalsHistory returns previous withdrawals data
|
||||
func (c *COINUT) GetWithdrawalsHistory(cur currency.Code) (resp []exchange.WithdrawalHistory, err error) {
|
||||
func (c *COINUT) GetWithdrawalsHistory(_ context.Context, _ currency.Code) (resp []exchange.WithdrawalHistory, err error) {
|
||||
return nil, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
// GetRecentTrades returns the most recent trades for a currency and asset
|
||||
func (c *COINUT) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
|
||||
func (c *COINUT) GetRecentTrades(ctx context.Context, p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
|
||||
var err error
|
||||
p, err = c.FormatExchangeCurrency(p, assetType)
|
||||
if err != nil {
|
||||
@@ -534,7 +535,7 @@ func (c *COINUT) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade
|
||||
return nil, errLookupInstrumentID
|
||||
}
|
||||
var tradeData Trades
|
||||
tradeData, err = c.GetTrades(currencyID)
|
||||
tradeData, err = c.GetTrades(ctx, currencyID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -567,12 +568,12 @@ func (c *COINUT) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade
|
||||
}
|
||||
|
||||
// GetHistoricTrades returns historic trade data within the timeframe provided
|
||||
func (c *COINUT) GetHistoricTrades(_ currency.Pair, _ asset.Item, _, _ time.Time) ([]trade.Data, error) {
|
||||
func (c *COINUT) GetHistoricTrades(_ context.Context, _ currency.Pair, _ asset.Item, _, _ time.Time) ([]trade.Data, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// SubmitOrder submits a new order
|
||||
func (c *COINUT) SubmitOrder(o *order.Submit) (order.SubmitResponse, error) {
|
||||
func (c *COINUT) SubmitOrder(ctx context.Context, o *order.Submit) (order.SubmitResponse, error) {
|
||||
if err := o.Validate(); err != nil {
|
||||
return order.SubmitResponse{}, err
|
||||
}
|
||||
@@ -620,8 +621,12 @@ func (c *COINUT) SubmitOrder(o *order.Submit) (order.SubmitResponse, error) {
|
||||
return submitOrderResponse, err
|
||||
}
|
||||
clientIDUint := uint32(clientIDInt)
|
||||
APIResponse, err = c.NewOrder(currencyID, o.Amount, o.Price,
|
||||
isBuyOrder, clientIDUint)
|
||||
APIResponse, err = c.NewOrder(ctx,
|
||||
currencyID,
|
||||
o.Amount,
|
||||
o.Price,
|
||||
isBuyOrder,
|
||||
clientIDUint)
|
||||
if err != nil {
|
||||
return submitOrderResponse, err
|
||||
}
|
||||
@@ -647,12 +652,12 @@ func (c *COINUT) SubmitOrder(o *order.Submit) (order.SubmitResponse, error) {
|
||||
|
||||
// ModifyOrder will allow of changing orderbook placement and limit to
|
||||
// market conversion
|
||||
func (c *COINUT) ModifyOrder(action *order.Modify) (order.Modify, error) {
|
||||
func (c *COINUT) 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 (c *COINUT) CancelOrder(o *order.Cancel) error {
|
||||
func (c *COINUT) CancelOrder(ctx context.Context, o *order.Cancel) error {
|
||||
if err := o.Validate(o.StandardCancel()); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -689,7 +694,7 @@ func (c *COINUT) CancelOrder(o *order.Cancel) error {
|
||||
if currencyID == 0 {
|
||||
return errLookupInstrumentID
|
||||
}
|
||||
_, err = c.CancelExistingOrder(currencyID, orderIDInt)
|
||||
_, err = c.CancelExistingOrder(ctx, currencyID, orderIDInt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -699,12 +704,12 @@ func (c *COINUT) CancelOrder(o *order.Cancel) error {
|
||||
}
|
||||
|
||||
// CancelBatchOrders cancels an orders by their corresponding ID numbers
|
||||
func (c *COINUT) CancelBatchOrders(o []order.Cancel) (order.CancelBatchResponse, error) {
|
||||
func (c *COINUT) CancelBatchOrders(_ context.Context, _ []order.Cancel) (order.CancelBatchResponse, error) {
|
||||
return order.CancelBatchResponse{}, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
// CancelAllOrders cancels all orders associated with a currency pair
|
||||
func (c *COINUT) CancelAllOrders(details *order.Cancel) (order.CancelAllResponse, error) {
|
||||
func (c *COINUT) CancelAllOrders(ctx context.Context, details *order.Cancel) (order.CancelAllResponse, error) {
|
||||
if err := details.Validate(); err != nil {
|
||||
return order.CancelAllResponse{}, err
|
||||
}
|
||||
@@ -752,7 +757,7 @@ func (c *COINUT) CancelAllOrders(details *order.Cancel) (order.CancelAllResponse
|
||||
return cancelAllOrdersResponse, err
|
||||
}
|
||||
if ids[x] == c.instrumentMap.LookupID(fpair.String()) {
|
||||
openOrders, err := c.GetOpenOrders(ids[x])
|
||||
openOrders, err := c.GetOpenOrders(ctx, ids[x])
|
||||
if err != nil {
|
||||
return cancelAllOrdersResponse, err
|
||||
}
|
||||
@@ -770,7 +775,7 @@ func (c *COINUT) CancelAllOrders(details *order.Cancel) (order.CancelAllResponse
|
||||
}
|
||||
|
||||
if len(allTheOrdersToCancel) > 0 {
|
||||
resp, err := c.CancelOrders(allTheOrdersToCancel)
|
||||
resp, err := c.CancelOrders(ctx, allTheOrdersToCancel)
|
||||
if err != nil {
|
||||
return cancelAllOrdersResponse, err
|
||||
}
|
||||
@@ -787,35 +792,35 @@ func (c *COINUT) CancelAllOrders(details *order.Cancel) (order.CancelAllResponse
|
||||
}
|
||||
|
||||
// GetOrderInfo returns order information based on order ID
|
||||
func (c *COINUT) GetOrderInfo(_ string, _ currency.Pair, _ asset.Item) (order.Detail, error) {
|
||||
func (c *COINUT) GetOrderInfo(_ context.Context, _ string, _ currency.Pair, _ asset.Item) (order.Detail, error) {
|
||||
return order.Detail{}, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
// GetDepositAddress returns a deposit address for a specified currency
|
||||
func (c *COINUT) GetDepositAddress(_ currency.Code, _ string) (string, error) {
|
||||
func (c *COINUT) GetDepositAddress(_ context.Context, _ currency.Code, _ string) (string, error) {
|
||||
return "", common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
|
||||
// submitted
|
||||
func (c *COINUT) WithdrawCryptocurrencyFunds(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
func (c *COINUT) WithdrawCryptocurrencyFunds(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// WithdrawFiatFunds returns a withdrawal ID when a
|
||||
// withdrawal is submitted
|
||||
func (c *COINUT) WithdrawFiatFunds(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
func (c *COINUT) WithdrawFiatFunds(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
|
||||
// withdrawal is submitted
|
||||
func (c *COINUT) WithdrawFiatFundsToInternationalBank(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
func (c *COINUT) WithdrawFiatFundsToInternationalBank(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
||||
return nil, common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// GetFeeByType returns an estimate of fee based on type of transaction
|
||||
func (c *COINUT) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
|
||||
func (c *COINUT) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) {
|
||||
if !c.AllowAuthenticatedRequest() && // Todo check connection status
|
||||
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
|
||||
feeBuilder.FeeType = exchange.OfflineTradeFee
|
||||
@@ -824,7 +829,7 @@ func (c *COINUT) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error)
|
||||
}
|
||||
|
||||
// GetActiveOrders retrieves any orders that are active/open
|
||||
func (c *COINUT) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
func (c *COINUT) GetActiveOrders(ctx context.Context, req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -905,7 +910,7 @@ func (c *COINUT) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
}
|
||||
|
||||
for x := range instrumentsToUse {
|
||||
openOrders, err := c.GetOpenOrders(instrumentsToUse[x])
|
||||
openOrders, err := c.GetOpenOrders(ctx, instrumentsToUse[x])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -940,7 +945,7 @@ func (c *COINUT) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
|
||||
// GetOrderHistory retrieves account order information
|
||||
// Can Limit response to specific order status
|
||||
func (c *COINUT) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
func (c *COINUT) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1011,7 +1016,7 @@ func (c *COINUT) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
}
|
||||
|
||||
for x := range instrumentsToUse {
|
||||
orders, err := c.GetTradeHistory(instrumentsToUse[x], -1, -1)
|
||||
orders, err := c.GetTradeHistory(ctx, instrumentsToUse[x], -1, -1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1045,7 +1050,7 @@ func (c *COINUT) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, e
|
||||
}
|
||||
|
||||
// AuthenticateWebsocket sends an authentication message to the websocket
|
||||
func (c *COINUT) AuthenticateWebsocket() error {
|
||||
func (c *COINUT) AuthenticateWebsocket(_ context.Context) error {
|
||||
return c.wsAuthenticate()
|
||||
}
|
||||
|
||||
@@ -1057,7 +1062,7 @@ func (c *COINUT) loadInstrumentsIfNotLoaded() error {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err := c.SeedInstruments()
|
||||
err := c.SeedInstruments(context.TODO())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1068,17 +1073,17 @@ func (c *COINUT) loadInstrumentsIfNotLoaded() error {
|
||||
|
||||
// ValidateCredentials validates current credentials used for wrapper
|
||||
// functionality
|
||||
func (c *COINUT) ValidateCredentials(assetType asset.Item) error {
|
||||
_, err := c.UpdateAccountInfo(assetType)
|
||||
func (c *COINUT) ValidateCredentials(ctx context.Context, assetType asset.Item) error {
|
||||
_, err := c.UpdateAccountInfo(ctx, assetType)
|
||||
return c.CheckTransientError(err)
|
||||
}
|
||||
|
||||
// GetHistoricCandles returns candles between a time period for a set time interval
|
||||
func (c *COINUT) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
func (c *COINUT) 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 (c *COINUT) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
||||
func (c *COINUT) 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