mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-31 23:16:54 +00:00
Feature: Implement funding rates, futures and coin margin (exchange API coverage) (#530)
* ALMOST THERE * more api wips * more api thingz * testing n more api wipz * more apiz * more wips * what is goin on * more wips * whip n testing * testing * testing no keys * remove log * kraken is broken ugh * still broken * fixing auth funcs + usdtm api docs * wip * api stuffs * whip * more wips * whip * more wip * api wip n testing * wip * wip * unsaved * wip n testing * wip * wip * wip * wip * wip * wip * wip * wip * wip * whip * wrapper authenticated functions * adding asset type and fixing dependencies * wip * binance auth wrapper start * wrapper functionality * wip * wip * wip * wrapper cancel functions * order submission for wrappers * wip * more error fixing and nits * websocket beginning n error fix * wip * WOW * glorious n shazzy nits * useless nits * wip * fixing things * merge stuffs * crapveyor * crapveyor rebuild * probably broke more things than he fixed * rm lns n other thangs * hope * please * stop it * done * ofcourse * rm vb * fix lbank * appveyor please * float lev * DONT ASK RYAN FOR HELP EVER * wip * wip * endpoint upgrades continued * path upgrade * NeeeNeeeNeeeNeeeNING * fix stuffs * fixing time issue * fixing broken funcs * glorious nits * shaz changes * fixing errors for fundmon * more error fixing for fundmon * test running past 30s * basic changes * THX AGAIN SHAZBERT * path system upgrade * config upgrade * unsaved stuffs * broken wip config upgrade * path system upgrade contd. * path system upgrade contd * path upgrade ready for review * testing verbose removed * linter stuffs * appveyor stuffs * appveyor stuff * fixed? * bugfix * wip * broken stuff * fix test * wierd hack fix * appveyor pls stop * error found * more useless nits * bitmex err * broken wip * broken wip path upgrade change to uint32 * changed url lookups to uint * WOW * ready4review * config fixed HOPEFULLY * config fix and glorious changes * efficient way of getting orders and open orders * binance wrapper logic fixing * testing, adding tests and fixing lot of errrrrs * merge master * appveyor stuffs * appveyor stuffs * fmt * test * octalLiteral issue fix? * octalLiteral fix? * rm vb * prnt ln to restart * adding testz * test fixzzz * READY FOR REVIEW * Actually ready now * FORMATTING * addressing shazzy n glorious nits * crapveyor * rm vb * small change * fixing err * shazbert nits * review changes * requested changes * more requested changes * noo * last nit fixes * restart appveyor * improving test cov * Update .golangci.yml * shazbert changes * moving pair formatting * format pair update wip * path upgrade complete * error fix * appveyor linters * more linters * remove testexch * more formatting changes * changes * shazbert changes * checking older requested changes to ensure completion * wip * fixing broken code * error fix * all fixed * additional changes * more changes * remove commented code * ftx margin api * appveyor fixes * more appveyor issues + test addition * more appveyor issues + test addition * remove unnecessary * testing * testing, fixing okex api, error fix * git merge fix * go sum * glorious changes and error fix * rm vb * more glorious changes and go mod tidy * fixed now * okex testing upgrade * old config migration and batch fetching fix * added test * glorious requested changes WIP * tested and fixed * go fmted * go fmt and test fix * additional funcs and tests for fundingRates * OKEX tested and fixed * appveyor fixes * ineff assign * 1 glorious change * error fix * typo * shazbert changes * glorious code changes and path fixing huobi WIP * adding assetType to accountinfo functions * fixing panic * panic fix and updating account info wrappers WIP * updateaccountinfo updated * testing WIP binance USDT n Coin Margined and Kraken Futures * auth functions tested and fixed * added test * config reverted * shazbert and glorious changes * shazbert and glorious changes * latest changes and portfolio update * go fmt change: * remove commented codes * improved error checking * index out of range fix * rm ln * critical nit * glorious latest changes * appveyor changes * shazbert change * easier readability * latest glorious changes * shadow dec * assetstore updated * last change * another last change * merge changes * go mod tidy * thrasher requested changes wip * improving struct layouts * appveyor go fmt * remove unnecessary code * shazbert changes * small change * oopsie * tidy * configtest reverted * error fix * oopsie * for what * test patch fix * insecurities * fixing tests * fix config
This commit is contained in:
@@ -50,8 +50,8 @@ type Gateio struct {
|
||||
// GetSymbols returns all supported symbols
|
||||
func (g *Gateio) GetSymbols() ([]string, error) {
|
||||
var result []string
|
||||
urlPath := fmt.Sprintf("%s/%s/%s", g.API.Endpoints.URLSecondary, gateioAPIVersion, gateioSymbol)
|
||||
err := g.SendHTTPRequest(urlPath, &result)
|
||||
urlPath := fmt.Sprintf("/%s/%s", gateioAPIVersion, gateioSymbol)
|
||||
err := g.SendHTTPRequest(exchange.RestSpotSupplementary, urlPath, &result)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -66,10 +66,10 @@ func (g *Gateio) GetMarketInfo() (MarketInfoResponse, error) {
|
||||
Pairs []interface{} `json:"pairs"`
|
||||
}
|
||||
|
||||
urlPath := fmt.Sprintf("%s/%s/%s", g.API.Endpoints.URLSecondary, gateioAPIVersion, gateioMarketInfo)
|
||||
urlPath := fmt.Sprintf("/%s/%s", gateioAPIVersion, gateioMarketInfo)
|
||||
var res response
|
||||
var result MarketInfoResponse
|
||||
err := g.SendHTTPRequest(urlPath, &res)
|
||||
err := g.SendHTTPRequest(exchange.RestSpotSupplementary, urlPath, &res)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
@@ -106,16 +106,16 @@ func (g *Gateio) GetLatestSpotPrice(symbol string) (float64, error) {
|
||||
// GetTicker returns a ticker for the supplied symbol
|
||||
// updated every 10 seconds
|
||||
func (g *Gateio) GetTicker(symbol string) (TickerResponse, error) {
|
||||
urlPath := fmt.Sprintf("%s/%s/%s/%s", g.API.Endpoints.URLSecondary, gateioAPIVersion, gateioTicker, symbol)
|
||||
urlPath := fmt.Sprintf("/%s/%s/%s", gateioAPIVersion, gateioTicker, symbol)
|
||||
var res TickerResponse
|
||||
return res, g.SendHTTPRequest(urlPath, &res)
|
||||
return res, g.SendHTTPRequest(exchange.RestSpotSupplementary, urlPath, &res)
|
||||
}
|
||||
|
||||
// GetTickers returns tickers for all symbols
|
||||
func (g *Gateio) GetTickers() (map[string]TickerResponse, error) {
|
||||
urlPath := fmt.Sprintf("%s/%s/%s", g.API.Endpoints.URLSecondary, gateioAPIVersion, gateioTickers)
|
||||
urlPath := fmt.Sprintf("/%s/%s", gateioAPIVersion, gateioTickers)
|
||||
resp := make(map[string]TickerResponse)
|
||||
err := g.SendHTTPRequest(urlPath, &resp)
|
||||
err := g.SendHTTPRequest(exchange.RestSpotSupplementary, urlPath, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -124,9 +124,9 @@ func (g *Gateio) GetTickers() (map[string]TickerResponse, error) {
|
||||
|
||||
// GetTrades returns trades for symbols
|
||||
func (g *Gateio) GetTrades(symbol string) (TradeHistory, error) {
|
||||
urlPath := fmt.Sprintf("%s/%s/%s/%s", g.API.Endpoints.URLSecondary, gateioAPIVersion, gateioTrades, symbol)
|
||||
urlPath := fmt.Sprintf("/%s/%s/%s", gateioAPIVersion, gateioTrades, symbol)
|
||||
var resp TradeHistory
|
||||
err := g.SendHTTPRequest(urlPath, &resp)
|
||||
err := g.SendHTTPRequest(exchange.RestSpotSupplementary, urlPath, &resp)
|
||||
if err != nil {
|
||||
return TradeHistory{}, err
|
||||
}
|
||||
@@ -135,9 +135,9 @@ func (g *Gateio) GetTrades(symbol string) (TradeHistory, error) {
|
||||
|
||||
// GetOrderbook returns the orderbook data for a suppled symbol
|
||||
func (g *Gateio) GetOrderbook(symbol string) (Orderbook, error) {
|
||||
urlPath := fmt.Sprintf("%s/%s/%s/%s", g.API.Endpoints.URLSecondary, gateioAPIVersion, gateioOrderbook, symbol)
|
||||
urlPath := fmt.Sprintf("/%s/%s/%s", gateioAPIVersion, gateioOrderbook, symbol)
|
||||
var resp OrderbookResponse
|
||||
err := g.SendHTTPRequest(urlPath, &resp)
|
||||
err := g.SendHTTPRequest(exchange.RestSpotSupplementary, urlPath, &resp)
|
||||
if err != nil {
|
||||
return Orderbook{}, err
|
||||
}
|
||||
@@ -196,8 +196,7 @@ func (g *Gateio) GetOrderbook(symbol string) (Orderbook, error) {
|
||||
|
||||
// GetSpotKline returns kline data for the most recent time period
|
||||
func (g *Gateio) GetSpotKline(arg KlinesRequestParams) (kline.Item, error) {
|
||||
urlPath := fmt.Sprintf("%s/%s/%s/%s?group_sec=%s&range_hour=%d",
|
||||
g.API.Endpoints.URLSecondary,
|
||||
urlPath := fmt.Sprintf("/%s/%s/%s?group_sec=%s&range_hour=%d",
|
||||
gateioAPIVersion,
|
||||
gateioKline,
|
||||
arg.Symbol,
|
||||
@@ -205,7 +204,7 @@ func (g *Gateio) GetSpotKline(arg KlinesRequestParams) (kline.Item, error) {
|
||||
arg.HourSize)
|
||||
|
||||
var rawKlines map[string]interface{}
|
||||
err := g.SendHTTPRequest(urlPath, &rawKlines)
|
||||
err := g.SendHTTPRequest(exchange.RestSpotSupplementary, urlPath, &rawKlines)
|
||||
if err != nil {
|
||||
return kline.Item{}, err
|
||||
}
|
||||
@@ -270,7 +269,7 @@ func (g *Gateio) GetBalances() (BalancesResponse, error) {
|
||||
var result BalancesResponse
|
||||
|
||||
return result,
|
||||
g.SendAuthenticatedHTTPRequest(http.MethodPost, gateioBalances, "", &result)
|
||||
g.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, gateioBalances, "", &result)
|
||||
}
|
||||
|
||||
// SpotNewOrder places a new order
|
||||
@@ -285,7 +284,7 @@ func (g *Gateio) SpotNewOrder(arg SpotNewOrderRequestParams) (SpotNewOrderRespon
|
||||
)
|
||||
|
||||
urlPath := fmt.Sprintf("%s/%s", gateioOrder, arg.Type)
|
||||
return result, g.SendAuthenticatedHTTPRequest(http.MethodPost, urlPath, params, &result)
|
||||
return result, g.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, urlPath, params, &result)
|
||||
}
|
||||
|
||||
// CancelExistingOrder cancels an order given the supplied orderID and symbol
|
||||
@@ -304,7 +303,7 @@ func (g *Gateio) CancelExistingOrder(orderID int64, symbol string) (bool, error)
|
||||
orderID,
|
||||
symbol,
|
||||
)
|
||||
err := g.SendAuthenticatedHTTPRequest(http.MethodPost, gateioCancelOrder, params, &result)
|
||||
err := g.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, gateioCancelOrder, params, &result)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -316,10 +315,14 @@ func (g *Gateio) CancelExistingOrder(orderID int64, symbol string) (bool, error)
|
||||
}
|
||||
|
||||
// SendHTTPRequest sends an unauthenticated HTTP request
|
||||
func (g *Gateio) SendHTTPRequest(path string, result interface{}) error {
|
||||
func (g *Gateio) SendHTTPRequest(ep exchange.URL, path string, result interface{}) error {
|
||||
endpoint, err := g.API.Endpoints.GetURL(ep)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.SendPayload(context.Background(), &request.Item{
|
||||
Method: http.MethodGet,
|
||||
Path: path,
|
||||
Path: endpoint + path,
|
||||
Result: result,
|
||||
Verbose: g.Verbose,
|
||||
HTTPDebugging: g.HTTPDebugging,
|
||||
@@ -341,7 +344,7 @@ func (g *Gateio) CancelAllExistingOrders(orderType int64, symbol string) error {
|
||||
orderType,
|
||||
symbol,
|
||||
)
|
||||
err := g.SendAuthenticatedHTTPRequest(http.MethodPost, gateioCancelAllOrders, params, &result)
|
||||
err := g.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, gateioCancelAllOrders, params, &result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -362,7 +365,7 @@ func (g *Gateio) GetOpenOrders(symbol string) (OpenOrdersResponse, error) {
|
||||
params = fmt.Sprintf("currencyPair=%s", symbol)
|
||||
}
|
||||
|
||||
err := g.SendAuthenticatedHTTPRequest(http.MethodPost, gateioOpenOrders, params, &result)
|
||||
err := g.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, gateioOpenOrders, params, &result)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
@@ -380,7 +383,7 @@ func (g *Gateio) GetTradeHistory(symbol string) (TradHistoryResponse, error) {
|
||||
var result TradHistoryResponse
|
||||
params = fmt.Sprintf("currencyPair=%s", symbol)
|
||||
|
||||
err := g.SendAuthenticatedHTTPRequest(http.MethodPost, gateioTradeHistory, params, &result)
|
||||
err := g.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, gateioTradeHistory, params, &result)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
@@ -400,12 +403,15 @@ func (g *Gateio) GenerateSignature(message string) []byte {
|
||||
|
||||
// SendAuthenticatedHTTPRequest sends authenticated requests to the Gateio API
|
||||
// To use this you must setup an APIKey and APISecret from the exchange
|
||||
func (g *Gateio) SendAuthenticatedHTTPRequest(method, endpoint, param string, result interface{}) error {
|
||||
func (g *Gateio) SendAuthenticatedHTTPRequest(ep exchange.URL, method, endpoint, param string, result interface{}) error {
|
||||
if !g.AllowAuthenticatedRequest() {
|
||||
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet,
|
||||
g.Name)
|
||||
}
|
||||
|
||||
ePoint, err := g.API.Endpoints.GetURL(ep)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
headers := make(map[string]string)
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
headers["key"] = g.API.Credentials.Key
|
||||
@@ -413,10 +419,10 @@ func (g *Gateio) SendAuthenticatedHTTPRequest(method, endpoint, param string, re
|
||||
hmac := g.GenerateSignature(param)
|
||||
headers["sign"] = crypto.HexEncodeToString(hmac)
|
||||
|
||||
urlPath := fmt.Sprintf("%s/%s/%s", g.API.Endpoints.URL, gateioAPIVersion, endpoint)
|
||||
urlPath := fmt.Sprintf("%s/%s/%s", ePoint, gateioAPIVersion, endpoint)
|
||||
|
||||
var intermidiary json.RawMessage
|
||||
err := g.SendPayload(context.Background(), &request.Item{
|
||||
err = g.SendPayload(context.Background(), &request.Item{
|
||||
Method: method,
|
||||
Path: urlPath,
|
||||
Headers: headers,
|
||||
@@ -518,7 +524,7 @@ func (g *Gateio) WithdrawCrypto(currency, address string, amount float64) (*with
|
||||
address,
|
||||
amount,
|
||||
)
|
||||
err := g.SendAuthenticatedHTTPRequest(http.MethodPost, gateioWithdraw, params, &result)
|
||||
err := g.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, gateioWithdraw, params, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -544,7 +550,7 @@ func (g *Gateio) GetCryptoDepositAddress(currency string) (string, error) {
|
||||
params := fmt.Sprintf("currency=%s",
|
||||
currency)
|
||||
|
||||
err := g.SendAuthenticatedHTTPRequest(http.MethodPost, gateioDepositAddress, params, &result)
|
||||
err := g.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, gateioDepositAddress, params, &result)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -285,7 +285,8 @@ func TestFormatWithdrawPermissions(t *testing.T) {
|
||||
|
||||
func TestGetActiveOrders(t *testing.T) {
|
||||
var getOrdersRequest = order.GetOrdersRequest{
|
||||
Type: order.AnyType,
|
||||
Type: order.AnyType,
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
_, err := g.GetActiveOrders(&getOrdersRequest)
|
||||
@@ -298,7 +299,8 @@ func TestGetActiveOrders(t *testing.T) {
|
||||
|
||||
func TestGetOrderHistory(t *testing.T) {
|
||||
var getOrdersRequest = order.GetOrdersRequest{
|
||||
Type: order.AnyType,
|
||||
Type: order.AnyType,
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
currPair := currency.NewPair(currency.LTC, currency.BTC)
|
||||
@@ -398,12 +400,12 @@ func TestCancelAllExchangeOrders(t *testing.T) {
|
||||
|
||||
func TestGetAccountInfo(t *testing.T) {
|
||||
if apiSecret == "" || apiKey == "" {
|
||||
_, err := g.UpdateAccountInfo()
|
||||
_, err := g.UpdateAccountInfo(asset.Spot)
|
||||
if err == nil {
|
||||
t.Error("GetAccountInfo() Expected error")
|
||||
}
|
||||
} else {
|
||||
_, err := g.UpdateAccountInfo()
|
||||
_, err := g.UpdateAccountInfo(asset.Spot)
|
||||
if err != nil {
|
||||
t.Error("GetAccountInfo() error", err)
|
||||
}
|
||||
|
||||
@@ -127,12 +127,15 @@ func (g *Gateio) SetDefaults() {
|
||||
}
|
||||
g.Requester = request.New(g.Name,
|
||||
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
|
||||
|
||||
g.API.Endpoints.URLDefault = gateioTradeURL
|
||||
g.API.Endpoints.URL = g.API.Endpoints.URLDefault
|
||||
g.API.Endpoints.URLSecondaryDefault = gateioMarketURL
|
||||
g.API.Endpoints.URLSecondary = g.API.Endpoints.URLSecondaryDefault
|
||||
g.API.Endpoints.WebsocketURL = gateioWebsocketEndpoint
|
||||
g.API.Endpoints = g.NewEndpoints()
|
||||
err = g.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
|
||||
exchange.RestSpot: gateioTradeURL,
|
||||
exchange.RestSpotSupplementary: gateioMarketURL,
|
||||
exchange.WebsocketSpot: gateioWebsocketEndpoint,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorln(log.ExchangeSys, err)
|
||||
}
|
||||
g.Websocket = stream.New()
|
||||
g.WebsocketResponseMaxLimit = exchange.DefaultWebsocketResponseMaxLimit
|
||||
g.WebsocketResponseCheckTimeout = exchange.DefaultWebsocketResponseCheckTimeout
|
||||
@@ -151,6 +154,11 @@ func (g *Gateio) Setup(exch *config.ExchangeConfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
wsRunningURL, err := g.API.Endpoints.GetURL(exchange.WebsocketSpot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = g.Websocket.Setup(&stream.WebsocketSetup{
|
||||
Enabled: exch.Features.Enabled.Websocket,
|
||||
Verbose: exch.Verbose,
|
||||
@@ -158,7 +166,7 @@ func (g *Gateio) Setup(exch *config.ExchangeConfig) error {
|
||||
WebsocketTimeout: exch.WebsocketTrafficTimeout,
|
||||
DefaultURL: gateioWebsocketEndpoint,
|
||||
ExchangeName: exch.Name,
|
||||
RunningURL: exch.API.Endpoints.WebsocketURL,
|
||||
RunningURL: wsRunningURL,
|
||||
Connector: g.WsConnect,
|
||||
Subscriber: g.Subscribe,
|
||||
GenerateSubscriptions: g.GenerateDefaultSubscriptions,
|
||||
@@ -316,7 +324,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() (account.Holdings, error) {
|
||||
func (g *Gateio) UpdateAccountInfo(assetType asset.Item) (account.Holdings, error) {
|
||||
var info account.Holdings
|
||||
var balances []account.Balance
|
||||
|
||||
@@ -401,10 +409,10 @@ func (g *Gateio) UpdateAccountInfo() (account.Holdings, error) {
|
||||
}
|
||||
|
||||
// FetchAccountInfo retrieves balances for all enabled currencies
|
||||
func (g *Gateio) FetchAccountInfo() (account.Holdings, error) {
|
||||
acc, err := account.GetHoldings(g.Name)
|
||||
func (g *Gateio) FetchAccountInfo(assetType asset.Item) (account.Holdings, error) {
|
||||
acc, err := account.GetHoldings(g.Name, assetType)
|
||||
if err != nil {
|
||||
return g.UpdateAccountInfo()
|
||||
return g.UpdateAccountInfo(assetType)
|
||||
}
|
||||
|
||||
return acc, nil
|
||||
@@ -802,8 +810,8 @@ func (g *Gateio) AuthenticateWebsocket() error {
|
||||
|
||||
// ValidateCredentials validates current credentials used for wrapper
|
||||
// functionality
|
||||
func (g *Gateio) ValidateCredentials() error {
|
||||
_, err := g.UpdateAccountInfo()
|
||||
func (g *Gateio) ValidateCredentials(assetType asset.Item) error {
|
||||
_, err := g.UpdateAccountInfo(assetType)
|
||||
return g.CheckTransientError(err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user