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

@@ -9,9 +9,11 @@ import (
"net/url"
"strconv"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"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/request"
)
@@ -41,6 +43,7 @@ const (
geminiWithdraw = "withdraw/"
geminiHeartbeat = "heartbeat"
geminiVolume = "notionalvolume"
geminiTransfers = "transfers"
)
// Gemini is the overarching type across the Gemini package, create multiple
@@ -169,6 +172,29 @@ func (g *Gemini) NewOrder(ctx context.Context, symbol string, amount, price floa
return response.OrderID, nil
}
// Transfers returns transfer history ie withdrawals and deposits
func (g *Gemini) Transfers(ctx context.Context, curr currency.Code, start time.Time, limit int64, account string, showCompletedDeposit bool) ([]TransferResponse, error) {
req := make(map[string]interface{})
if !curr.IsEmpty() {
req["symbol"] = curr.String()
}
if !start.IsZero() {
req["timestamp"] = start.Unix()
}
if limit > 0 {
req["limit"] = limit
}
if account != "" {
req["account"] = account
}
if showCompletedDeposit {
req["showCompletedDeposit"] = showCompletedDeposit
}
var response []TransferResponse
return response, g.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, geminiTransfers, req, &response)
}
// CancelExistingOrder will cancel an order. If the order is already canceled, the
// message will succeed but have no effect.
func (g *Gemini) CancelExistingOrder(ctx context.Context, orderID int64) (Order, error) {
@@ -361,7 +387,7 @@ func (g *Gemini) SendHTTPRequest(ctx context.Context, ep exchange.URL, path stri
return g.SendPayload(ctx, request.Unset, func() (*request.Item, error) {
return item, nil
})
}, request.UnauthenticatedRequest)
}
// SendAuthenticatedHTTPRequest sends an authenticated HTTP request to the
@@ -412,13 +438,12 @@ func (g *Gemini) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.U
Path: endpoint + "/v1/" + path,
Headers: headers,
Result: result,
AuthRequest: true,
NonceEnabled: true,
Verbose: g.Verbose,
HTTPDebugging: g.HTTPDebugging,
HTTPRecording: g.HTTPRecording,
}, nil
})
}, request.AuthenticatedRequest)
}
// GetFee returns an estimate of fee based on type of transaction

View File

@@ -358,7 +358,7 @@ func TestFormatWithdrawPermissions(t *testing.T) {
func TestGetActiveOrders(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),
@@ -380,7 +380,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,
@@ -1219,3 +1219,43 @@ func TestGetHistoricTrades(t *testing.T) {
t.Error(err)
}
}
func TestTransfers(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, g)
_, err := g.Transfers(context.Background(), currency.BTC, time.Time{}, 100, "", true)
if err != nil {
t.Error(err)
}
}
func TestGetAccountFundingHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, g)
_, err := g.GetAccountFundingHistory(context.Background())
if err != nil {
t.Error(err)
}
}
func TestGetWithdrawalsHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, g)
_, err := g.GetWithdrawalsHistory(context.Background(), currency.BTC, asset.Spot)
if err != nil {
t.Error(err)
}
}
func TestGetOrderInfo(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, g)
_, err := g.GetOrderInfo(context.Background(), "1234", currency.EMPTYPAIR, asset.Empty)
if err != nil {
t.Error(err)
}
}

View File

