fix: replace json.Number with MillisecondTimestamp in types

This commit is contained in:
zenix 2023-09-27 15:52:02 +09:00
parent 2e4336a604
commit 08dad1c497
4 changed files with 9 additions and 23 deletions

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/adshao/go-binance/v2/futures"
@ -18,8 +17,8 @@ import (
)
type EventBase struct {
Event string `json:"e"` // event name
Time json.Number `json:"E"` // event time
Event string `json:"e"` // event name
Time types.MillisecondTimestamp `json:"E"` // event time
}
/*
@ -462,11 +461,7 @@ func (e *DepthEvent) String() (o string) {
func (e *DepthEvent) OrderBook() (book types.SliceOrderBook, err error) {
book.Symbol = e.Symbol
t, err := e.EventBase.Time.Int64()
if err != nil {
return book, err
}
book.Time = types.NewMillisecondTimestampFromInt(t).Time()
book.Time = e.EventBase.Time.Time()
// already in descending order
book.Bids = e.Bids
@ -505,7 +500,7 @@ func parseDepthEvent(val *fastjson.Value) (*DepthEvent, error) {
var depth = &DepthEvent{
EventBase: EventBase{
Event: string(val.GetStringBytes("e")),
Time: json.Number(strconv.FormatInt(val.GetInt64("E"), 10)),
Time: types.NewMillisecondTimestampFromInt(val.GetInt64("E")),
},
Symbol: string(val.GetStringBytes("s")),
FirstUpdateID: val.GetInt64("U"),

View File

@ -397,7 +397,7 @@ func TestParseOrderFuturesUpdate(t *testing.T) {
assert.Equal(t, "SELL", orderTradeEvent.OrderTrade.Side)
assert.Equal(t, "x-NSUYEBKMe60cf610-f5c7-49a4-9c1", orderTradeEvent.OrderTrade.ClientOrderID)
assert.Equal(t, "MARKET", orderTradeEvent.OrderTrade.OrderType)
assert.Equal(t, int64(1639933384763), orderTradeEvent.Time)
assert.Equal(t, types.NewMillisecondTimestampFromInt(1639933384763), orderTradeEvent.Time)
assert.Equal(t, types.MillisecondTimestamp(time.UnixMilli(1639933384755)), orderTradeEvent.OrderTrade.OrderTradeTime)
assert.Equal(t, fixedpoint.MustNewFromString("0.001"), orderTradeEvent.OrderTrade.OriginalQuantity)
assert.Equal(t, fixedpoint.MustNewFromString("0.001"), orderTradeEvent.OrderTrade.OrderLastFilledQuantity)

View File

@ -86,13 +86,9 @@ func NewStream(ex *Exchange, client *binance.Client, futuresClient *futures.Clie
stream.OnDepthEvent(func(e *DepthEvent) {
f, ok := stream.depthBuffers[e.Symbol]
if ok {
t, err := e.EventBase.Time.Int64()
if err != nil {
log.WithError(err).Errorf("Time parsing failed: %v", e.EventBase.Time)
}
err = f.AddUpdate(types.SliceOrderBook{
err := f.AddUpdate(types.SliceOrderBook{
Symbol: e.Symbol,
Time: types.NewMillisecondTimestampFromInt(t).Time(),
Time: e.EventBase.Time.Time(),
Bids: e.Bids,
Asks: e.Asks,
}, e.FirstUpdateID, e.FinalUpdateID)

View File

@ -527,19 +527,14 @@ func (s *Strategy) handleAccountUpdate(ctx context.Context, e *binance.AccountUp
if b.Asset != s.ProfitStats.FundingFeeCurrency {
continue
}
t, err := e.EventBase.Time.Int64()
if err != nil {
log.WithError(err).Error("unable to parse event timestamp")
continue
}
txnTime := time.UnixMilli(t)
txnTime := e.EventBase.Time.Time()
fee := FundingFee{
Asset: b.Asset,
Amount: b.BalanceChange,
Txn: e.Transaction,
Time: txnTime,
}
err = s.ProfitStats.AddFundingFee(fee)
err := s.ProfitStats.AddFundingFee(fee)
if err != nil {
log.WithError(err).Error("unable to add funding fee to profitStats")
continue