mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 23:16:51 +00:00
exchanges/wrappers: Refactor fetch orderbook/ticker/account info funcs (#1440)
* acrost: Pull thread, examine * fix tests * linter * fix_linter * revert rm ctx param to limit breakages when merging usptream * linter fix * Add in priority update grouping so that tests pass * Update cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * glorious nits * fixed spelling * whoopsie * aanother whoops * glorious: NITTERS! * glorious: further nitters * srry linter gods * glorious: nits continued * sub test p ara lel * drop main t.Parallel * fix whoops * wrappertests: use context with cancel (test) * linter: fix * ensure primary execution * kucoin test fix * revert standards test changes and bypass non critical errors * rm single override * wrap exported error for accounts * thrasher: nits ch name * gk: nits * gk: nits FetchTickerCached -> GetCachedTicker * gk: nits rn FetchOrderbookCached -> GetCachedOrderbook * gk: nits rn FetchAccountInfoCached -> GetCachedAccountInfo * linter: fix * gk: nits * thrasher: nitters 1 * thrasher: nitters tmpls * gk: nitter --------- Co-authored-by: shazbert <ryan.oharareid@thrasher.io> Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
@@ -56,13 +56,13 @@ for i := range bot.Exchanges {
|
||||
// Public calls - wrapper functions
|
||||
|
||||
// Fetches current ticker information
|
||||
tick, err := g.FetchTicker()
|
||||
tick, err := g.UpdateTicker(...)
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
// Fetches current orderbook information
|
||||
ob, err := g.FetchOrderbook()
|
||||
ob, err := g.UpdateOrderbook(...)
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"slices"
|
||||
@@ -2710,26 +2711,20 @@ func TestOptionsCandlesticksPushData(t *testing.T) {
|
||||
|
||||
const (
|
||||
optionsOrderbookTickerPushDataJSON = `{ "time": 1630650452, "channel": "options.book_ticker", "event": "update", "result": { "t": 1615366379123, "u": 2517661076, "s": "BTC_USDT-20211130-50000-C", "b": "54696.6", "B": 37000, "a": "54696.7", "A": 47061 }}`
|
||||
optionsOrderbookUpdatePushDataJSON = `{ "time": 1630650445, "channel": "options.order_book_update", "event": "update", "result": { "t": 1615366381417, "s": "BTC_USDT-20211130-50000-C", "U": 2517661101, "u": 2517661113, "b": [ { "p": "54672.1", "s": 95 }, { "p": "54664.5", "s": 58794 } ], "a": [ { "p": "54743.6", "s": 95 }, { "p": "54742", "s": 95 } ] }}`
|
||||
optionsOrderbookUpdatePushDataJSON = `{ "time": 1630650445, "channel": "options.order_book_update", "event": "update", "result": { "t": 1615366381417, "s": "%s", "U": 2517661101, "u": 2517661113, "b": [ { "p": "54672.1", "s": 95 }, { "p": "54664.5", "s": 58794 } ], "a": [ { "p": "54743.6", "s": 95 }, { "p": "54742", "s": 95 } ] }}`
|
||||
optionsOrderbookSnapshotPushDataJSON = `{ "time": 1630650445, "channel": "options.order_book", "event": "all", "result": { "t": 1541500161123, "contract": "BTC_USDT-20211130-50000-C", "id": 93973511, "asks": [ { "p": "97.1", "s": 2245 }, { "p": "97.2", "s": 2245 } ], "bids": [ { "p": "97.2", "s": 2245 }, { "p": "97.1", "s": 2245 } ] }}`
|
||||
optionsOrderbookSnapshotUpdateEventPushDataJSON = `{"channel": "options.order_book", "event": "update", "time": 1630650445, "result": [ { "p": "49525.6", "s": 7726, "c": "BTC_USDT-20211130-50000-C", "id": 93973511 } ]}`
|
||||
)
|
||||
|
||||
func TestOptionsOrderbookPushData(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookTickerPushDataJSON))
|
||||
if err != nil {
|
||||
t.Errorf("%s websocket options orderbook ticker push data error: %v", g.Name, err)
|
||||
}
|
||||
if err = g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookSnapshotPushDataJSON)); err != nil {
|
||||
t.Errorf("%s websocket options orderbook snapshot push data error: %v", g.Name, err)
|
||||
}
|
||||
if err = g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookUpdatePushDataJSON)); err != nil {
|
||||
t.Errorf("%s websocket options orderbook update push data error: %v", g.Name, err)
|
||||
}
|
||||
if err = g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookSnapshotUpdateEventPushDataJSON)); err != nil {
|
||||
t.Errorf("%s websocket options orderbook snapshot update event push data error: %v", g.Name, err)
|
||||
}
|
||||
testexch.UpdatePairsOnce(t, g)
|
||||
assert.NoError(t, g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookTickerPushDataJSON)))
|
||||
avail, err := g.GetAvailablePairs(asset.Options)
|
||||
require.NoError(t, err, "GetAvailablePairs must not error")
|
||||
assert.NoError(t, g.WsHandleOptionsData(context.Background(), []byte(fmt.Sprintf(optionsOrderbookUpdatePushDataJSON, avail[0].Upper().String()))))
|
||||
assert.NoError(t, g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookSnapshotPushDataJSON)))
|
||||
assert.NoError(t, g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookSnapshotUpdateEventPushDataJSON)))
|
||||
}
|
||||
|
||||
const optionsOrderPushDataJSON = `{"time": 1630654851,"channel": "options.orders", "event": "update", "result": [ { "contract": "BTC_USDT-20211130-65000-C", "create_time": 1637897000, "fill_price": 0, "finish_as": "cancelled", "iceberg": 0, "id": 106, "is_close": false, "is_liq": false, "is_reduce_only": false, "left": -10, "mkfr": 0.0004, "price": 15000, "refr": 0, "refu": 0, "size": -10, "status": "finished", "text": "web", "tif": "gtc", "tkfr": 0.0004, "underlying": "BTC_USDT", "user": "9xxx", "time": 1639051907,"time_ms": 1639051907000}]}`
|
||||
|
||||
@@ -389,7 +389,7 @@ func (g *Gateio) processOrderbookUpdate(incoming []byte, updatePushedAt time.Tim
|
||||
|
||||
sPair := data.CurrencyPair.String()
|
||||
if !fetchedCurrencyPairSnapshotOrderbook[sPair] {
|
||||
orderbooks, err := g.FetchOrderbook(context.Background(), data.CurrencyPair, asset.Spot) // currency pair orderbook data for Spot, Margin, and Cross Margin is same
|
||||
orderbooks, err := g.UpdateOrderbook(context.Background(), data.CurrencyPair, asset.Spot) // currency pair orderbook data for Spot, Margin, and Cross Margin is same
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -420,7 +420,7 @@ func (g *Gateio) processFuturesAndOptionsOrderbookUpdate(incoming []byte, assetT
|
||||
}
|
||||
if (assetType == asset.Options && !fetchedOptionsCurrencyPairSnapshotOrderbook[data.ContractName.String()]) ||
|
||||
(assetType != asset.Options && !fetchedFuturesCurrencyPairSnapshotOrderbook[data.ContractName.String()]) {
|
||||
orderbooks, err := g.FetchOrderbook(context.Background(), data.ContractName, assetType)
|
||||
orderbooks, err := g.UpdateOrderbook(context.Background(), data.ContractName, assetType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -438,19 +438,6 @@ func (g *Gateio) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item
|
||||
return ticker.GetTicker(g.Name, fPair, a)
|
||||
}
|
||||
|
||||
// FetchTicker retrieves a list of tickers.
|
||||
func (g *Gateio) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
|
||||
fPair, err := g.FormatExchangeCurrency(p, assetType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tickerNew, err := ticker.GetTicker(g.Name, fPair, assetType)
|
||||
if err != nil {
|
||||
return g.UpdateTicker(ctx, fPair, assetType)
|
||||
}
|
||||
return tickerNew, nil
|
||||
}
|
||||
|
||||
// FetchTradablePairs returns a list of the exchanges tradable pairs
|
||||
func (g *Gateio) FetchTradablePairs(ctx context.Context, a asset.Item) (currency.Pairs, error) {
|
||||
if !g.SupportsAsset(a) {
|
||||
@@ -704,15 +691,6 @@ func (g *Gateio) UpdateTickers(ctx context.Context, a asset.Item) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// FetchOrderbook returns orderbook base on the currency pair
|
||||
func (g *Gateio) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
||||
ob, err := orderbook.Get(g.Name, p, assetType)
|
||||
if err != nil {
|
||||
return g.UpdateOrderbook(ctx, p, assetType)
|
||||
}
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
||||
func (g *Gateio) UpdateOrderbook(ctx context.Context, p currency.Pair, a asset.Item) (*orderbook.Base, error) {
|
||||
p, err := g.FormatExchangeCurrency(p, a)
|
||||
@@ -896,19 +874,6 @@ func (g *Gateio) UpdateAccountInfo(ctx context.Context, a asset.Item) (account.H
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// FetchAccountInfo retrieves balances for all enabled currencies
|
||||
func (g *Gateio) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
||||
creds, err := g.GetCredentials(ctx)
|
||||
if err != nil {
|
||||
return account.Holdings{}, err
|
||||
}
|
||||
acc, err := account.GetHoldings(g.Name, creds, assetType)
|
||||
if err != nil {
|
||||
return g.UpdateAccountInfo(ctx, assetType)
|
||||
}
|
||||
return acc, nil
|
||||
}
|
||||
|
||||
// GetAccountFundingHistory returns funding history, deposits and
|
||||
// withdrawals
|
||||
func (g *Gateio) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) {
|
||||
|
||||
Reference in New Issue
Block a user