Skip to content

Commit f17230e

Browse files
author
Ryan O'Hara-Reid
committed
gateio_ob_v2_cont: thrasher-corp#2060 includes thrasher-corp#2045
1 parent e5960bf commit f17230e

18 files changed

+420
-48
lines changed

common/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ var (
7575
ErrGettingField = errors.New("error getting field")
7676
ErrSettingField = errors.New("error setting field")
7777
ErrParsingWSField = errors.New("error parsing websocket field")
78+
ErrMalformedData = errors.New("malformed data")
7879
)
7980

8081
var (

currency/pair.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import (
77
"unicode"
88
)
99

10+
// Public errors
11+
var (
12+
ErrCreatingPair = errors.New("error creating currency pair")
13+
)
14+
1015
var (
11-
errCannotCreatePair = errors.New("cannot create currency pair")
1216
errDelimiterNotFound = errors.New("delimiter not found")
1317
errDelimiterCannotBeEmpty = errors.New("delimiter cannot be empty")
1418
)
@@ -70,7 +74,7 @@ func NewPairWithDelimiter(base, quote, delimiter string) Pair {
7074
// with or without delimiter
7175
func NewPairFromString(currencyPair string) (Pair, error) {
7276
if len(currencyPair) < 3 {
73-
return EMPTYPAIR, fmt.Errorf("%w from %s string too short to be a currency pair", errCannotCreatePair, currencyPair)
77+
return EMPTYPAIR, fmt.Errorf("%w from %s string too short to be a currency pair", ErrCreatingPair, currencyPair)
7478
}
7579

7680
for x := range currencyPair {

currency/pair_methods.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (p *Pair) UnmarshalJSON(d []byte) error {
6161
// incorrectly converted to DUS-KUSDT, ELKRW (Bithumb) which will convert
6262
// converted to ELK-RW and HTUSDT (Lbank) which will be incorrectly
6363
// converted to HTU-SDT.
64-
return fmt.Errorf("%w from %s cannot ensure pair is in correct format, please use exchange method MatchSymbolWithAvailablePairs", errCannotCreatePair, pair)
64+
return fmt.Errorf("%w from %s cannot ensure pair is in correct format, please use exchange method MatchSymbolWithAvailablePairs", ErrCreatingPair, pair)
6565
}
6666

6767
// MarshalJSON conforms type to the marshaler interface

currency/pair_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestPairUnmarshalJSON(t *testing.T) {
5454
assert.Equal(t, "usd", p.Quote.String(), "Quote should be correct")
5555
assert.Equal(t, "_", p.Delimiter, "Delimiter should be correct")
5656

57-
assert.ErrorIs(t, p.UnmarshalJSON([]byte(`"btcusd"`)), errCannotCreatePair, "UnmarshalJSON with no delimiter should error")
57+
assert.ErrorIs(t, p.UnmarshalJSON([]byte(`"btcusd"`)), ErrCreatingPair, "UnmarshalJSON with no delimiter should error")
5858

5959
assert.NoError(t, p.UnmarshalJSON([]byte(`""`)), "UnmarshalJSON should not error on empty value")
6060
assert.Equal(t, EMPTYPAIR, p, "UnmarshalJSON empty value should give EMPTYPAIR")

currency/pairs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestPairsFromString(t *testing.T) {
5454
_, err := NewPairsFromString("", "")
5555
assert.ErrorIs(t, err, errNoDelimiter)
5656
_, err = NewPairsFromString("", ",")
57-
assert.ErrorIs(t, err, errCannotCreatePair)
57+
assert.ErrorIs(t, err, ErrCreatingPair)
5858

5959
pairs, err := NewPairsFromString("ALGO-AUD,BAT-AUD,BCH-AUD,BSV-AUD,BTC-AUD,COMP-AUD,ENJ-AUD,ETC-AUD,ETH-AUD,ETH-BTC,GNT-AUD,LINK-AUD,LTC-AUD,LTC-BTC,MCAU-AUD,OMG-AUD,POWR-AUD,UNI-AUD,USDT-AUD,XLM-AUD,XRP-AUD,XRP-BTC", ",")
6060
require.NoError(t, err)

exchanges/deribit/deribit_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ var (
4242
errInvalidID = errors.New("invalid id")
4343
errInvalidMarginModel = errors.New("missing margin model")
4444
errInvalidEmailAddress = errors.New("invalid email address")
45-
errMalformedData = errors.New("malformed data")
4645
errWebsocketConnectionNotAuthenticated = errors.New("websocket connection is not authenticated")
4746
errResolutionNotSet = errors.New("resolution not set")
4847
errInvalidDestinationID = errors.New("invalid destination id")

exchanges/deribit/deribit_websocket.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func (e *Exchange) wsSendHeartbeat(ctx context.Context) {
335335

336336
func (e *Exchange) processUserOrders(respRaw []byte, channels []string) error {
337337
if len(channels) != 4 && len(channels) != 5 {
338-
return fmt.Errorf("%w, expected format 'user.orders.{instrument_name}.raw, user.orders.{instrument_name}.{interval}, user.orders.{kind}.{currency}.raw, or user.orders.{kind}.{currency}.{interval}', but found %s", errMalformedData, strings.Join(channels, "."))
338+
return fmt.Errorf("%w, expected format 'user.orders.{instrument_name}.raw, user.orders.{instrument_name}.{interval}, user.orders.{kind}.{currency}.raw, or user.orders.{kind}.{currency}.{interval}', but found %s", common.ErrMalformedData, strings.Join(channels, "."))
339339
}
340340
var response wsResponse
341341
orderData := []WsOrder{}
@@ -384,7 +384,7 @@ func (e *Exchange) processUserOrders(respRaw []byte, channels []string) error {
384384

385385
func (e *Exchange) processUserOrderChanges(respRaw []byte, channels []string) error {
386386
if len(channels) < 4 || len(channels) > 5 {
387-
return fmt.Errorf("%w, expected format 'trades.{instrument_name}.{interval} or trades.{kind}.{currency}.{interval}', but found %s", errMalformedData, strings.Join(channels, "."))
387+
return fmt.Errorf("%w, expected format 'trades.{instrument_name}.{interval} or trades.{kind}.{currency}.{interval}', but found %s", common.ErrMalformedData, strings.Join(channels, "."))
388388
}
389389
var response wsResponse
390390
changeData := &wsChanges{}
@@ -494,7 +494,7 @@ func (e *Exchange) processTrades(respRaw []byte, channels []string) error {
494494
}
495495

496496
if len(channels) < 3 || len(channels) > 5 {
497-
return fmt.Errorf("%w, expected format 'trades.{instrument_name}.{interval} or trades.{kind}.{currency}.{interval}', but found %s", errMalformedData, strings.Join(channels, "."))
497+
return fmt.Errorf("%w, expected format 'trades.{instrument_name}.{interval} or trades.{kind}.{currency}.{interval}', but found %s", common.ErrMalformedData, strings.Join(channels, "."))
498498
}
499499
var response wsResponse
500500
var tradeList []wsTrade
@@ -538,7 +538,7 @@ func (e *Exchange) processTrades(respRaw []byte, channels []string) error {
538538

539539
func (e *Exchange) processIncrementalTicker(respRaw []byte, channels []string) error {
540540
if len(channels) != 2 {
541-
return fmt.Errorf("%w, expected format 'incremental_ticker.{instrument_name}', but found %s", errMalformedData, strings.Join(channels, "."))
541+
return fmt.Errorf("%w, expected format 'incremental_ticker.{instrument_name}', but found %s", common.ErrMalformedData, strings.Join(channels, "."))
542542
}
543543
a, cp, err := getAssetPairByInstrument(channels[1])
544544
if err != nil {
@@ -570,7 +570,7 @@ func (e *Exchange) processIncrementalTicker(respRaw []byte, channels []string) e
570570

571571
func (e *Exchange) processInstrumentTicker(respRaw []byte, channels []string) error {
572572
if len(channels) != 3 {
573-
return fmt.Errorf("%w, expected format 'ticker.{instrument_name}.{interval}', but found %s", errMalformedData, strings.Join(channels, "."))
573+
return fmt.Errorf("%w, expected format 'ticker.{instrument_name}.{interval}', but found %s", common.ErrMalformedData, strings.Join(channels, "."))
574574
}
575575
return e.processTicker(respRaw, channels)
576576
}
@@ -625,7 +625,7 @@ func (e *Exchange) processData(respRaw []byte, result any) error {
625625

626626
func (e *Exchange) processCandleChart(respRaw []byte, channels []string) error {
627627
if len(channels) != 4 {
628-
return fmt.Errorf("%w, expected format 'chart.trades.{instrument_name}.{resolution}', but found %s", errMalformedData, strings.Join(channels, "."))
628+
return fmt.Errorf("%w, expected format 'chart.trades.{instrument_name}.{resolution}', but found %s", common.ErrInvalidResponse, strings.Join(channels, "."))
629629
}
630630
a, cp, err := getAssetPairByInstrument(channels[2])
631631
if err != nil {
@@ -668,15 +668,15 @@ func (e *Exchange) processOrderbook(respRaw []byte, channels []string) error {
668668
asks := make(orderbook.Levels, 0, len(orderbookData.Asks))
669669
for x := range orderbookData.Asks {
670670
if len(orderbookData.Asks[x]) != 3 {
671-
return errMalformedData
671+
return common.ErrMalformedData
672672
}
673673
price, okay := orderbookData.Asks[x][1].(float64)
674674
if !okay {
675-
return fmt.Errorf("%w, invalid orderbook price", errMalformedData)
675+
return fmt.Errorf("%w, invalid orderbook price", common.ErrMalformedData)
676676
}
677677
amount, okay := orderbookData.Asks[x][2].(float64)
678678
if !okay {
679-
return fmt.Errorf("%w, invalid amount", errMalformedData)
679+
return fmt.Errorf("%w, invalid amount", common.ErrMalformedData)
680680
}
681681
asks = append(asks, orderbook.Level{
682682
Price: price,
@@ -686,17 +686,17 @@ func (e *Exchange) processOrderbook(respRaw []byte, channels []string) error {
686686
bids := make(orderbook.Levels, 0, len(orderbookData.Bids))
687687
for x := range orderbookData.Bids {
688688
if len(orderbookData.Bids[x]) != 3 {
689-
return errMalformedData
689+
return common.ErrMalformedData
690690
}
691691
price, okay := orderbookData.Bids[x][1].(float64)
692692
if !okay {
693-
return fmt.Errorf("%w, invalid orderbook price", errMalformedData)
693+
return fmt.Errorf("%w, invalid orderbook price", common.ErrMalformedData)
694694
} else if price == 0.0 {
695695
continue
696696
}
697697
amount, okay := orderbookData.Bids[x][2].(float64)
698698
if !okay {
699-
return fmt.Errorf("%w, invalid amount", errMalformedData)
699+
return fmt.Errorf("%w, invalid amount", common.ErrMalformedData)
700700
}
701701
bids = append(bids, orderbook.Level{
702702
Price: price,
@@ -737,17 +737,17 @@ func (e *Exchange) processOrderbook(respRaw []byte, channels []string) error {
737737
asks := make(orderbook.Levels, 0, len(orderbookData.Asks))
738738
for x := range orderbookData.Asks {
739739
if len(orderbookData.Asks[x]) != 2 {
740-
return errMalformedData
740+
return common.ErrMalformedData
741741
}
742742
price, okay := orderbookData.Asks[x][0].(float64)
743743
if !okay {
744-
return fmt.Errorf("%w, invalid orderbook price", errMalformedData)
744+
return fmt.Errorf("%w, invalid orderbook price", common.ErrMalformedData)
745745
} else if price == 0 {
746746
continue
747747
}
748748
amount, okay := orderbookData.Asks[x][1].(float64)
749749
if !okay {
750-
return fmt.Errorf("%w, invalid amount", errMalformedData)
750+
return fmt.Errorf("%w, invalid amount", common.ErrMalformedData)
751751
}
752752
asks = append(asks, orderbook.Level{
753753
Price: price,
@@ -757,17 +757,17 @@ func (e *Exchange) processOrderbook(respRaw []byte, channels []string) error {
757757
bids := make([]orderbook.Level, 0, len(orderbookData.Bids))
758758
for x := range orderbookData.Bids {
759759
if len(orderbookData.Bids[x]) != 2 {
760-
return errMalformedData
760+
return common.ErrMalformedData
761761
}
762762
price, okay := orderbookData.Bids[x][0].(float64)
763763
if !okay {
764-
return fmt.Errorf("%w, invalid orderbook price", errMalformedData)
764+
return fmt.Errorf("%w, invalid orderbook price", common.ErrMalformedData)
765765
} else if price == 0 {
766766
continue
767767
}
768768
amount, okay := orderbookData.Bids[x][1].(float64)
769769
if !okay {
770-
return fmt.Errorf("%w, invalid amount", errMalformedData)
770+
return fmt.Errorf("%w, invalid amount", common.ErrMalformedData)
771771
}
772772
bids = append(bids, orderbook.Level{
773773
Price: price,

exchanges/gateio/gateio.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ type Exchange struct {
194194

195195
messageIDSeq common.Counter
196196
wsOBUpdateMgr *wsOBUpdateManager
197+
wsOBResubMgr *wsOBResubManager
197198
}
198199

199200
// ***************************************** SubAccounts ********************************

exchanges/gateio/gateio_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,17 @@ type WsOrderbookUpdate struct {
20882088
Asks orderbook.LevelsArrayPriceAmount `json:"a"`
20892089
}
20902090

2091+
// WsOrderbookUpdateWithSnapshot represents websocket orderbook update push data
2092+
type WsOrderbookUpdateWithSnapshot struct {
2093+
UpdateTime types.Time `json:"t"`
2094+
Full bool `json:"full"`
2095+
Channel string `json:"s"`
2096+
FirstUpdateID int64 `json:"U"`
2097+
LastUpdateID int64 `json:"u"`
2098+
Bids [][2]types.Number `json:"b"`
2099+
Asks [][2]types.Number `json:"a"`
2100+
}
2101+
20912102
// WsOrderbookSnapshot represents a websocket orderbook snapshot push data
20922103
type WsOrderbookSnapshot struct {
20932104
UpdateTime types.Time `json:"t"`

0 commit comments

Comments
 (0)