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:
Adam
2021-02-12 16:19:18 +11:00
committed by GitHub
parent e9bd2ad4d8
commit 504c2fad6d
169 changed files with 227754 additions and 31776 deletions

View File

@@ -57,7 +57,7 @@ func (z *ZB) SpotNewOrder(arg SpotNewOrderRequestParams) (int64, error) {
vals.Set("price", strconv.FormatFloat(arg.Price, 'f', -1, 64))
vals.Set("tradeType", string(arg.Type))
err := z.SendAuthenticatedHTTPRequest(http.MethodGet, vals, &result, request.Auth)
err := z.SendAuthenticatedHTTPRequest(exchange.RestSpotSupplementary, http.MethodGet, vals, &result, request.Auth)
if err != nil {
return 0, err
}
@@ -85,7 +85,7 @@ func (z *ZB) CancelExistingOrder(orderID int64, symbol string) error {
vals.Set("currency", symbol)
var result response
err := z.SendAuthenticatedHTTPRequest(http.MethodGet, vals, &result, request.Auth)
err := z.SendAuthenticatedHTTPRequest(exchange.RestSpotSupplementary, http.MethodGet, vals, &result, request.Auth)
if err != nil {
return err
}
@@ -105,7 +105,7 @@ func (z *ZB) GetAccountInformation() (AccountsResponse, error) {
vals.Set("accesskey", z.API.Credentials.Key)
vals.Set("method", "getAccountInfo")
return result, z.SendAuthenticatedHTTPRequest(http.MethodGet, vals, &result, request.Auth)
return result, z.SendAuthenticatedHTTPRequest(exchange.RestSpotSupplementary, http.MethodGet, vals, &result, request.Auth)
}
// GetUnfinishedOrdersIgnoreTradeType returns unfinished orders
@@ -118,7 +118,7 @@ func (z *ZB) GetUnfinishedOrdersIgnoreTradeType(currency string, pageindex, page
vals.Set("pageIndex", strconv.FormatInt(pageindex, 10))
vals.Set("pageSize", strconv.FormatInt(pagesize, 10))
err := z.SendAuthenticatedHTTPRequest(http.MethodGet, vals, &result, request.Auth)
err := z.SendAuthenticatedHTTPRequest(exchange.RestSpotSupplementary, http.MethodGet, vals, &result, request.Auth)
return result, err
}
@@ -131,16 +131,16 @@ func (z *ZB) GetOrders(currency string, pageindex, side int64) ([]Order, error)
vals.Set("currency", currency)
vals.Set("pageIndex", strconv.FormatInt(pageindex, 10))
vals.Set("tradeType", strconv.FormatInt(side, 10))
return response, z.SendAuthenticatedHTTPRequest(http.MethodGet, vals, &response, request.Auth)
return response, z.SendAuthenticatedHTTPRequest(exchange.RestSpotSupplementary, http.MethodGet, vals, &response, request.Auth)
}
// GetMarkets returns market information including pricing, symbols and
// each symbols decimal precision
func (z *ZB) GetMarkets() (map[string]MarketResponseItem, error) {
endpoint := fmt.Sprintf("%s/%s/%s/%s", z.API.Endpoints.URL, zbData, zbAPIVersion, zbMarkets)
endpoint := fmt.Sprintf("/%s/%s/%s", zbData, zbAPIVersion, zbMarkets)
var res map[string]MarketResponseItem
err := z.SendHTTPRequest(endpoint, &res, request.UnAuth)
err := z.SendHTTPRequest(exchange.RestSpot, endpoint, &res, request.UnAuth)
if err != nil {
return nil, err
}
@@ -164,34 +164,34 @@ func (z *ZB) GetLatestSpotPrice(symbol string) (float64, error) {
// GetTicker returns a ticker for a given symbol
func (z *ZB) GetTicker(symbol string) (TickerResponse, error) {
urlPath := fmt.Sprintf("%s/%s/%s/%s?market=%s", z.API.Endpoints.URL, zbData, zbAPIVersion, zbTicker, symbol)
urlPath := fmt.Sprintf("/%s/%s/%s?market=%s", zbData, zbAPIVersion, zbTicker, symbol)
var res TickerResponse
err := z.SendHTTPRequest(urlPath, &res, request.UnAuth)
err := z.SendHTTPRequest(exchange.RestSpot, urlPath, &res, request.UnAuth)
return res, err
}
// GetTrades returns trades for a given symbol
func (z *ZB) GetTrades(symbol string) (TradeHistory, error) {
urlPath := fmt.Sprintf("%s/%s/%s/%s?market=%s", z.API.Endpoints.URL, zbData, zbAPIVersion, zbTrades, symbol)
urlPath := fmt.Sprintf("/%s/%s/%s?market=%s", zbData, zbAPIVersion, zbTrades, symbol)
var res TradeHistory
err := z.SendHTTPRequest(urlPath, &res, request.UnAuth)
err := z.SendHTTPRequest(exchange.RestSpot, urlPath, &res, request.UnAuth)
return res, err
}
// GetTickers returns ticker data for all supported symbols
func (z *ZB) GetTickers() (map[string]TickerChildResponse, error) {
urlPath := fmt.Sprintf("%s/%s/%s/%s", z.API.Endpoints.URL, zbData, zbAPIVersion, zbTickers)
urlPath := fmt.Sprintf("/%s/%s/%s", zbData, zbAPIVersion, zbTickers)
resp := make(map[string]TickerChildResponse)
err := z.SendHTTPRequest(urlPath, &resp, request.UnAuth)
err := z.SendHTTPRequest(exchange.RestSpot, urlPath, &resp, request.UnAuth)
return resp, err
}
// GetOrderbook returns the orderbook for a given symbol
func (z *ZB) GetOrderbook(symbol string) (OrderbookResponse, error) {
urlPath := fmt.Sprintf("%s/%s/%s/%s?market=%s", z.API.Endpoints.URL, zbData, zbAPIVersion, zbDepth, symbol)
urlPath := fmt.Sprintf("/%s/%s/%s?market=%s", zbData, zbAPIVersion, zbDepth, symbol)
var res OrderbookResponse
err := z.SendHTTPRequest(urlPath, &res, request.UnAuth)
err := z.SendHTTPRequest(exchange.RestSpot, urlPath, &res, request.UnAuth)
if err != nil {
return res, err
}
@@ -226,11 +226,11 @@ func (z *ZB) GetSpotKline(arg KlinesRequestParams) (KLineResponse, error) {
vals.Set("size", fmt.Sprintf("%d", arg.Size))
}
urlPath := fmt.Sprintf("%s/%s/%s/%s?%s", z.API.Endpoints.URL, zbData, zbAPIVersion, zbKline, vals.Encode())
urlPath := fmt.Sprintf("/%s/%s/%s?%s", zbData, zbAPIVersion, zbKline, vals.Encode())
var res KLineResponse
var rawKlines map[string]interface{}
err := z.SendHTTPRequest(urlPath, &rawKlines, klineFunc)
err := z.SendHTTPRequest(exchange.RestSpot, urlPath, &rawKlines, klineFunc)
if err != nil {
return res, err
}
@@ -277,14 +277,18 @@ func (z *ZB) GetCryptoAddress(currency currency.Code) (UserAddress, error) {
vals.Set("currency", currency.Lower().String())
return resp,
z.SendAuthenticatedHTTPRequest(http.MethodGet, vals, &resp, request.Auth)
z.SendAuthenticatedHTTPRequest(exchange.RestSpotSupplementary, http.MethodGet, vals, &resp, request.Auth)
}
// SendHTTPRequest sends an unauthenticated HTTP request
func (z *ZB) SendHTTPRequest(path string, result interface{}, f request.EndpointLimit) error {
func (z *ZB) SendHTTPRequest(ep exchange.URL, path string, result interface{}, f request.EndpointLimit) error {
endpoint, err := z.API.Endpoints.GetURL(ep)
if err != nil {
return err
}
return z.SendPayload(context.Background(), &request.Item{
Method: http.MethodGet,
Path: path,
Path: endpoint + path,
Result: result,
Verbose: z.Verbose,
HTTPDebugging: z.HTTPDebugging,
@@ -294,11 +298,14 @@ func (z *ZB) SendHTTPRequest(path string, result interface{}, f request.Endpoint
}
// SendAuthenticatedHTTPRequest sends authenticated requests to the zb API
func (z *ZB) SendAuthenticatedHTTPRequest(httpMethod string, params url.Values, result interface{}, f request.EndpointLimit) error {
func (z *ZB) SendAuthenticatedHTTPRequest(ep exchange.URL, httpMethod string, params url.Values, result interface{}, f request.EndpointLimit) error {
if !z.AllowAuthenticatedRequest() {
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, z.Name)
}
endpoint, err := z.API.Endpoints.GetURL(ep)
if err != nil {
return err
}
params.Set("accesskey", z.API.Credentials.Key)
hmac := crypto.GetHMAC(crypto.HashMD5,
@@ -310,7 +317,7 @@ func (z *ZB) SendAuthenticatedHTTPRequest(httpMethod string, params url.Values,
params.Set("sign", fmt.Sprintf("%x", hmac))
urlPath := fmt.Sprintf("%s/%s?%s",
z.API.Endpoints.URLSecondary,
endpoint,
params.Get("method"),
params.Encode())
@@ -324,7 +331,7 @@ func (z *ZB) SendAuthenticatedHTTPRequest(httpMethod string, params url.Values,
// Expiry of timestamp doesn't appear to be documented, so making a reasonable assumption
ctx, cancel := context.WithDeadline(context.Background(), now.Add(15*time.Second))
defer cancel()
err := z.SendPayload(ctx, &request.Item{
err = z.SendPayload(ctx, &request.Item{
Method: httpMethod,
Path: urlPath,
Body: strings.NewReader(""),
@@ -433,7 +440,7 @@ func (z *ZB) Withdraw(currency, address, safepassword string, amount, fees float
vals.Set("safePwd", safepassword)
var resp response
err := z.SendAuthenticatedHTTPRequest(http.MethodGet, vals, &resp, request.Auth)
err := z.SendAuthenticatedHTTPRequest(exchange.RestSpotSupplementary, http.MethodGet, vals, &resp, request.Auth)
if err != nil {
return "", err
}

View File

@@ -34,7 +34,7 @@ func TestMain(m *testing.M) {
if err != nil {
log.Fatal("ZB setup error", err)
}
log.Printf(sharedtestvalues.LiveTesting, z.Name, z.API.Endpoints.URL)
log.Printf(sharedtestvalues.LiveTesting, z.Name)
z.Websocket.DataHandler = sharedtestvalues.GetWebsocketInterfaceChannelOverride()
z.Websocket.TrafficAlert = sharedtestvalues.GetWebsocketStructChannelOverride()
os.Exit(m.Run())

View File

@@ -45,11 +45,17 @@ func TestMain(m *testing.M) {
if err != nil {
log.Fatalf("Mock server error %s", err)
}
z.HTTPClient = newClient
z.API.Endpoints.URL = serverDetails
endpoints := z.API.Endpoints.GetURLMap()
for k := range endpoints {
err = z.API.Endpoints.SetRunning(k, serverDetails)
if err != nil {
log.Fatal(err)
}
}
log.Printf(sharedtestvalues.MockTesting,
z.Name,
z.API.Endpoints.URL)
z.Name)
os.Exit(m.Run())
}

View File

@@ -244,6 +244,7 @@ func TestGetActiveOrders(t *testing.T) {
Type: order.AnyType,
Pairs: []currency.Pair{currency.NewPair(currency.XRP,
currency.USDT)},
AssetType: asset.Spot,
}
_, err := z.GetActiveOrders(&getOrdersRequest)
@@ -259,8 +260,9 @@ func TestGetOrderHistory(t *testing.T) {
t.Skip("skipping authenticated function for mock testing")
}
var getOrdersRequest = order.GetOrdersRequest{
Type: order.AnyType,
Side: order.Buy,
Type: order.AnyType,
Side: order.Buy,
AssetType: asset.Spot,
Pairs: []currency.Pair{currency.NewPair(currency.LTC,
currency.BTC)},
}
@@ -368,12 +370,12 @@ func TestGetAccountInfo(t *testing.T) {
t.Skip("skipping authenticated function for mock testing")
}
if z.ValidateAPICredentials() {
_, err := z.UpdateAccountInfo()
_, err := z.UpdateAccountInfo(asset.Spot)
if err != nil {
t.Error("GetAccountInfo() error", err)
}
} else {
_, err := z.UpdateAccountInfo()
_, err := z.UpdateAccountInfo(asset.Spot)
if err == nil {
t.Error("GetAccountInfo() Expected error")
}

View File

@@ -131,12 +131,15 @@ func (z *ZB) SetDefaults() {
z.Requester = request.New(z.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
z.API.Endpoints.URLDefault = zbTradeURL
z.API.Endpoints.URL = z.API.Endpoints.URLDefault
z.API.Endpoints.URLSecondaryDefault = zbMarketURL
z.API.Endpoints.URLSecondary = z.API.Endpoints.URLSecondaryDefault
z.API.Endpoints.WebsocketURL = zbWebsocketAPI
z.API.Endpoints = z.NewEndpoints()
err = z.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: zbTradeURL,
exchange.RestSpotSupplementary: zbMarketURL,
exchange.WebsocketSpot: zbWebsocketAPI,
})
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
z.Websocket = stream.New()
z.WebsocketResponseMaxLimit = exchange.DefaultWebsocketResponseMaxLimit
z.WebsocketResponseCheckTimeout = exchange.DefaultWebsocketResponseCheckTimeout
@@ -154,6 +157,11 @@ func (z *ZB) Setup(exch *config.ExchangeConfig) error {
return err
}
wsRunningURL, err := z.API.Endpoints.GetURL(exchange.WebsocketSpot)
if err != nil {
return err
}
err = z.Websocket.Setup(&stream.WebsocketSetup{
Enabled: exch.Features.Enabled.Websocket,
Verbose: exch.Verbose,
@@ -161,7 +169,7 @@ func (z *ZB) Setup(exch *config.ExchangeConfig) error {
WebsocketTimeout: exch.WebsocketTrafficTimeout,
DefaultURL: zbWebsocketAPI,
ExchangeName: exch.Name,
RunningURL: exch.API.Endpoints.WebsocketURL,
RunningURL: wsRunningURL,
Connector: z.WsConnect,
GenerateSubscriptions: z.GenerateDefaultSubscriptions,
Subscriber: z.Subscribe,
@@ -330,7 +338,7 @@ func (z *ZB) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.
// UpdateAccountInfo retrieves balances for all enabled currencies for the
// ZB exchange
func (z *ZB) UpdateAccountInfo() (account.Holdings, error) {
func (z *ZB) UpdateAccountInfo(assetType asset.Item) (account.Holdings, error) {
var info account.Holdings
var balances []account.Balance
var coins []AccountsResponseCoin
@@ -380,10 +388,10 @@ func (z *ZB) UpdateAccountInfo() (account.Holdings, error) {
}
// FetchAccountInfo retrieves balances for all enabled currencies
func (z *ZB) FetchAccountInfo() (account.Holdings, error) {
acc, err := account.GetHoldings(z.Name)
func (z *ZB) FetchAccountInfo(assetType asset.Item) (account.Holdings, error) {
acc, err := account.GetHoldings(z.Name, assetType)
if err != nil {
return z.UpdateAccountInfo()
return z.UpdateAccountInfo(assetType)
}
return acc, nil
@@ -805,8 +813,8 @@ func (z *ZB) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, error
// ValidateCredentials validates current credentials used for wrapper
// functionality
func (z *ZB) ValidateCredentials() error {
_, err := z.UpdateAccountInfo()
func (z *ZB) ValidateCredentials(assetType asset.Item) error {
_, err := z.UpdateAccountInfo(assetType)
return z.CheckTransientError(err)
}