exchanges/qa: Add exchange wrapper testing suite (#1159)

* initial concept of a nice validation tester for exchanges

* adds some datahandler design

* expand testing

* more tests and fixes

* minor end of day fix for bithumb

* fixes implementation issues

* more test coverage and improvements, but not sure if i should continue

* fix more wrapper implementations

* adds error type, more fixes

* changes signature, fixes implementations

* fixes more wrapper implementations

* one more bit

* more cleanup

* WOW things work?

* lintle 1/1337

* mini bump

* fixes all linting

* neaten

* GetOrderInfo+ asset pair fixes+improvements

* adds new websocket test

* expand ws testing

* fix bug, expand tests, improve implementation

* code coverage of a lot of new codes

* fixes everything

* reverts accidental changes

* minor fixes from reviewing code

* removes Bitfinex cancelBatchOrder implementation

* fixes dumb baby typo for babies

* mini nit fixes

* so many nits to address

* addresses all the nits

* Titlecase

* switcheroo

* removes websocket testing for now

* fix appveyor, minor test fix

* fixes typo, re-kindles killed kode

* skip binance wrapper tests when running CI

* expired context, huobi okx fixes

* kodespull

* fix ordering

* time fix because why not

* fix exmo, others

* hopefully this fixes all of my life's problems

* last thing today

* huobi, more like hypotrophy

* golangci-lint, more like mypooroldknee-splint

* fix huobi times by removing them

* should fix okx currency issues

* blocks the application

* adds last little contingency for pairs

* addresses most nits and new problems

* lovely fixed before seeing why okx sucks

* fixes issues with okx websocket

* the classic receieieivaier

* lintle

* adds test and fixes existing tests

* expands error handling messages during setup

* fixes dumb okx bugs introduced

* quick fix for lint and exmo

* fixes nixes

* fix exmo deposit issue

* lint

* fixes issue with extra asset runs missing

* fix surprise race

* all the lint and merge fixes

* fixes surprise bugs in OKx

* fixes issues with times and chains

* fixing all the merge stuff

* merge fix

* rm logs and a panic potential

* lovely lint lament

* an easy demonstration of scenario, but not of initial purpose

* put it in the bin

* Revert "put it in the bin"

This reverts commit 15c6490f713233d43f10957367fcbf18e3818bdd.

* re-add after immediate error popup

* fix mini poor test design

* okx okay

* merge fixes

* fixes issues discovered in lovely test

* I FORGOT TO COMMIT THIS

* nit fixaroonaboo

* forgoetten test fix

* revert old okx asset intrument work

* fixes

* revert problems I didnt understand. update bybit

* fix merge bugs

* test cleanup

* further improvements

* reshuffle and lint

* rm redundant CI_TEST by rm the CI_TEST field that is redundant

* path fix

* move to its own section, dont run on 32 bit + appveyor

* lint

* fix lbank

* address nits

* let it rip

* fix failing test time range

* niteroo boogaloo

* mod tidy, use common.SimpleTimeFormat
This commit is contained in:
Scott
2023-07-03 11:09:43 +10:00
committed by GitHub
parent ef605a3c19
commit fcc5ad4551
210 changed files with 38548 additions and 6519 deletions

View File

@@ -11,10 +11,12 @@ import (
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
)
@@ -34,9 +36,12 @@ const (
zbDepth = "depth"
zbUnfinishedOrdersIgnoreTradeType = "getUnfinishedOrdersIgnoreTradeType"
zbGetOrdersGet = "getOrders"
zbGetOrder = "getOrder"
zbWithdraw = "withdraw"
zbDepositAddress = "getUserAddress"
zbMultiChainDepositAddress = "getPayinAddress"
zbWithdrawalRecords = "getWithdrawRecord"
zbDepositRecords = "getChargeRecord"
)
// ZB is the overarching type across this package
@@ -68,7 +73,7 @@ func (z *ZB) SpotNewOrder(ctx context.Context, arg SpotNewOrderRequestParams) (i
return 0, err
}
if result.Code != 1000 {
return 0, fmt.Errorf("unsuccessful new order, message: %s code: %d", result.Message, result.Code)
return 0, fmt.Errorf("%w unsuccessful new order, message: %s code: %d", request.ErrAuthRequestFailed, result.Message, result.Code)
}
newOrderID, err := strconv.ParseInt(result.ID, 10, 64)
if err != nil {
@@ -77,6 +82,38 @@ func (z *ZB) SpotNewOrder(ctx context.Context, arg SpotNewOrderRequestParams) (i
return newOrderID, nil
}
// GetDepositRecords returns the deposit records
func (z *ZB) GetDepositRecords(ctx context.Context, arg *WalletRecordsRequest) (*DepositRecordsResponse, error) {
if arg == nil {
return nil, fmt.Errorf("%w WalletRecordsRequest", common.ErrNilPointer)
}
var resp DepositRecordsResponse
vals := url.Values{}
vals.Set("method", "getChargeRecord")
vals.Set("currency", arg.Currency.String())
if arg.PageSize > 0 {
vals.Set("pageIndex", strconv.FormatInt(arg.PageIndex, 10))
}
if arg.PageIndex > 0 {
vals.Set("pageSize", strconv.FormatInt(arg.PageSize, 10))
}
return &resp, z.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpotSupplementary, http.MethodGet, vals, &resp, request.Auth)
}
// GetWithdrawalRecords returns the withdrawal records
func (z *ZB) GetWithdrawalRecords(ctx context.Context, arg *WalletRecordsRequest) (*WithdrawalRecordsResponse, error) {
if arg == nil {
return nil, fmt.Errorf("%w WalletRecordsRequest", common.ErrNilPointer)
}
var resp WithdrawalRecordsResponse
vals := url.Values{}
vals.Set("method", "getWithdrawRecord")
vals.Set("currency", arg.Currency.String())
vals.Set("pageIndex", strconv.FormatInt(arg.PageIndex, 10))
vals.Set("pageSize", strconv.FormatInt(arg.PageSize, 10))
return &resp, z.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpotSupplementary, http.MethodGet, vals, &resp, request.Auth)
}
// CancelExistingOrder cancels an order
func (z *ZB) CancelExistingOrder(ctx context.Context, orderID int64, symbol string) error {
creds, err := z.GetCredentials(ctx)
@@ -102,7 +139,7 @@ func (z *ZB) CancelExistingOrder(ctx context.Context, orderID int64, symbol stri
}
if result.Code != 1000 {
return errors.New(result.Message)
return fmt.Errorf("%w %v", request.ErrAuthRequestFailed, result.Message)
}
return nil
}
@@ -137,8 +174,7 @@ func (z *ZB) GetUnfinishedOrdersIgnoreTradeType(ctx context.Context, currency st
vals.Set("pageIndex", strconv.FormatInt(pageindex, 10))
vals.Set("pageSize", strconv.FormatInt(pagesize, 10))
err = z.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpotSupplementary, http.MethodGet, vals, &result, request.Auth)
return result, err
return result, z.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpotSupplementary, http.MethodGet, vals, &result, request.Auth)
}
// GetOrders returns finished orders
@@ -157,6 +193,32 @@ func (z *ZB) GetOrders(ctx context.Context, currency string, pageindex, side int
return response, z.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpotSupplementary, http.MethodGet, vals, &response, request.Auth)
}
// GetSingleOrder Get single buy order or sell order
func (z *ZB) GetSingleOrder(ctx context.Context, orderID, customerOrderID string, currency currency.Pair) (*Order, error) {
creds, err := z.GetCredentials(ctx)
if err != nil {
return nil, err
}
var response Order
pFmt, err := z.GetPairFormat(asset.Spot, true)
if err != nil {
return nil, err
}
vals := url.Values{}
vals.Set("accesskey", creds.Key)
vals.Set("method", zbGetOrder)
vals.Set("currency", pFmt.Format(currency))
if orderID != "" {
vals.Set("id", orderID)
}
if customerOrderID != "" {
vals.Set("customerOrderId", customerOrderID)
}
return &response, z.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpotSupplementary, http.MethodGet, vals, &response, request.Auth)
}
// GetMarkets returns market information including pricing, symbols and
// each symbols decimal precision
func (z *ZB) GetMarkets(ctx context.Context) (map[string]MarketResponseItem, error) {
@@ -360,7 +422,7 @@ func (z *ZB) SendHTTPRequest(ctx context.Context, ep exchange.URL, path string,
return z.SendPayload(ctx, f, func() (*request.Item, error) {
return item, nil
})
}, request.UnauthenticatedRequest)
}
// SendAuthenticatedHTTPRequest sends authenticated requests to the zb API
@@ -401,14 +463,13 @@ func (z *ZB) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.URL,
Method: httpMethod,
Path: urlPath,
Result: &intermediary,
AuthRequest: true,
Verbose: z.Verbose,
HTTPDebugging: z.HTTPDebugging,
HTTPRecording: z.HTTPRecording,
}, nil
}
err = z.SendPayload(ctx, f, newRequest)
err = z.SendPayload(ctx, f, newRequest, request.AuthenticatedRequest)
if err != nil {
return err
}
@@ -421,7 +482,8 @@ func (z *ZB) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.URL,
err = json.Unmarshal(intermediary, &errCap)
if err == nil {
if errCap.Code > 1000 {
return fmt.Errorf("error code: %d error code message: %s error message: %s",
return fmt.Errorf("%w error code: %d error code message: %s error message: %s",
request.ErrAuthRequestFailed,
errCap.Code,
errorCode[errCap.Code],
errCap.Message)
@@ -523,7 +585,7 @@ func (z *ZB) Withdraw(ctx context.Context, currency, address, safepassword strin
return "", err
}
if resp.Code != 1000 {
return "", errors.New(resp.Message)
return "", fmt.Errorf("%w %v", request.ErrAuthRequestFailed, resp.Message)
}
return resp.ID, nil

View File

@@ -243,7 +243,7 @@ func TestGetActiveOrders(t *testing.T) {
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
var getOrdersRequest = order.GetOrdersRequest{
var getOrdersRequest = order.MultiOrderRequest{
Type: order.AnyType,
Pairs: []currency.Pair{currency.NewPair(currency.XRP, currency.USDT)},
AssetType: asset.Spot,
@@ -263,7 +263,7 @@ func TestGetOrderHistory(t *testing.T) {
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
var getOrdersRequest = order.GetOrdersRequest{
var getOrdersRequest = order.MultiOrderRequest{
Type: order.AnyType,
Side: order.Buy,
AssetType: asset.Spot,
@@ -890,7 +890,7 @@ func TestGetSpotKline(t *testing.T) {
func TestGetHistoricCandles(t *testing.T) {
t.Parallel()
if mockTests {
t.Skip("mock testing is not supported for this function")
t.Skip("skipping test, mock response cannot be implemented")
}
currencyPair, err := currency.NewPairFromString(testCurrency)
if err != nil {
@@ -907,6 +907,9 @@ func TestGetHistoricCandles(t *testing.T) {
func TestGetHistoricCandlesExtended(t *testing.T) {
t.Parallel()
if mockTests {
t.Skip("skipping test, mock response cannot be implemented")
}
currencyPair, err := currency.NewPairFromString(testCurrency)
if err != nil {
t.Fatal(err)
@@ -921,9 +924,6 @@ func TestGetHistoricCandlesExtended(t *testing.T) {
startTime = time.Now().Add(-time.Hour * 24 * 365)
endTime = time.Now()
if mockTests {
t.Skip("mock testing is not supported for this function")
}
_, err = z.GetHistoricCandlesExtended(context.Background(),
currencyPair, asset.Spot, kline.OneDay, startTime, endTime)
if err != nil {
@@ -1055,3 +1055,67 @@ func TestGetAvailableTransferChains(t *testing.T) {
t.Error("expected 3 results")
}
}
func TestGetDepositRecords(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, z, canManipulateRealOrders)
_, err := z.GetDepositRecords(context.Background(), &WalletRecordsRequest{
Currency: currency.BTC,
})
if err != nil {
t.Error(err)
}
}
func TestGetWithdrawalRecords(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, z, canManipulateRealOrders)
_, err := z.GetWithdrawalRecords(context.Background(), &WalletRecordsRequest{
Currency: currency.BTC,
})
if err != nil {
t.Error(err)
}
}
func TestGetSingleOrder(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, z, canManipulateRealOrders)
_, err := z.GetSingleOrder(context.Background(), "1337", "", currency.NewPair(currency.BTC, currency.USDT))
if err != nil {
t.Error(err)
}
}
func TestGetAccountFundingHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, z, canManipulateRealOrders)
_, err := z.GetAccountFundingHistory(context.Background())
if err != nil {
t.Error(err)
}
}
func TestGetWithdrawalsHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, z, canManipulateRealOrders)
_, err := z.GetWithdrawalsHistory(context.Background(), currency.BTC, asset.Spot)
if err != nil {
t.Error(err)
}
}
func TestGetOrderInfo(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, z, canManipulateRealOrders)
_, err := z.GetOrderInfo(context.Background(), "1234", currency.NewPair(currency.BTC, currency.USDT), asset.Spot)
if err != nil {
t.Error(err)
}
}

View File

@@ -36,12 +36,12 @@ type AccountsBaseResponse struct {
// Order is the order details for retrieving all orders
type Order struct {
Currency string `json:"currency"`
ID int64 `json:"id,string"`
ID string `json:"id"`
Price float64 `json:"price"`
Status int `json:"status"`
Status int64 `json:"status"`
TotalAmount float64 `json:"total_amount"`
TradeAmount float64 `json:"trade_amount"`
TradeDate int `json:"trade_date"`
TradeDate int64 `json:"trade_date"`
TradeMoney float64 `json:"trade_money"`
Type int64 `json:"type"`
Fees float64 `json:"fees,omitempty"`
@@ -108,8 +108,6 @@ type SpotNewOrderResponse struct {
ID string `json:"id"` // 委托挂单号
}
// //-------------Kline
// KlinesRequestParams represents Klines request data.
type KlinesRequestParams struct {
Symbol string // 交易对, zb_qc,zb_usdt,zb_btc...
@@ -259,3 +257,51 @@ type TradeHistory []struct {
TradeType string `json:"trade_type"`
Type string `json:"type"`
}
// WalletRecordsRequest hold request params
type WalletRecordsRequest struct {
Currency currency.Code `json:"method"`
PageIndex int64 `json:"pageIndex"`
PageSize int64 `json:"pageSize"`
}
// DepositRecordsResponse holds response data
type DepositRecordsResponse struct {
List []DepositRecord `json:"list"`
PageIndex int64 `json:"pageIndex"`
PageSize int64 `json:"pageSize"`
Total int64 `json:"total"`
}
// DepositRecord holds details of a deposit
type DepositRecord struct {
Address string `json:"address"`
Amount float64 `json:"amount,string"`
ConfirmTimes int64 `json:"confirmTimes"`
Currency string `json:"currency"`
Description string `json:"description"`
Hash string `json:"hash"`
ID int64 `json:"id"`
InternalTransfer int64 `json:"itransfer"`
Status int64 `json:"status"`
SubmitTime int64 `json:"submit_time,string"`
}
// WithdrawalRecordsResponse holds response data
type WithdrawalRecordsResponse struct {
List []WithdrawalRecord `json:"list"`
PageIndex int64 `json:"pageIndex"`
PageSize int64 `json:"pageSize"`
Total int64 `json:"total"`
}
// WithdrawalRecord holds details of a withdrawal
type WithdrawalRecord struct {
Amount float64 `json:"amount"`
Fees float64 `json:"fees"`
ID int64 `json:"id"`
ManageTime int64 `json:"manageTime"`
Status int64 `json:"status"`
SubmitTime int64 `json:"submitTime"`
ToAddress string `json:"toAddress"`
}

View File

@@ -138,11 +138,11 @@ func (z *ZB) wsHandleData(respRaw []byte) error {
for i := range depth.Asks {
amt, ok := depth.Asks[i][1].(float64)
if !ok {
return errors.New("unable to type assert ask amount")
return common.GetTypeAssertError("float64", depth.Asks[i][1], "ask amount")
}
price, ok := depth.Asks[i][0].(float64)
if !ok {
return errors.New("unable to type assert ask price")
return common.GetTypeAssertError("float64", depth.Asks[i][0], "ask price")
}
book.Asks[i] = orderbook.Item{
Amount: amt,
@@ -152,11 +152,11 @@ func (z *ZB) wsHandleData(respRaw []byte) error {
for i := range depth.Bids {
amt, ok := depth.Bids[i][1].(float64)
if !ok {
return errors.New("unable to type assert bid amount")
return common.GetTypeAssertError("float64", depth.Bids[i][1], "bid amount")
}
price, ok := depth.Bids[i][0].(float64)
if !ok {
return errors.New("unable to type assert bid price")
return common.GetTypeAssertError("float64", depth.Bids[i][0], "bid price")
}
book.Bids[i] = orderbook.Item{
Amount: amt,

View File

@@ -2,7 +2,6 @@ package zb
import (
"context"
"errors"
"fmt"
"sort"
"strconv"
@@ -117,13 +116,13 @@ func (z *ZB) SetDefaults() {
kline.IntervalCapacity{Interval: kline.FifteenMin},
kline.IntervalCapacity{Interval: kline.ThirtyMin},
kline.IntervalCapacity{Interval: kline.OneHour},
kline.IntervalCapacity{Interval: kline.TwoHour},
kline.IntervalCapacity{Interval: kline.FourHour},
// NOTE: The supported time intervals below are returned
// offset to the Asia/Shanghai time zone. This may lead to
// issues with candle quality and conversion as the
// intervals may be broken up. Therefore the below intervals
// are constructed from hourly candles.
// kline.IntervalCapacity{Interval: kline.TwoHour},
// kline.IntervalCapacity{Interval: kline.FourHour},
// kline.IntervalCapacity{Interval: kline.SixHour},
// kline.IntervalCapacity{Interval: kline.TwelveHour},
// kline.IntervalCapacity{Interval: kline.OneDay},
@@ -254,7 +253,11 @@ func (z *ZB) UpdateTradablePairs(ctx context.Context, forceUpdate bool) error {
if err != nil {
return err
}
return z.UpdatePairs(pairs, asset.Spot, false, forceUpdate)
err = z.UpdatePairs(pairs, asset.Spot, false, forceUpdate)
if err != nil {
return err
}
return z.EnsureOnePairEnabled()
}
// UpdateTickers updates the ticker for all currency pairs of a given asset type
@@ -322,6 +325,12 @@ func (z *ZB) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asse
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (z *ZB) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
if p.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
if err := z.CurrencyPairs.IsAssetEnabled(assetType); err != nil {
return nil, err
}
book := &orderbook.Base{
Exchange: z.Name,
Pair: p,
@@ -429,15 +438,125 @@ func (z *ZB) FetchAccountInfo(ctx context.Context, assetType asset.Item) (accoun
return acc, nil
}
// GetFundingHistory returns funding history, deposits and
// GetAccountFundingHistory returns funding history, deposits and
// withdrawals
func (z *ZB) GetFundingHistory(_ context.Context) ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
func (z *ZB) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) {
pairs, err := z.GetEnabledPairs(asset.Spot)
if err != nil {
return nil, err
}
currs := pairs.GetCurrencies()
var records []exchange.FundingHistory
for x := range currs {
totalPages := int64(1)
for y := int64(0); y < totalPages; y++ {
var deposits *DepositRecordsResponse
deposits, err = z.GetDepositRecords(ctx, &WalletRecordsRequest{
Currency: currs[x],
PageIndex: y,
})
if err != nil {
return nil, err
}
totalPages = deposits.Total
for i := range deposits.List {
var status string
switch deposits.List[i].Status {
case 0:
status = "pending confirm"
case 1:
status = "failed"
case 2:
status = "confirmed"
}
records = append(records, exchange.FundingHistory{
ExchangeName: z.GetName(),
Status: status,
TransferID: strconv.FormatInt(deposits.List[i].ID, 10),
Description: deposits.List[i].Description,
Timestamp: time.Unix(deposits.List[i].SubmitTime, 0),
Currency: deposits.List[i].Currency,
Amount: deposits.List[i].Amount,
CryptoToAddress: deposits.List[i].Address,
TransferType: "deposit",
})
}
}
}
for x := range currs {
var resp []exchange.WithdrawalHistory
resp, err = z.GetWithdrawalsHistory(ctx, currs[x], asset.Spot)
if err != nil {
return nil, err
}
for y := range resp {
records = append(records, exchange.FundingHistory{
ExchangeName: z.GetName(),
Status: resp[y].Status,
TransferID: resp[y].TransferID,
Timestamp: resp[y].Timestamp,
Currency: resp[y].Currency,
Amount: resp[y].Amount,
CryptoToAddress: resp[y].CryptoToAddress,
Fee: resp[y].Fee,
TransferType: resp[y].TransferType,
})
}
}
sort.Slice(records, func(i, j int) bool {
return records[i].Timestamp.Before(records[j].Timestamp)
})
return records, nil
}
// GetWithdrawalsHistory returns previous withdrawals data
func (z *ZB) GetWithdrawalsHistory(_ context.Context, _ currency.Code, _ asset.Item) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
func (z *ZB) GetWithdrawalsHistory(ctx context.Context, c currency.Code, _ asset.Item) ([]exchange.WithdrawalHistory, error) {
totalPages := int64(1)
var records []exchange.WithdrawalHistory
for y := int64(0); y < totalPages; y++ {
withdrawals, err := z.GetWithdrawalRecords(ctx, &WalletRecordsRequest{
Currency: c,
PageIndex: y,
})
if err != nil {
return nil, err
}
totalPages = withdrawals.Total
for i := range withdrawals.List {
var status string
switch withdrawals.List[i].Status {
case 0:
status = "submitted"
case 1:
status = "failed"
case 2:
status = "successful"
case 3:
status = "cancelled"
case 5:
status = "confirmed"
}
records = append(records, exchange.WithdrawalHistory{
Status: status,
TransferID: strconv.FormatInt(withdrawals.List[i].ID, 10),
Timestamp: time.Unix(withdrawals.List[i].SubmitTime, 0),
Currency: c.String(),
Amount: withdrawals.List[i].Amount,
CryptoToAddress: withdrawals.List[i].ToAddress,
Fee: withdrawals.List[i].Fees,
TransferType: "withdrawal",
})
}
}
return records, nil
}
// GetServerTime returns the current exchange server time.
func (z *ZB) GetServerTime(_ context.Context, _ asset.Item) (time.Time, error) {
return time.Time{}, common.ErrFunctionNotSupported
}
// GetRecentTrades returns the most recent trades for a currency and asset
@@ -555,20 +674,20 @@ func (z *ZB) CancelOrder(ctx context.Context, o *order.Cancel) error {
return err
}
if !response.Success {
return fmt.Errorf("%v - Could not cancel order %v", z.Name, o.OrderID)
return fmt.Errorf("%w %v %v", request.ErrAuthRequestFailed, o.OrderID, response.Message)
}
return nil
}
fpair, err := z.FormatExchangeCurrency(o.Pair, o.AssetType)
fPair, err := z.FormatExchangeCurrency(o.Pair, o.AssetType)
if err != nil {
return err
}
return z.CancelExistingOrder(ctx, orderIDInt, fpair.String())
return z.CancelExistingOrder(ctx, orderIDInt, fPair.String())
}
// CancelBatchOrders cancels an orders by their corresponding ID numbers
func (z *ZB) CancelBatchOrders(_ context.Context, _ []order.Cancel) (order.CancelBatchResponse, error) {
return order.CancelBatchResponse{}, common.ErrNotYetImplemented
func (z *ZB) CancelBatchOrders(_ context.Context, _ []order.Cancel) (*order.CancelBatchResponse, error) {
return nil, common.ErrFunctionNotSupported
}
// CancelAllOrders cancels all orders associated with a currency pair
@@ -612,17 +731,17 @@ func (z *ZB) CancelAllOrders(ctx context.Context, _ *order.Cancel) (order.Cancel
for i := range allOpenOrders {
p, err := currency.NewPairFromString(allOpenOrders[i].Currency)
if err != nil {
cancelAllOrdersResponse.Status[strconv.FormatInt(allOpenOrders[i].ID, 10)] = err.Error()
cancelAllOrdersResponse.Status[allOpenOrders[i].ID] = err.Error()
continue
}
err = z.CancelOrder(ctx, &order.Cancel{
OrderID: strconv.FormatInt(allOpenOrders[i].ID, 10),
OrderID: allOpenOrders[i].ID,
Pair: p,
AssetType: asset.Spot,
})
if err != nil {
cancelAllOrdersResponse.Status[strconv.FormatInt(allOpenOrders[i].ID, 10)] = err.Error()
cancelAllOrdersResponse.Status[allOpenOrders[i].ID] = err.Error()
}
}
@@ -630,9 +749,46 @@ func (z *ZB) CancelAllOrders(ctx context.Context, _ *order.Cancel) (order.Cancel
}
// GetOrderInfo returns order information based on order ID
func (z *ZB) GetOrderInfo(_ context.Context, _ string, _ currency.Pair, _ asset.Item) (order.Detail, error) {
var orderDetail order.Detail
return orderDetail, common.ErrNotYetImplemented
func (z *ZB) GetOrderInfo(ctx context.Context, orderID string, pair currency.Pair, assetType asset.Item) (*order.Detail, error) {
if pair.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
if err := z.CurrencyPairs.IsAssetEnabled(assetType); err != nil {
return nil, err
}
resp, err := z.GetSingleOrder(ctx, orderID, "", pair)
if err != nil {
return nil, err
}
side := order.Sell
if resp.Type == 1 {
side = order.Buy
}
var status order.Status
switch resp.Status {
case 1:
status = order.Cancelled
case 2:
status = order.Closed
case 3:
status = order.Pending
}
return &order.Detail{
Price: resp.Price,
Amount: resp.TotalAmount,
ExecutedAmount: resp.TradeAmount,
RemainingAmount: resp.TotalAmount - resp.TradeAmount,
Fee: resp.Fees,
FeeAsset: pair.Quote,
Exchange: z.Name,
OrderID: resp.ID,
Side: side,
Status: status,
AssetType: assetType,
Date: time.Unix(resp.TradeDate, 0),
Pair: pair,
}, nil
}
// GetDepositAddress returns a deposit address for a specified currency
@@ -650,7 +806,7 @@ func (z *ZB) GetDepositAddress(ctx context.Context, cryptocurrency currency.Code
}, nil
}
}
return nil, fmt.Errorf("%s does not support chain %s", cryptocurrency.String(), chain)
return nil, fmt.Errorf("%w %s does not support chain %s", request.ErrAuthRequestFailed, cryptocurrency.String(), chain)
}
address, err := z.GetCryptoAddress(ctx, cryptocurrency)
if err != nil {
@@ -710,7 +866,7 @@ func (z *ZB) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuilder)
// GetActiveOrders retrieves any orders that are active/open
// This function is not concurrency safe due to orderSide/orderType maps
func (z *ZB) GetActiveOrders(ctx context.Context, req *order.GetOrdersRequest) (order.FilteredOrders, error) {
func (z *ZB) GetActiveOrders(ctx context.Context, req *order.MultiOrderRequest) (order.FilteredOrders, error) {
err := req.Validate()
if err != nil {
return nil, err
@@ -759,10 +915,10 @@ func (z *ZB) GetActiveOrders(ctx context.Context, req *order.GetOrdersRequest) (
if err != nil {
return nil, err
}
orderDate := time.Unix(int64(allOrders[i].TradeDate), 0)
orderDate := time.Unix(allOrders[i].TradeDate, 0)
orderSide := orderSideMap[allOrders[i].Type]
orders[i] = order.Detail{
OrderID: strconv.FormatInt(allOrders[i].ID, 10),
OrderID: allOrders[i].ID,
Amount: allOrders[i].TotalAmount,
Exchange: z.Name,
Date: orderDate,
@@ -777,16 +933,22 @@ func (z *ZB) GetActiveOrders(ctx context.Context, req *order.GetOrdersRequest) (
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
// This function is not concurrency safe due to orderSide/orderType maps
func (z *ZB) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) (order.FilteredOrders, error) {
func (z *ZB) GetOrderHistory(ctx context.Context, req *order.MultiOrderRequest) (order.FilteredOrders, error) {
err := req.Validate()
if err != nil {
return nil, err
}
if req.Side == order.AnySide {
return nil, errors.New("specific order side is required")
sides := make([]int64, 0, 2)
switch {
case req.Side == order.AnySide:
sides = append(sides, 0, 1)
case req.Side.IsLong():
sides = append(sides, 1)
case req.Side.IsShort():
sides = append(sides, 0)
default:
return nil, fmt.Errorf("%w %v", order.ErrUnsupportedOrderType, req.Side)
}
var allOrders []Order
if z.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
for x := range req.Pairs {
@@ -803,28 +965,26 @@ func (z *ZB) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) (
}
}
} else {
var side int64
if req.Side == order.Buy {
side = 1
}
for x := range req.Pairs {
for y := int64(1); ; y++ {
var fPair currency.Pair
fPair, err = z.FormatExchangeCurrency(req.Pairs[x], asset.Spot)
if err != nil {
return nil, err
}
var resp []Order
resp, err = z.GetOrders(ctx, fPair.String(), y, side)
if err != nil {
return nil, err
}
if len(resp) == 0 {
break
}
allOrders = append(allOrders, resp...)
if len(resp) != 10 {
break
for i := range sides {
for x := range req.Pairs {
for y := int64(1); ; y++ {
var fPair currency.Pair
fPair, err = z.FormatExchangeCurrency(req.Pairs[x], asset.Spot)
if err != nil {
return nil, err
}
var resp []Order
resp, err = z.GetOrders(ctx, fPair.String(), y, sides[i])
if err != nil {
return nil, err
}
if len(resp) == 0 {
break
}
allOrders = append(allOrders, resp...)
if len(resp) != 10 {
break
}
}
}
}
@@ -843,10 +1003,10 @@ func (z *ZB) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) (
if err != nil {
return nil, err
}
orderDate := time.Unix(int64(allOrders[i].TradeDate), 0)
orderDate := time.Unix(allOrders[i].TradeDate, 0)
orderSide := orderSideMap[allOrders[i].Type]
detail := order.Detail{
OrderID: strconv.FormatInt(allOrders[i].ID, 10),
OrderID: allOrders[i].ID,
Amount: allOrders[i].TotalAmount,
ExecutedAmount: allOrders[i].TradeAmount,
RemainingAmount: allOrders[i].TotalAmount - allOrders[i].TradeAmount,