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

@@ -675,11 +675,8 @@ func (b *Bitmex) ConfirmWithdrawal(ctx context.Context, token string) (Transacti
// GetCryptoDepositAddress returns a deposit address for a cryptocurency
func (b *Bitmex) GetCryptoDepositAddress(ctx context.Context, cryptoCurrency string) (string, error) {
var address string
if !strings.EqualFold(cryptoCurrency, currency.XBT.String()) {
return "",
fmt.Errorf("cryptocurrency %s deposits are not supported by exchange only bitcoin",
cryptoCurrency)
return "", fmt.Errorf("%v %w only bitcoin", cryptoCurrency, currency.ErrCurrencyNotSupported)
}
return address, b.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet,
@@ -845,7 +842,7 @@ func (b *Bitmex) SendHTTPRequest(ctx context.Context, ep exchange.URL, path stri
err = b.SendPayload(ctx, request.Unset, func() (*request.Item, error) {
return item, nil
})
}, request.UnauthenticatedRequest)
if err != nil {
return err
}
@@ -906,13 +903,12 @@ func (b *Bitmex) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.U
Headers: headers,
Body: strings.NewReader(payload),
Result: &respCheck,
AuthRequest: true,
Verbose: b.Verbose,
HTTPDebugging: b.HTTPDebugging,
HTTPRecording: b.HTTPRecording,
}, nil
}
err = b.SendPayload(ctx, request.Auth, newRequest)
err = b.SendPayload(ctx, request.Auth, newRequest, request.AuthenticatedRequest)
if err != nil {
return err
}

View File