@@ -1,6 +1,8 @@
package gemini
import "github.com/thrasher-corp/gocryptotrader/currency"
import (
"github.com/thrasher-corp/gocryptotrader/currency"
)
const (
marketDataLevel2 = "l2"
@@ -105,6 +107,25 @@ type OrderResult struct {
Message string `json:"message"`
}
// TransferResponse contains transfer information
type TransferResponse struct {
Type string `json:"type"`
Status string `json:"status"`
Timestamp int64 `json:"timestampms"`
EventID int64 `json:"eid"`
DepositAdvanceEventID int64 `json:"advanceEid"`
Currency currency.Code `json:"currency"`
Amount float64 `json:"amount,string"`
FeeAmount float64 `json:"feeAmount,string"`
FeeCurrency currency.Code `json:"feeCurrency"`
Method string `json:"method"`
TxHash string `json:"txHash"`
WithdrawalID string `json:"withdrawalId"`
OutputIdx int64 `json:"outputIdx"`
Destination string `json:"destination"`
Purpose string `json:"purpose"`
}
// Order contains order information
type Order struct {
OrderID int64 `json:"order_id,string"`

View File

@@ -309,7 +309,11 @@ func (g *Gemini) UpdateTradablePairs(ctx context.Context, forceUpdate bool) erro
if err != nil {
return err
}
return g.UpdatePairs(pairs, asset.Spot, false, forceUpdate)
err = g.UpdatePairs(pairs, asset.Spot, false, forceUpdate)
if err != nil {
return err
}
return g.EnsureOnePairEnabled()
}
// UpdateAccountInfo Retrieves balances for all enabled currencies for the
@@ -426,6 +430,12 @@ func (g *Gemini) FetchOrderbook(ctx context.Context, p currency.Pair, assetType
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (g *Gemini) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
if p.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
if err := g.CurrencyPairs.IsAssetEnabled(assetType); err != nil {
return nil, err
}
book := &orderbook.Base{
Exchange: g.Name,
Pair: p,
@@ -464,15 +474,57 @@ func (g *Gemini) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType
return orderbook.Get(g.Name, fPair, assetType)
}
// GetFundingHistory returns funding history, deposits and
// GetAccountFundingHistory returns funding history, deposits and
// withdrawals
func (g *Gemini) GetFundingHistory(_ context.Context) ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
func (g *Gemini) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) {
transfers, err := g.Transfers(ctx, currency.EMPTYCODE, time.Time{}, 50, "", false)
if err != nil {
return nil, err
}
resp := make([]exchange.FundingHistory, len(transfers))
for i := range transfers {
resp[i] = exchange.FundingHistory{
Status: transfers[i].Status,
TransferID: transfers[i].WithdrawalID,
Timestamp: time.UnixMilli(transfers[i].Timestamp),
Currency: transfers[i].Currency.String(),
Amount: transfers[i].Amount,
Fee: transfers[i].FeeAmount,
TransferType: transfers[i].Type,
CryptoToAddress: transfers[i].Destination,
CryptoTxID: transfers[i].TxHash,
}
}
return resp, nil
}
// GetWithdrawalsHistory returns previous withdrawals data
func (g *Gemini) GetWithdrawalsHistory(_ context.Context, _ currency.Code, _ asset.Item) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
func (g *Gemini) GetWithdrawalsHistory(ctx context.Context, c currency.Code, a asset.Item) ([]exchange.WithdrawalHistory, error) {
if err := g.CurrencyPairs.IsAssetEnabled(a); err != nil {
return nil, err
}
transfers, err := g.Transfers(ctx, c, time.Time{}, 50, "", false)
if err != nil {
return nil, err
}
resp := make([]exchange.WithdrawalHistory, 0, len(transfers))
for i := range transfers {
if transfers[i].Type != "Withdrawal" {
continue
}
resp = append(resp, exchange.WithdrawalHistory{
Status: transfers[i].Status,
TransferID: transfers[i].WithdrawalID,
Timestamp: time.UnixMilli(transfers[i].Timestamp),
Currency: transfers[i].Currency.String(),
Amount: transfers[i].Amount,
Fee: transfers[i].FeeAmount,
TransferType: transfers[i].Type,
CryptoToAddress: transfers[i].Destination,
CryptoTxID: transfers[i].TxHash,
})
}
return resp, nil
}
// GetRecentTrades returns the most recent trades for a currency and asset
@@ -557,13 +609,13 @@ func (g *Gemini) SubmitOrder(ctx context.Context, s *order.Submit) (*order.Submi
return nil, errors.New("only limit orders are enabled through this exchange")
}
fpair, err := g.FormatExchangeCurrency(s.Pair, asset.Spot)
fPair, err := g.FormatExchangeCurrency(s.Pair, asset.Spot)
if err != nil {
return nil, err
}
response, err := g.NewOrder(ctx,
fpair.String(),
fPair.String(),
s.Amount,
s.Price,
s.Side.String(),
@@ -597,8 +649,13 @@ func (g *Gemini) CancelOrder(ctx context.Context, o *order.Cancel) error {
}
// CancelBatchOrders cancels an orders by their corresponding ID numbers
func (g *Gemini) CancelBatchOrders(_ context.Context, _ []order.Cancel) (order.CancelBatchResponse, error) {
return order.CancelBatchResponse{}, common.ErrNotYetImplemented
func (g *Gemini) CancelBatchOrders(_ context.Context, _ []order.Cancel) (*order.CancelBatchResponse, error) {
return nil, common.ErrFunctionNotSupported
}
// GetServerTime returns the current exchange server time.
func (g *Gemini) GetServerTime(_ context.Context, _ asset.Item) (time.Time, error) {
return time.Time{}, common.ErrFunctionNotSupported
}
// CancelAllOrders cancels all orders associated with a currency pair
@@ -619,9 +676,43 @@ func (g *Gemini) CancelAllOrders(ctx context.Context, _ *order.Cancel) (order.Ca
}
// GetOrderInfo returns order information based on order ID
func (g *Gemini) GetOrderInfo(_ context.Context, _ string, _ currency.Pair, _ asset.Item) (order.Detail, error) {
var orderDetail order.Detail
return orderDetail, common.ErrNotYetImplemented
func (g *Gemini) GetOrderInfo(ctx context.Context, orderID string, _ currency.Pair, _ asset.Item) (*order.Detail, error) {
iOID, err := strconv.ParseInt(orderID, 10, 64)
if err != nil {
return nil, err
}
resp, err := g.GetOrderStatus(ctx, iOID)
if err != nil {
return nil, err
}
cp, err := currency.NewPairFromString(resp.Symbol)
if err != nil {
return nil, err
}
var orderType order.Type
if resp.Type == "exchange limit" {
orderType = order.Limit
} else if resp.Type == "market buy" || resp.Type == "market sell" {
orderType = order.Market
}
var side order.Side
side, err = order.StringToOrderSide(resp.Side)
if err != nil {
return nil, err
}
return &order.Detail{
OrderID: strconv.FormatInt(resp.OrderID, 10),
Amount: resp.OriginalAmount,
RemainingAmount: resp.RemainingAmount,
Pair: cp,
Date: time.UnixMilli(resp.TimestampMS),
Price: resp.Price,
HiddenOrder: resp.IsHidden,
ClientOrderID: resp.ClientOrderID,
Type: orderType,
Side: side,
}, nil
}
// GetDepositAddress returns a deposit address for a specified currency
@@ -680,7 +771,7 @@ func (g *Gemini) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuild
}
// GetActiveOrders retrieves any orders that are active/open
func (g *Gemini) GetActiveOrders(ctx context.Context, req *order.GetOrdersRequest) (order.FilteredOrders, error) {
func (g *Gemini) GetActiveOrders(ctx context.Context, req *order.MultiOrderRequest) (order.FilteredOrders, error) {
err := req.Validate()
if err != nil {
return nil, err
@@ -715,7 +806,7 @@ func (g *Gemini) GetActiveOrders(ctx context.Context, req *order.GetOrdersReques
orderType = order.Market
}
var side order.Side
side, err = order.StringToOrderSide(resp[i].Type)
side, err = order.StringToOrderSide(resp[i].Side)
if err != nil {
return nil, err
}
@@ -739,7 +830,7 @@ func (g *Gemini) GetActiveOrders(ctx context.Context, req *order.GetOrdersReques
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func (g *Gemini) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) (order.FilteredOrders, error) {
func (g *Gemini) GetOrderHistory(ctx context.Context, req *order.MultiOrderRequest) (order.FilteredOrders, error) {
err := req.Validate()
if err != nil {
return nil, err