Skip to content

Commit

Permalink
pkg/exchange: upgrade market trade ws to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
bailantaotao committed Aug 12, 2024
1 parent 4ae3426 commit 97093da
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 148 deletions.
113 changes: 33 additions & 80 deletions pkg/exchange/bitget/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,19 +354,21 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"28303.43",
"0.0452",
"sell"
],
[
"1697697794663",
"28345.67",
"0.1234",
"sell"
]
],
{
"ts":"1723476690562",
"price":"59440.52",
"size":"0.018545",
"side":"sell",
"tradeId":"1206914205132210181"
},
{
"ts":"1723476690562",
"price":"59440.52",
"size":"0.001255",
"side":"sell",
"tradeId":"1206914205132210179"
}
],
"ts":1697697791670
}`

Expand All @@ -378,17 +380,19 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
assert.Equal(t, MarketTradeEvent{
Events: MarketTradeSlice{
{
Ts: types.NewMillisecondTimestampFromInt(1697697791663),
Price: fixedpoint.NewFromFloat(28303.43),
Size: fixedpoint.NewFromFloat(0.0452),
Side: "sell",
Ts: types.NewMillisecondTimestampFromInt(1723476690562),
Price: fixedpoint.NewFromFloat(59440.52),
Size: fixedpoint.NewFromFloat(0.018545),
Side: "sell",
TradeId: 1206914205132210181,
},

{
Ts: types.NewMillisecondTimestampFromInt(1697697794663),
Price: fixedpoint.NewFromFloat(28345.67),
Size: fixedpoint.NewFromFloat(0.1234),
Side: "sell",
Ts: types.NewMillisecondTimestampFromInt(1723476690562),
Price: fixedpoint.NewFromFloat(59440.52),
Size: fixedpoint.NewFromFloat(0.001255),
Side: "sell",
TradeId: 1206914205132210179,
},
},
actionType: actionType,
Expand All @@ -407,29 +411,6 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
})
})

t.Run("Unexpected length of market trade", func(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"28303.43",
"28303.43",
"0.0452",
"sell"
]
],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "unexpected trades length")
})

t.Run("Unexpected timestamp", func(t *testing.T) {
input := `{
"action":"%s",
Expand All @@ -438,18 +419,11 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"TIMESTAMP",
"28303.43",
"0.0452",
"sell"
]
],
"data":[{"ts":"TIMESTAMP","price":"59440.52","size":"0.018545","side":"sell","tradeId":"1206914205132210181"},{"ts":"1723476690562","price":"59440.52","size":"0.001255","side":"sell","tradeId":"1206914205132210179"}],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "timestamp")
assert.ErrorContains(t, err, "failed to unmarshal data")
})

t.Run("Unexpected price", func(t *testing.T) {
Expand All @@ -460,18 +434,11 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"1p",
"0.0452",
"sell"
]
],
"data":[{"ts":"1723476690562","price":"UNEXPECTED","size":"0.018545","side":"sell","tradeId":"1206914205132210181"},{"ts":"1723476690562","price":"59440.52","size":"0.001255","side":"sell","tradeId":"1206914205132210179"}],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "price")
assert.ErrorContains(t, err, "failed to unmarshal data")
})

t.Run("Unexpected size", func(t *testing.T) {
Expand All @@ -482,18 +449,11 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"28303.43",
"2v",
"sell"
]
],
"data":[{"ts":"1723476690562","price":"59440.52","size":"2v","side":"sell","tradeId":"1206914205132210181"},{"ts":"1723476690562","price":"59440.52","size":"0.001255","side":"sell","tradeId":"1206914205132210179"}],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "size")
assert.ErrorContains(t, err, "failed to unmarshal data")
})

t.Run("Unexpected side", func(t *testing.T) {
Expand All @@ -504,18 +464,11 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
"channel":"trade",
"instId":"BTCUSDT"
},
"data":[
[
"1697697791663",
"28303.43",
"0.0452",
12345
]
],
"data":[{"ts":"1723476690562","price":"59440.52","size":"0.018545","side":"ssss","tradeId":"1206914205132210181"},{"ts":"1723476690562","price":"59440.52","size":"0.001255","side":"sell","tradeId":"1206914205132210179"}],
"ts":1697697791670
}`
_, err := parseWebSocketEvent([]byte(input))
assert.ErrorContains(t, err, "side")
assert.ErrorContains(t, err, "failed to unmarshal data")
})
}

Expand Down
90 changes: 22 additions & 68 deletions pkg/exchange/bitget/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ const (
SideSell SideType = "sell"
)

func (s *SideType) UnmarshalJSON(b []byte) error {
var a string
err := json.Unmarshal(b, &a)
if err != nil {
return err
}

switch SideType(a) {
case SideSell, SideBuy:
*s = SideType(a)
return nil
default:
return fmt.Errorf("unexpected side type: %s", b)
}
}

func (s SideType) ToGlobal() (types.SideType, error) {
switch s {
case SideBuy:
Expand All @@ -174,85 +190,23 @@ func (s SideType) ToGlobal() (types.SideType, error) {
}

type MarketTrade struct {
Ts types.MillisecondTimestamp
Price fixedpoint.Value
Size fixedpoint.Value
Side SideType
Ts types.MillisecondTimestamp
Price fixedpoint.Value
Size fixedpoint.Value
Side SideType
TradeId types.StrInt64
}

type MarketTradeSlice []MarketTrade

func (m *MarketTradeSlice) UnmarshalJSON(b []byte) error {
if m == nil {
return errors.New("nil pointer of market trade slice")
}
s, err := parseMarketTradeSliceJSON(b)
if err != nil {
return err
}

*m = s
return nil
}

// ParseMarketTradeSliceJSON tries to parse a 2 dimensional string array into a MarketTradeSlice
//
// [
//
// [
// "1697694819663",
// "28312.97",
// "0.1653",
// "sell"
// ],
// [
// "1697694818663",
// "28313",
// "0.1598",
// "buy"
// ]
//
// ]
func parseMarketTradeSliceJSON(in []byte) (slice MarketTradeSlice, err error) {
var rawTrades [][]json.RawMessage

err = json.Unmarshal(in, &rawTrades)
if err != nil {
return slice, err
}

for _, raw := range rawTrades {
if len(raw) != 4 {
return nil, fmt.Errorf("unexpected trades length: %d, data: %q", len(raw), raw)
}
var trade MarketTrade
if err = json.Unmarshal(raw[0], &trade.Ts); err != nil {
return nil, fmt.Errorf("failed to unmarshal into timestamp: %q", raw[0])
}
if err = json.Unmarshal(raw[1], &trade.Price); err != nil {
return nil, fmt.Errorf("failed to unmarshal into price: %q", raw[1])
}
if err = json.Unmarshal(raw[2], &trade.Size); err != nil {
return nil, fmt.Errorf("failed to unmarshal into size: %q", raw[2])
}
if err = json.Unmarshal(raw[3], &trade.Side); err != nil {
return nil, fmt.Errorf("failed to unmarshal into side: %q", raw[3])
}

slice = append(slice, trade)
}

return slice, nil
}

func (m MarketTrade) ToGlobal(symbol string) (types.Trade, error) {
side, err := m.Side.ToGlobal()
if err != nil {
return types.Trade{}, err
}

return types.Trade{
ID: 0, // not supported
ID: uint64(m.TradeId),
OrderID: 0, // not supported
Exchange: types.ExchangeBitget,
Price: m.Price,
Expand Down

0 comments on commit 97093da

Please sign in to comment.