@@ -189,9 +189,9 @@ func TestGetAccountExecutionTradeHistory(t *testing.T) {
func TestGetFundingHistory(t *testing.T) {
t.Parallel()
_, err := b.GetFundingHistory(context.Background())
_, err := b.GetAccountFundingHistory(context.Background())
if err == nil {
t.Error("GetFundingHistory() Expected error")
t.Error("GetAccountFundingHistory() Expected error")
}
}
@@ -590,7 +590,7 @@ func TestFormatWithdrawPermissions(t *testing.T) {
func TestGetActiveOrders(t *testing.T) {
t.Parallel()
var getOrdersRequest = order.GetOrdersRequest{
var getOrdersRequest = order.MultiOrderRequest{
Type: order.AnyType,
AssetType: asset.Spot,
Side: order.AnySide,
@@ -606,7 +606,7 @@ func TestGetActiveOrders(t *testing.T) {
func TestGetOrderHistory(t *testing.T) {
t.Parallel()
var getOrdersRequest = order.GetOrdersRequest{
var getOrdersRequest = order.MultiOrderRequest{
Type: order.AnyType,
Pairs: []currency.Pair{currency.NewPair(currency.LTC, currency.BTC)},
AssetType: asset.Spot,
@@ -1188,3 +1188,47 @@ func TestGetActionFromString(t *testing.T) {
t.Fatalf("received: '%v' but expected: '%v'", action, orderbook.UpdateInsert)
}
}
func TestGetAccountFundingHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
_, err := b.GetAccountFundingHistory(context.Background())
if err != nil {
t.Error(err)
}
}
func TestGetWithdrawalsHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
_, err := b.GetWithdrawalsHistory(context.Background(), currency.BTC, asset.Spot)
if err != nil {
t.Error(err)
}
}
func TestGetOrderInfo(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, b)
_, err := b.GetOrderInfo(context.Background(), "1234", currency.NewPair(currency.BTC, currency.USD), asset.Spot)
if err != nil {
t.Error(err)
}
}
func TestCancelBatchOrders(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
_, err := b.CancelBatchOrders(context.Background(), []order.Cancel{
{
OrderID: "1234",
AssetType: asset.Spot,
Pair: currency.NewPair(currency.BTC, currency.USD),
},
})
if err != nil {
t.Error(err)
}
}

View File

@@ -564,12 +564,13 @@ type AffiliateStatus struct {
type TransactionInfo struct {
Account int64 `json:"account"`
Address string `json:"address"`
Amount int64 `json:"amount"`
Amount float64 `json:"amount"`
Currency string `json:"currency"`
Fee int64 `json:"fee"`
Fee float64 `json:"fee"`
Text string `json:"text"`
Timestamp time.Time `json:"timestamp"`
TransactID string `json:"transactID"`
Network string `json:"network"`
TransactStatus string `json:"transactStatus"`
TransactTime string `json:"transactTime"`
TransactType string `json:"transactType"`

View File

@@ -337,7 +337,6 @@ func (b *Bitmex) FetchTradablePairs(ctx context.Context, a asset.Item) (currency
// them in the exchanges config
func (b *Bitmex) UpdateTradablePairs(ctx context.Context, _ bool) error {
assets := b.GetAssetTypes(false)
for x := range assets {
pairs, err := b.FetchTradablePairs(ctx, assets[x])
if err != nil {
@@ -349,29 +348,65 @@ func (b *Bitmex) UpdateTradablePairs(ctx context.Context, _ bool) error {
return err
}
}
return nil
return b.EnsureOnePairEnabled()
}
// UpdateTickers updates the ticker for all currency pairs of a given asset type
func (b *Bitmex) UpdateTickers(ctx context.Context, a asset.Item) error {
if !b.SupportsAsset(a) {
return fmt.Errorf("%w for [%v]", asset.ErrNotSupported, a)
}
tick, err := b.GetActiveAndIndexInstruments(ctx)
if err != nil {
return err
}
pairs, err := b.GetEnabledPairs(a)
enabled, err := b.GetEnabledPairs(a)
if err != nil {
return err
}
instruments:
for j := range tick {
var pair currency.Pair
pair, err = currency.NewPairFromString(tick[j].Symbol)
if err != nil {
return err
switch a {
case asset.Futures:
if tick[j].Typ != futuresID {
continue instruments
}
pair, err = enabled.DeriveFrom(tick[j].Symbol)
case asset.Index:
switch tick[j].Typ {
case bitMEXBasketIndexID,
bitMEXPriceIndexID,
bitMEXLendingPremiumIndexID,
bitMEXVolatilityIndexID:
default:
continue instruments
}
// NOTE: Filtering is done below to remove the underscore in a
// limited amount of index asset strings while the rest do not
// contain an underscore. Calling DeriveFrom will then error and
// the instruments will be missed.
tick[j].Symbol = strings.Replace(tick[j].Symbol, currency.UnderscoreDelimiter, "", 1)
pair, err = enabled.DeriveFrom(tick[j].Symbol)
case asset.PerpetualContract:
if tick[j].Typ != perpetualContractID {
continue instruments
}
pair, err = enabled.DeriveFrom(tick[j].Symbol)
case asset.Spot:
if tick[j].Typ != spotID {
continue instruments
}
tick[j].Symbol = strings.Replace(tick[j].Symbol, currency.UnderscoreDelimiter, "", 1)
pair, err = enabled.DeriveFrom(tick[j].Symbol)
}
if !pairs.Contains(pair, true) {
if err != nil {
if !errors.Is(err, currency.ErrPairNotFound) {
return err
}
continue
}
@@ -438,6 +473,12 @@ func (b *Bitmex) FetchOrderbook(ctx context.Context, p currency.Pair, assetType
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *Bitmex) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
if p.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
if err := b.CurrencyPairs.IsAssetEnabled(assetType); err != nil {
return nil, err
}
book := &orderbook.Base{
Exchange: b.Name,
Pair: p,
@@ -449,14 +490,14 @@ func (b *Bitmex) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType
return book, common.ErrFunctionNotSupported
}
fpair, err := b.FormatExchangeCurrency(p, assetType)
fPair, err := b.FormatExchangeCurrency(p, assetType)
if err != nil {
return book, err
}
orderbookNew, err := b.GetOrderbook(ctx,
OrderBookGetL2Params{
Symbol: fpair.String(),
Symbol: fPair.String(),
Depth: 500})
if err != nil {
return book, err
@@ -549,15 +590,57 @@ func (b *Bitmex) FetchAccountInfo(ctx context.Context, assetType asset.Item) (ac
return acc, nil
}
// GetFundingHistory returns funding history, deposits and
// GetAccountFundingHistory returns funding history, deposits and
// withdrawals
func (b *Bitmex) GetFundingHistory(_ context.Context) ([]exchange.FundHistory, error) {
return nil, common.ErrNotYetImplemented
func (b *Bitmex) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) {
history, err := b.GetWalletHistory(ctx, "all")
if err != nil {
return nil, err
}
resp := make([]exchange.FundingHistory, len(history))
for i := range history {
resp[i] = exchange.FundingHistory{
ExchangeName: b.Name,
Status: history[i].TransactStatus,
Timestamp: history[i].Timestamp,
Currency: history[i].Currency,
Amount: history[i].Amount,
Fee: history[i].Fee,
TransferType: history[i].TransactType,
CryptoToAddress: history[i].Address,
CryptoTxID: history[i].TransactID,
CryptoChain: history[i].Network,
}
}
return resp, nil
}
// GetWithdrawalsHistory returns previous withdrawals data
func (b *Bitmex) GetWithdrawalsHistory(_ context.Context, _ currency.Code, _ asset.Item) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
func (b *Bitmex) GetWithdrawalsHistory(ctx context.Context, c currency.Code, _ asset.Item) ([]exchange.WithdrawalHistory, error) {
history, err := b.GetWalletHistory(ctx, c.String())
if err != nil {
return nil, err
}
resp := make([]exchange.WithdrawalHistory, len(history))
for i := range history {
resp[i] = exchange.WithdrawalHistory{
Status: history[i].TransactStatus,
Timestamp: history[i].Timestamp,
Currency: history[i].Currency,
Amount: history[i].Amount,
Fee: history[i].Fee,
TransferType: history[i].TransactType,
CryptoToAddress: history[i].Address,
CryptoTxID: history[i].TransactID,
CryptoChain: history[i].Network,
}
}
return resp, nil
}
// GetServerTime returns the current exchange server time.
func (b *Bitmex) GetServerTime(_ context.Context, _ asset.Item) (time.Time, error) {
return time.Time{}, common.ErrFunctionNotSupported
}
// GetRecentTrades returns the most recent trades for a currency and asset
@@ -568,7 +651,7 @@ func (b *Bitmex) GetRecentTrades(ctx context.Context, p currency.Pair, assetType
// GetHistoricTrades returns historic trade data within the timeframe provided
func (b *Bitmex) GetHistoricTrades(ctx context.Context, p currency.Pair, assetType asset.Item, timestampStart, timestampEnd time.Time) ([]trade.Data, error) {
if assetType == asset.Index {
return nil, fmt.Errorf("asset type '%v' not supported", assetType)
return nil, fmt.Errorf("%w %v", asset.ErrNotSupported, assetType)
}
if err := common.StartEndTimeCheck(timestampStart, timestampEnd); err != nil {
return nil, fmt.Errorf("invalid time range supplied. Start: %v End %v %w", timestampStart, timestampEnd, err)
@@ -715,8 +798,38 @@ func (b *Bitmex) CancelOrder(ctx context.Context, o *order.Cancel) error {
}
// CancelBatchOrders cancels an orders by their corresponding ID numbers
func (b *Bitmex) CancelBatchOrders(_ context.Context, _ []order.Cancel) (order.CancelBatchResponse, error) {
return order.CancelBatchResponse{}, common.ErrNotYetImplemented
func (b *Bitmex) CancelBatchOrders(ctx context.Context, o []order.Cancel) (*order.CancelBatchResponse, error) {
if len(o) == 0 {
return nil, order.ErrCancelOrderIsNil
}
var orderIDs, clientIDs []string
for i := range o {
switch {
case o[i].ClientOrderID != "":
clientIDs = append(clientIDs, o[i].ClientID)
case o[i].OrderID != "":
orderIDs = append(orderIDs, o[i].OrderID)
default:
return nil, order.ErrOrderIDNotSet
}
}
joinedOrderIDs := strings.Join(orderIDs, ",")
joinedClientIDs := strings.Join(clientIDs, ",")
params := &OrderCancelParams{
OrderID: joinedOrderIDs,
ClientOrderID: joinedClientIDs,
}
resp := &order.CancelBatchResponse{
Status: make(map[string]string),
}
cancelResponse, err := b.CancelOrders(ctx, params)
if err != nil {
return nil, err
}
for i := range cancelResponse {
resp.Status[cancelResponse[i].OrderID] = cancelResponse[i].OrdStatus
}
return resp, nil
}
// CancelAllOrders cancels all orders associated with a currency pair
@@ -740,9 +853,50 @@ func (b *Bitmex) CancelAllOrders(ctx context.Context, _ *order.Cancel) (order.Ca
}
// GetOrderInfo returns order information based on order ID
func (b *Bitmex) GetOrderInfo(_ context.Context, _ string, _ currency.Pair, _ asset.Item) (order.Detail, error) {
var orderDetail order.Detail
return orderDetail, common.ErrNotYetImplemented
func (b *Bitmex) GetOrderInfo(ctx context.Context, orderID string, pair currency.Pair, assetType asset.Item) (*order.Detail, error) {
if pair.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
if err := b.CurrencyPairs.IsAssetEnabled(assetType); err != nil {
return nil, err
}
resp, err := b.GetOrders(ctx, &OrdersRequest{
Filter: `{"orderID":"` + orderID + `"}`,
})
if err != nil {
return nil, err
}
for i := range resp {
if resp[i].OrderID != orderID {
continue
}
var orderStatus order.Status
orderStatus, err = order.StringToOrderStatus(resp[i].OrdStatus)
if err != nil {
return nil, err
}
var oType order.Type
oType, err = b.getOrderType(resp[i].OrdType)
if err != nil {
return nil, err
}
return &order.Detail{
Date: resp[i].Timestamp,
Price: resp[i].Price,
Amount: resp[i].OrderQty,
ExecutedAmount: resp[i].CumQty,
RemainingAmount: resp[i].LeavesQty,
Exchange: b.Name,
OrderID: resp[i].OrderID,
Side: orderSideMap[resp[i].Side],
Status: orderStatus,
Type: oType,
Pair: pair,
AssetType: assetType,
}, nil
}
return nil, fmt.Errorf("%w %v", order.ErrOrderNotFound, orderID)
}
// GetDepositAddress returns a deposit address for a specified currency
@@ -809,7 +963,7 @@ func (b *Bitmex) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuild
// GetActiveOrders retrieves any orders that are active/open
// This function is not concurrency safe due to orderSide/orderType maps
func (b *Bitmex) GetActiveOrders(ctx context.Context, req *order.GetOrdersRequest) (order.FilteredOrders, error) {
func (b *Bitmex) GetActiveOrders(ctx context.Context, req *order.MultiOrderRequest) (order.FilteredOrders, error) {
err := req.Validate()
if err != nil {
return nil, err
@@ -864,7 +1018,7 @@ func (b *Bitmex) GetActiveOrders(ctx context.Context, req *order.GetOrdersReques
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
// This function is not concurrency safe due to orderSide/orderType maps
func (b *Bitmex) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) (order.FilteredOrders, error) {
func (b *Bitmex) GetOrderHistory(ctx context.Context, req *order.MultiOrderRequest) (order.FilteredOrders, error) {
err := req.Validate()
if err != nil {
return nil, err