max: use fixedpoint.Value for field parsing

This commit is contained in:
c9s 2023-07-24 15:00:03 +08:00
parent 5f2ead4ffd
commit 9c20215f41
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 13 additions and 46 deletions

View File

@ -288,36 +288,19 @@ func convertWebSocketTrade(t max.TradeUpdate) (*types.Trade, error) {
// trade time
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{
ID: t.ID,
OrderID: t.OrderID,
Symbol: toGlobalSymbol(t.Market),
Exchange: types.ExchangeMax,
Price: price,
Quantity: quantity,
Price: t.Price,
Quantity: t.Volume,
Side: side,
IsBuyer: side == types.SideTypeBuy,
IsMaker: t.Maker,
Fee: fee,
Fee: t.Fee,
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
QuoteQuantity: quoteQuantity,
QuoteQuantity: t.Price.Mul(t.Volume),
Time: types.Time(mts),
}, nil
}

View File

@ -94,15 +94,15 @@ func parserOrderSnapshotEvent(v *fastjson.Value) *OrderSnapshotEvent {
}
type TradeUpdate struct {
ID uint64 `json:"i"`
Side string `json:"sd"`
Price string `json:"p"`
Volume string `json:"v"`
Market string `json:"M"`
ID uint64 `json:"i"`
Side string `json:"sd"`
Price fixedpoint.Value `json:"p"`
Volume fixedpoint.Value `json:"v"`
Market string `json:"M"`
Fee string `json:"f"`
FeeCurrency string `json:"fc"`
FeeDiscounted bool `json:"fd"`
Fee fixedpoint.Value `json:"f"`
FeeCurrency string `json:"fc"`
FeeDiscounted bool `json:"fd"`
Timestamp int64 `json:"T"`
UpdateTime int64 `json:"TU"`
@ -112,22 +112,6 @@ type TradeUpdate struct {
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 {
BaseEvent
Trades []TradeUpdate `json:"t"`

View File

@ -40,6 +40,6 @@ func Test_parseTradeSnapshotEvent(t *testing.T) {
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, "3.2", evt.Trades[0].Fee.String())
assert.Equal(t, "twd", evt.Trades[0].FeeCurrency)
}