ftx: unmarshal all fields at the same time

This commit is contained in:
ycdesu 2021-03-03 10:31:46 +08:00
parent 009dafd176
commit e34f68ab90
2 changed files with 15 additions and 30 deletions

View File

@ -2,7 +2,6 @@ package ftx
import ( import (
"encoding/json" "encoding/json"
"fmt"
"math" "math"
"strings" "strings"
"time" "time"
@ -54,9 +53,9 @@ type rawResponse struct {
// The following fields are optional. // The following fields are optional.
// Example 1: {"type": "error", "code": 404, "msg": "No such market: BTCUSDT"} // Example 1: {"type": "error", "code": 404, "msg": "No such market: BTCUSDT"}
Code int64 `json:"code"` Code int64 `json:"code"`
Message string `json:"msg"` Message string `json:"msg"`
Data map[string]json.RawMessage `json:"data"` Data json.RawMessage `json:"data"`
} }
func (r rawResponse) toSubscribedResp() subscribedResponse { func (r rawResponse) toSubscribedResp() subscribedResponse {
@ -70,28 +69,12 @@ func (r rawResponse) toSnapshotResp() (snapshotResponse, error) {
mandatoryFields: r.mandatoryFields, mandatoryFields: r.mandatoryFields,
} }
if err := json.Unmarshal(r.Data["action"], &o.Action); err != nil { if err := json.Unmarshal(r.Data, &o); err != nil {
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.action field: %w", err) return snapshotResponse{}, err
} }
var t float64 sec, dec := math.Modf(o.Time)
if err := json.Unmarshal(r.Data["time"], &t); err != nil { o.Timestamp = time.Unix(int64(sec), int64(dec*1e9))
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.time field: %w", err)
}
sec, dec := math.Modf(t)
o.Time = time.Unix(int64(sec), int64(dec*1e9))
if err := json.Unmarshal(r.Data["checksum"], &o.Checksum); err != nil {
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.checksum field: %w", err)
}
if err := json.Unmarshal(r.Data["bids"], &o.Bids); err != nil {
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.bids field: %w", err)
}
if err := json.Unmarshal(r.Data["asks"], &o.Asks); err != nil {
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.asks field: %w", err)
}
return o, nil return o, nil
} }
@ -104,17 +87,19 @@ type subscribedResponse struct {
type snapshotResponse struct { type snapshotResponse struct {
mandatoryFields mandatoryFields
Action string Action string `json:"action"`
Time time.Time Time float64 `json:"time"`
Checksum int64 Timestamp time.Time
Checksum int64 `json:"checksum"`
// Best 100 orders // Best 100 orders
Bids [][]float64 Bids [][]float64 `json:"bids"`
// Best 100 orders // Best 100 orders
Asks [][]float64 Asks [][]float64 `json:"asks"`
} }
func (r snapshotResponse) toGlobalOrderBook() types.OrderBook { func (r snapshotResponse) toGlobalOrderBook() types.OrderBook {

View File

@ -31,7 +31,7 @@ func Test_rawResponse_toSnapshotResp(t *testing.T) {
assert.Equal(t, partialRespType, r.Type) assert.Equal(t, partialRespType, r.Type)
assert.Equal(t, orderbook, r.Channel) assert.Equal(t, orderbook, r.Channel)
assert.Equal(t, "BTC/USDT", r.Market) assert.Equal(t, "BTC/USDT", r.Market)
assert.Equal(t, int64(1614520368), r.Time.Unix()) assert.Equal(t, int64(1614520368), r.Timestamp.Unix())
assert.Equal(t, int64(2150525410), r.Checksum) assert.Equal(t, int64(2150525410), r.Checksum)
assert.Len(t, r.Bids, 100) assert.Len(t, r.Bids, 100)
assert.Equal(t, []float64{44555.0, 3.3968}, r.Bids[0]) assert.Equal(t, []float64{44555.0, 3.3968}, r.Bids[0])