diff --git a/exchanges/bitfinex/bitfinex_types.go b/exchanges/bitfinex/bitfinex_types.go index 1ad4d314947..be6634e0dc2 100644 --- a/exchanges/bitfinex/bitfinex_types.go +++ b/exchanges/bitfinex/bitfinex_types.go @@ -1,6 +1,7 @@ package bitfinex import ( + "encoding/json" "errors" "sync" "time" @@ -497,6 +498,11 @@ type wsTrade struct { Period int64 // Funding offer period in days } +// UnmarshalJSON unmarshals json bytes into a wsTrade +func (t *wsTrade) UnmarshalJSON(data []byte) error { + return json.Unmarshal(data, &[]any{&t.ID, &t.Timestamp, &t.Amount, &t.Price, &t.Period}) +} + // Candle holds OHLC data type Candle struct { Timestamp time.Time diff --git a/exchanges/bitfinex/bitfinex_websocket.go b/exchanges/bitfinex/bitfinex_websocket.go index 15cac683f6f..f48514093f9 100644 --- a/exchanges/bitfinex/bitfinex_websocket.go +++ b/exchanges/bitfinex/bitfinex_websocket.go @@ -939,7 +939,7 @@ func (b *Bitfinex) handleWSPublicTradesSnapshot(respRaw []byte) (trades []*wsTra errs = common.AppendError(errs, fmt.Errorf("%w `tradesSnapshot[1][*]`: %w `%s`", errParsingWSField, jsonparser.UnknownValueTypeError, valueType)) } else { t := &wsTrade{} - if err := json.Unmarshal(v, &[]any{&t.ID, &t.Timestamp, &t.Amount, &t.Price, &t.Period}); err != nil { + if err := json.Unmarshal(v, t); err != nil { errs = common.AppendError(errs, fmt.Errorf("%w `tradesSnapshot[1][*]`: %w", errParsingWSField, err)) } else { trades = append(trades, t) @@ -962,7 +962,7 @@ func (b *Bitfinex) handleWSPublicTradeUpdate(respRaw []byte) (*wsTrade, error) { return nil, fmt.Errorf("%w `tradesUpdate[2]`: %w `%s`", errParsingWSField, jsonparser.UnknownValueTypeError, valueType) } t := &wsTrade{} - if err := json.Unmarshal(v, &[]any{&t.ID, &t.Timestamp, &t.Amount, &t.Price, &t.Period}); err != nil { + if err := json.Unmarshal(v, t); err != nil { return nil, fmt.Errorf("%w `tradeUpdate[2]`: %w", errParsingWSField, err) } return t, nil