Merge pull request #1241 from c9s/c9s/max-add-fee-discounted-field

FEATURE: [max] add fee discounted field support
This commit is contained in:
c9s 2023-07-24 16:58:40 +08:00 committed by GitHub
commit c42ad19955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 73 deletions

View File

@ -288,36 +288,19 @@ func convertWebSocketTrade(t max.TradeUpdate) (*types.Trade, error) {
// trade time // trade time
mts := time.Unix(0, t.Timestamp*int64(time.Millisecond)) mts := time.Unix(0, t.Timestamp*int64(time.Millisecond))
price, err := fixedpoint.NewFromString(t.Price)
if err != nil {
return nil, err
}
quantity, err := fixedpoint.NewFromString(t.Volume)
if err != nil {
return nil, err
}
quoteQuantity := price.Mul(quantity)
fee, err := fixedpoint.NewFromString(t.Fee)
if err != nil {
return nil, err
}
return &types.Trade{ return &types.Trade{
ID: t.ID, ID: t.ID,
OrderID: t.OrderID, OrderID: t.OrderID,
Symbol: toGlobalSymbol(t.Market), Symbol: toGlobalSymbol(t.Market),
Exchange: types.ExchangeMax, Exchange: types.ExchangeMax,
Price: price, Price: t.Price,
Quantity: quantity, Quantity: t.Volume,
Side: side, Side: side,
IsBuyer: side == types.SideTypeBuy, IsBuyer: side == types.SideTypeBuy,
IsMaker: t.Maker, IsMaker: t.Maker,
Fee: fee, Fee: t.Fee,
FeeCurrency: toGlobalCurrency(t.FeeCurrency), FeeCurrency: toGlobalCurrency(t.FeeCurrency),
QuoteQuantity: quoteQuantity, QuoteQuantity: t.Price.Mul(t.Volume),
Time: types.Time(mts), Time: types.Time(mts),
}, nil }, nil
} }

View File

@ -94,74 +94,46 @@ func parserOrderSnapshotEvent(v *fastjson.Value) *OrderSnapshotEvent {
} }
type TradeUpdate struct { type TradeUpdate struct {
ID uint64 `json:"i"` ID uint64 `json:"i"`
Side string `json:"sd"` Side string `json:"sd"`
Price string `json:"p"` Price fixedpoint.Value `json:"p"`
Volume string `json:"v"` Volume fixedpoint.Value `json:"v"`
Market string `json:"M"` Market string `json:"M"`
Fee string `json:"f"` Fee fixedpoint.Value `json:"f"`
FeeCurrency string `json:"fc"` FeeCurrency string `json:"fc"`
Timestamp int64 `json:"T"` FeeDiscounted bool `json:"fd"`
UpdateTime int64 `json:"TU"`
Timestamp int64 `json:"T"`
UpdateTime int64 `json:"TU"`
OrderID uint64 `json:"oi"` OrderID uint64 `json:"oi"`
Maker bool `json:"m"` Maker bool `json:"m"`
} }
func parseTradeUpdate(v *fastjson.Value) TradeUpdate {
return TradeUpdate{
ID: v.GetUint64("i"),
Side: string(v.GetStringBytes("sd")),
Price: string(v.GetStringBytes("p")),
Volume: string(v.GetStringBytes("v")),
Market: string(v.GetStringBytes("M")),
Fee: string(v.GetStringBytes("f")),
FeeCurrency: string(v.GetStringBytes("fc")),
Timestamp: v.GetInt64("T"),
UpdateTime: v.GetInt64("TU"),
OrderID: v.GetUint64("oi"),
Maker: v.GetBool("m"),
}
}
type TradeUpdateEvent struct { type TradeUpdateEvent struct {
BaseEvent BaseEvent
Trades []TradeUpdate `json:"t"` 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 { type TradeSnapshotEvent struct {
BaseEvent BaseEvent
Trades []TradeUpdate `json:"t"` Trades []TradeUpdate `json:"t"`
} }
func parseTradeSnapshotEvent(v *fastjson.Value) *TradeSnapshotEvent { func parseTradeUpdateEvent(v *fastjson.Value) (*TradeUpdateEvent, error) {
jsonBytes := v.String()
var e TradeUpdateEvent
err := json.Unmarshal([]byte(jsonBytes), &e)
return &e, err
}
func parseTradeSnapshotEvent(v *fastjson.Value) (*TradeSnapshotEvent, error) {
jsonBytes := v.String()
var e TradeSnapshotEvent var e TradeSnapshotEvent
e.Event = string(v.GetStringBytes("e")) err := json.Unmarshal([]byte(jsonBytes), &e)
e.Timestamp = v.GetInt64("T") return &e, err
for _, tv := range v.GetArray("t") {
e.Trades = append(e.Trades, parseTradeUpdate(tv))
}
return &e
} }
type BalanceMessage struct { type BalanceMessage struct {
@ -252,10 +224,10 @@ func ParseUserEvent(v *fastjson.Value) (interface{}, error) {
return parseOrderUpdateEvent(v), nil return parseOrderUpdateEvent(v), nil
case "trade_snapshot", "mwallet_trade_snapshot": case "trade_snapshot", "mwallet_trade_snapshot":
return parseTradeSnapshotEvent(v), nil return parseTradeSnapshotEvent(v)
case "trade_update", "mwallet_trade_update": case "trade_update", "mwallet_trade_update":
return parseTradeUpdateEvent(v), nil return parseTradeUpdateEvent(v)
case "ad_ratio_snapshot", "ad_ratio_update": case "ad_ratio_snapshot", "ad_ratio_update":
return parseADRatioEvent(v) return parseADRatioEvent(v)

View File

@ -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.String())
assert.Equal(t, "twd", evt.Trades[0].FeeCurrency)
}