Skip to content

Commit

Permalink
maxapi: parse fd field and optimize trade snapshot parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 24, 2023
1 parent bded2ed commit 5f2ead4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
50 changes: 19 additions & 31 deletions pkg/exchange/max/maxapi/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ type TradeUpdate struct {
Volume string `json:"v"`
Market string `json:"M"`

Fee string `json:"f"`
FeeCurrency string `json:"fc"`
Timestamp int64 `json:"T"`
UpdateTime int64 `json:"TU"`
Fee string `json:"f"`
FeeCurrency string `json:"fc"`
FeeDiscounted bool `json:"fd"`

Timestamp int64 `json:"T"`
UpdateTime int64 `json:"TU"`

OrderID uint64 `json:"oi"`

Expand All @@ -128,40 +130,26 @@ func parseTradeUpdate(v *fastjson.Value) TradeUpdate {

type TradeUpdateEvent struct {
BaseEvent

Trades []TradeUpdate `json:"t"`
}

func parseTradeUpdateEvent(v *fastjson.Value) *TradeUpdateEvent {
var e TradeUpdateEvent
e.Event = string(v.GetStringBytes("e"))
e.Timestamp = v.GetInt64("T")

for _, tv := range v.GetArray("t") {
e.Trades = append(e.Trades, parseTradeUpdate(tv))
}

return &e
}

type TradeSnapshot []TradeUpdate

type TradeSnapshotEvent struct {
BaseEvent

Trades []TradeUpdate `json:"t"`
}

func parseTradeSnapshotEvent(v *fastjson.Value) *TradeSnapshotEvent {
var e TradeSnapshotEvent
e.Event = string(v.GetStringBytes("e"))
e.Timestamp = v.GetInt64("T")

for _, tv := range v.GetArray("t") {
e.Trades = append(e.Trades, parseTradeUpdate(tv))
}
func parseTradeUpdateEvent(v *fastjson.Value) (*TradeUpdateEvent, error) {
jsonBytes := v.String()
var e TradeUpdateEvent
err := json.Unmarshal([]byte(jsonBytes), &e)
return &e, err
}

return &e
func parseTradeSnapshotEvent(v *fastjson.Value) (*TradeSnapshotEvent, error) {
jsonBytes := v.String()
var e TradeSnapshotEvent
err := json.Unmarshal([]byte(jsonBytes), &e)
return &e, err
}

type BalanceMessage struct {
Expand Down Expand Up @@ -252,10 +240,10 @@ func ParseUserEvent(v *fastjson.Value) (interface{}, error) {
return parseOrderUpdateEvent(v), nil

case "trade_snapshot", "mwallet_trade_snapshot":
return parseTradeSnapshotEvent(v), nil
return parseTradeSnapshotEvent(v)

case "trade_update", "mwallet_trade_update":
return parseTradeUpdateEvent(v), nil
return parseTradeUpdateEvent(v)

case "ad_ratio_snapshot", "ad_ratio_update":
return parseADRatioEvent(v)
Expand Down
45 changes: 45 additions & 0 deletions pkg/exchange/max/maxapi/userdata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package max

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/valyala/fastjson"
)

func Test_parseTradeSnapshotEvent(t *testing.T) {
fv, err := fastjson.Parse(`{
"c": "user",
"e": "trade_snapshot",
"t": [{
"i": 68444,
"p": "21499.0",
"v": "0.2658",
"M": "ethtwd",
"T": 1521726960357,
"sd": "bid",
"f": "3.2",
"fc": "twd",
"fd": false,
"m": true,
"oi": 7423,
"ci": "client-oid-1",
"gi": 123
}],
"T": 1591786735192
}`)
assert.NoError(t, err)
assert.NotNil(t, fv)

evt, err := parseTradeSnapshotEvent(fv)
assert.NoError(t, err)
assert.NotNil(t, evt)
assert.Equal(t, "trade_snapshot", evt.Event)
assert.Equal(t, int64(1591786735192), evt.Timestamp)
assert.Equal(t, 1, len(evt.Trades))
assert.Equal(t, "bid", evt.Trades[0].Side)
assert.Equal(t, "ethtwd", evt.Trades[0].Market)
assert.Equal(t, int64(1521726960357), evt.Trades[0].Timestamp)
assert.Equal(t, "3.2", evt.Trades[0].Fee)
assert.Equal(t, "twd", evt.Trades[0].FeeCurrency)
}

0 comments on commit 5f2ead4

Please sign in to comment.