mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-06 15:10:59 +00:00
exchanges: Rename UpdatePushedAt field to LastPushed and use field in gateio REST books (#1917)
* Set update pushed at time and general clean * after merge fix * gk: nits * Update exchanges/gateio/gateio_types.go Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com> * Update exchanges/gateio/gateio_test.go Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com> * boss: nits * sneaky boss attack: nits --------- Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io> Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
This commit is contained in:
@@ -410,11 +410,10 @@ func (g *Gateio) GetOrderbook(ctx context.Context, pairString, interval string,
|
||||
}
|
||||
params.Set("with_id", strconv.FormatBool(withOrderbookID))
|
||||
var response *OrderbookData
|
||||
err := g.SendHTTPRequest(ctx, exchange.RestSpot, publicOrderbookSpotEPL, common.EncodeURLValues(gateioSpotOrderbook, params), &response)
|
||||
if err != nil {
|
||||
if err := g.SendHTTPRequest(ctx, exchange.RestSpot, publicOrderbookSpotEPL, common.EncodeURLValues(gateioSpotOrderbook, params), &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.MakeOrderbook()
|
||||
return response.MakeOrderbook(), nil
|
||||
}
|
||||
|
||||
// GetMarketTrades retrieve market trades
|
||||
|
||||
@@ -1802,8 +1802,16 @@ func TestUpdateTickers(t *testing.T) {
|
||||
func TestUpdateOrderbook(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, a := range g.GetAssetTypes(false) {
|
||||
_, err := g.UpdateOrderbook(t.Context(), getPair(t, a), a)
|
||||
assert.NoErrorf(t, err, "UpdateOrderbook should not error for %s", a)
|
||||
pair := getPair(t, a)
|
||||
t.Run(a.String()+" "+pair.String(), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
o, err := g.UpdateOrderbook(t.Context(), pair, a)
|
||||
require.NoError(t, err)
|
||||
if a != asset.Options { // Options orderbooks can be empty
|
||||
assert.NotEmpty(t, o.Bids)
|
||||
assert.NotEmpty(t, o.Asks)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -526,30 +526,25 @@ type OrderbookData struct {
|
||||
Bids [][2]types.Number `json:"bids"`
|
||||
}
|
||||
|
||||
// MakeOrderbook parse Orderbook asks/bids Price and Amount and create an Orderbook Instance with asks and bids data in []OrderbookItem.
|
||||
func (a *OrderbookData) MakeOrderbook() (*Orderbook, error) {
|
||||
ob := &Orderbook{
|
||||
ID: a.ID,
|
||||
Current: a.Current,
|
||||
Update: a.Update,
|
||||
Asks: make([]OrderbookItem, len(a.Asks)),
|
||||
Bids: make([]OrderbookItem, len(a.Bids)),
|
||||
}
|
||||
// MakeOrderbook converts OrderbookData into an Orderbook
|
||||
func (a *OrderbookData) MakeOrderbook() *Orderbook {
|
||||
asks := make([]OrderbookItem, len(a.Asks))
|
||||
for x := range a.Asks {
|
||||
ob.Asks[x].Price = a.Asks[x][0]
|
||||
ob.Asks[x].Amount = a.Asks[x][1].Float64()
|
||||
asks[x].Price = a.Asks[x][0]
|
||||
asks[x].Amount = a.Asks[x][1]
|
||||
}
|
||||
bids := make([]OrderbookItem, len(a.Bids))
|
||||
for x := range a.Bids {
|
||||
ob.Bids[x].Price = a.Bids[x][0]
|
||||
ob.Bids[x].Amount = a.Bids[x][1].Float64()
|
||||
bids[x].Price = a.Bids[x][0]
|
||||
bids[x].Amount = a.Bids[x][1]
|
||||
}
|
||||
return ob, nil
|
||||
return &Orderbook{ID: a.ID, Current: a.Current, Update: a.Update, Asks: asks, Bids: bids}
|
||||
}
|
||||
|
||||
// OrderbookItem stores an orderbook item
|
||||
type OrderbookItem struct {
|
||||
Price types.Number `json:"p"`
|
||||
Amount float64 `json:"s"`
|
||||
Amount types.Number `json:"s"`
|
||||
}
|
||||
|
||||
// Orderbook stores the orderbook data
|
||||
|
||||
@@ -353,23 +353,23 @@ func (g *Gateio) processCandlestick(incoming []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gateio) processOrderbookTicker(incoming []byte, updatePushedAt time.Time) error {
|
||||
func (g *Gateio) processOrderbookTicker(incoming []byte, lastPushed time.Time) error {
|
||||
var data WsOrderbookTickerData
|
||||
if err := json.Unmarshal(incoming, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.Websocket.Orderbook.LoadSnapshot(&orderbook.Base{
|
||||
Exchange: g.Name,
|
||||
Pair: data.Pair,
|
||||
Asset: asset.Spot,
|
||||
LastUpdated: data.UpdateTime.Time(),
|
||||
UpdatePushedAt: updatePushedAt,
|
||||
Bids: []orderbook.Tranche{{Price: data.BestBidPrice.Float64(), Amount: data.BestBidAmount.Float64()}},
|
||||
Asks: []orderbook.Tranche{{Price: data.BestAskPrice.Float64(), Amount: data.BestAskAmount.Float64()}},
|
||||
Exchange: g.Name,
|
||||
Pair: data.Pair,
|
||||
Asset: asset.Spot,
|
||||
LastUpdated: data.UpdateTime.Time(),
|
||||
LastPushed: lastPushed,
|
||||
Bids: []orderbook.Tranche{{Price: data.BestBidPrice.Float64(), Amount: data.BestBidAmount.Float64()}},
|
||||
Asks: []orderbook.Tranche{{Price: data.BestAskPrice.Float64(), Amount: data.BestAskAmount.Float64()}},
|
||||
})
|
||||
}
|
||||
|
||||
func (g *Gateio) processOrderbookUpdate(ctx context.Context, incoming []byte, updatePushedAt time.Time) error {
|
||||
func (g *Gateio) processOrderbookUpdate(ctx context.Context, incoming []byte, lastPushed time.Time) error {
|
||||
var data WsOrderbookUpdate
|
||||
if err := json.Unmarshal(incoming, &data); err != nil {
|
||||
return err
|
||||
@@ -385,18 +385,18 @@ func (g *Gateio) processOrderbookUpdate(ctx context.Context, incoming []byte, up
|
||||
bids[x].Amount = data.Bids[x][1].Float64()
|
||||
}
|
||||
return g.wsOBUpdateMgr.ProcessOrderbookUpdate(ctx, g, data.FirstUpdateID, &orderbook.Update{
|
||||
UpdateID: data.LastUpdateID,
|
||||
UpdateTime: data.UpdateTime.Time(),
|
||||
UpdatePushedAt: updatePushedAt,
|
||||
Pair: data.Pair,
|
||||
Asset: asset.Spot,
|
||||
Asks: asks,
|
||||
Bids: bids,
|
||||
AllowEmpty: true,
|
||||
UpdateID: data.LastUpdateID,
|
||||
UpdateTime: data.UpdateTime.Time(),
|
||||
LastPushed: lastPushed,
|
||||
Pair: data.Pair,
|
||||
Asset: asset.Spot,
|
||||
Asks: asks,
|
||||
Bids: bids,
|
||||
AllowEmpty: true,
|
||||
})
|
||||
}
|
||||
|
||||
func (g *Gateio) processOrderbookSnapshot(incoming []byte, updatePushedAt time.Time) error {
|
||||
func (g *Gateio) processOrderbookSnapshot(incoming []byte, lastPushed time.Time) error {
|
||||
var data WsOrderbookSnapshot
|
||||
if err := json.Unmarshal(incoming, &data); err != nil {
|
||||
return err
|
||||
@@ -416,13 +416,13 @@ func (g *Gateio) processOrderbookSnapshot(incoming []byte, updatePushedAt time.T
|
||||
for _, a := range standardMarginAssetTypes {
|
||||
if enabled, _ := g.CurrencyPairs.IsPairEnabled(data.CurrencyPair, a); enabled {
|
||||
if err := g.Websocket.Orderbook.LoadSnapshot(&orderbook.Base{
|
||||
Exchange: g.Name,
|
||||
Pair: data.CurrencyPair,
|
||||
Asset: a,
|
||||
LastUpdated: data.UpdateTime.Time(),
|
||||
UpdatePushedAt: updatePushedAt,
|
||||
Bids: bids,
|
||||
Asks: asks,
|
||||
Exchange: g.Name,
|
||||
Pair: data.CurrencyPair,
|
||||
Asset: a,
|
||||
LastUpdated: data.UpdateTime.Time(),
|
||||
LastPushed: lastPushed,
|
||||
Bids: bids,
|
||||
Asks: asks,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -423,18 +423,18 @@ func (g *Gateio) processFuturesOrderbookUpdate(ctx context.Context, incoming []b
|
||||
}
|
||||
|
||||
return g.wsOBUpdateMgr.ProcessOrderbookUpdate(ctx, g, data.FirstUpdatedID, &orderbook.Update{
|
||||
UpdateID: data.LastUpdatedID,
|
||||
UpdateTime: data.Timestamp.Time(),
|
||||
UpdatePushedAt: pushTime,
|
||||
Pair: data.ContractName,
|
||||
Asset: a,
|
||||
Asks: asks,
|
||||
Bids: bids,
|
||||
AllowEmpty: true,
|
||||
UpdateID: data.LastUpdatedID,
|
||||
UpdateTime: data.Timestamp.Time(),
|
||||
LastPushed: pushTime,
|
||||
Pair: data.ContractName,
|
||||
Asset: a,
|
||||
Asks: asks,
|
||||
Bids: bids,
|
||||
AllowEmpty: true,
|
||||
})
|
||||
}
|
||||
|
||||
func (g *Gateio) processFuturesOrderbookSnapshot(event string, incoming []byte, assetType asset.Item, updatePushedAt time.Time) error {
|
||||
func (g *Gateio) processFuturesOrderbookSnapshot(event string, incoming []byte, assetType asset.Item, lastPushed time.Time) error {
|
||||
if event == "all" {
|
||||
var data WsFuturesOrderbookSnapshot
|
||||
err := json.Unmarshal(incoming, &data)
|
||||
@@ -446,7 +446,7 @@ func (g *Gateio) processFuturesOrderbookSnapshot(event string, incoming []byte,
|
||||
Exchange: g.Name,
|
||||
Pair: data.Contract,
|
||||
LastUpdated: data.Timestamp.Time(),
|
||||
UpdatePushedAt: updatePushedAt,
|
||||
LastPushed: lastPushed,
|
||||
VerifyOrderbook: g.CanVerifyOrderbook,
|
||||
}
|
||||
base.Asks = make([]orderbook.Tranche, len(data.Asks))
|
||||
@@ -501,8 +501,8 @@ func (g *Gateio) processFuturesOrderbookSnapshot(event string, incoming []byte,
|
||||
Asset: assetType,
|
||||
Exchange: g.Name,
|
||||
Pair: currencyPair,
|
||||
LastUpdated: updatePushedAt,
|
||||
UpdatePushedAt: updatePushedAt,
|
||||
LastUpdated: lastPushed,
|
||||
LastPushed: lastPushed,
|
||||
VerifyOrderbook: g.CanVerifyOrderbook,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -514,18 +514,18 @@ func (g *Gateio) processOptionsOrderbookUpdate(ctx context.Context, incoming []b
|
||||
bids[x].Amount = data.Bids[x].Size
|
||||
}
|
||||
return g.wsOBUpdateMgr.ProcessOrderbookUpdate(ctx, g, data.FirstUpdatedID, &orderbook.Update{
|
||||
UpdateID: data.LastUpdatedID,
|
||||
UpdateTime: data.Timestamp.Time(),
|
||||
UpdatePushedAt: pushTime,
|
||||
Pair: data.ContractName,
|
||||
Asset: a,
|
||||
Asks: asks,
|
||||
Bids: bids,
|
||||
AllowEmpty: true,
|
||||
UpdateID: data.LastUpdatedID,
|
||||
UpdateTime: data.Timestamp.Time(),
|
||||
LastPushed: pushTime,
|
||||
Pair: data.ContractName,
|
||||
Asset: a,
|
||||
Asks: asks,
|
||||
Bids: bids,
|
||||
AllowEmpty: true,
|
||||
})
|
||||
}
|
||||
|
||||
func (g *Gateio) processOptionsOrderbookSnapshotPushData(event string, incoming []byte, updatePushedAt time.Time) error {
|
||||
func (g *Gateio) processOptionsOrderbookSnapshotPushData(event string, incoming []byte, lastPushed time.Time) error {
|
||||
if event == "all" {
|
||||
var data WsOptionsOrderbookSnapshot
|
||||
err := json.Unmarshal(incoming, &data)
|
||||
@@ -537,7 +537,7 @@ func (g *Gateio) processOptionsOrderbookSnapshotPushData(event string, incoming
|
||||
Exchange: g.Name,
|
||||
Pair: data.Contract,
|
||||
LastUpdated: data.Timestamp.Time(),
|
||||
UpdatePushedAt: updatePushedAt,
|
||||
LastPushed: lastPushed,
|
||||
VerifyOrderbook: g.CanVerifyOrderbook,
|
||||
}
|
||||
base.Asks = make([]orderbook.Tranche, len(data.Asks))
|
||||
@@ -590,8 +590,8 @@ func (g *Gateio) processOptionsOrderbookSnapshotPushData(event string, incoming
|
||||
Asset: asset.Options,
|
||||
Exchange: g.Name,
|
||||
Pair: currencyPair,
|
||||
LastUpdated: updatePushedAt,
|
||||
UpdatePushedAt: updatePushedAt,
|
||||
LastUpdated: lastPushed,
|
||||
LastPushed: lastPushed,
|
||||
VerifyOrderbook: g.CanVerifyOrderbook,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -695,18 +695,19 @@ func (g *Gateio) UpdateOrderbookWithLimit(ctx context.Context, p currency.Pair,
|
||||
Pair: p.Upper(),
|
||||
LastUpdateID: o.ID,
|
||||
LastUpdated: o.Update.Time(),
|
||||
LastPushed: o.Current.Time(),
|
||||
}
|
||||
book.Bids = make(orderbook.Tranches, len(o.Bids))
|
||||
for x := range o.Bids {
|
||||
book.Bids[x] = orderbook.Tranche{
|
||||
Amount: o.Bids[x].Amount,
|
||||
Amount: o.Bids[x].Amount.Float64(),
|
||||
Price: o.Bids[x].Price.Float64(),
|
||||
}
|
||||
}
|
||||
book.Asks = make(orderbook.Tranches, len(o.Asks))
|
||||
for x := range o.Asks {
|
||||
book.Asks[x] = orderbook.Tranche{
|
||||
Amount: o.Asks[x].Amount,
|
||||
Amount: o.Asks[x].Amount.Float64(),
|
||||
Price: o.Asks[x].Price.Float64(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,14 +24,14 @@ func TestProcessOrderbookUpdate(t *testing.T) {
|
||||
|
||||
pair := currency.NewPair(currency.BABY, currency.BABYDOGE)
|
||||
err = g.Websocket.Orderbook.LoadSnapshot(&orderbook.Base{
|
||||
Exchange: g.Name,
|
||||
Pair: pair,
|
||||
Asset: asset.USDTMarginedFutures,
|
||||
Bids: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
||||
Asks: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
||||
LastUpdated: time.Now(),
|
||||
UpdatePushedAt: time.Now(),
|
||||
LastUpdateID: 1336,
|
||||
Exchange: g.Name,
|
||||
Pair: pair,
|
||||
Asset: asset.USDTMarginedFutures,
|
||||
Bids: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
||||
Asks: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
||||
LastUpdated: time.Now(),
|
||||
LastPushed: time.Now(),
|
||||
LastUpdateID: 1336,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -146,14 +146,14 @@ func TestApplyPendingUpdates(t *testing.T) {
|
||||
m := newWsOBUpdateManager(defaultWSSnapshotSyncDelay)
|
||||
pair := currency.NewPair(currency.LTC, currency.USDT)
|
||||
err := g.Websocket.Orderbook.LoadSnapshot(&orderbook.Base{
|
||||
Exchange: g.Name,
|
||||
Pair: pair,
|
||||
Asset: asset.USDTMarginedFutures,
|
||||
Bids: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
||||
Asks: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
||||
LastUpdated: time.Now(),
|
||||
UpdatePushedAt: time.Now(),
|
||||
LastUpdateID: 1335,
|
||||
Exchange: g.Name,
|
||||
Pair: pair,
|
||||
Asset: asset.USDTMarginedFutures,
|
||||
Bids: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
||||
Asks: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
||||
LastUpdated: time.Now(),
|
||||
LastPushed: time.Now(),
|
||||
LastUpdateID: 1335,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user