Skip to content

Commit

Permalink
fixup! Bitfinex: Fix WS trade processing
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Dec 27, 2024
1 parent 8b8da5c commit 4d3a7e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions exchanges/bitfinex/bitfinex_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bitfinex

import (
"encoding/json"
"errors"
"sync"
"time"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions exchanges/bitfinex/bitfinex_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 4d3a7e0

Please sign in to comment.