github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/trade/trade.go (about) 1 package trade 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "github.com/bitfinexcom/bitfinex-api-go/pkg/convert" 9 ) 10 11 // Trade represents a trade on the public data feed. 12 type Trade struct { 13 Pair string 14 ID int64 15 MTS int64 16 Amount float64 17 Price float64 18 Rate float64 19 Period int 20 } 21 22 type Snapshot struct { 23 Snapshot []*Trade 24 } 25 26 func FromRaw(pair string, raw []interface{}) (t *Trade, err error) { 27 if strings.HasPrefix(pair, "t") && len(raw) >= 4 { 28 t = &Trade{ 29 Pair: pair, 30 ID: convert.I64ValOrZero(raw[0]), 31 MTS: convert.I64ValOrZero(raw[1]), 32 Amount: convert.F64ValOrZero(raw[2]), 33 Price: convert.F64ValOrZero(raw[3]), 34 } 35 return 36 } 37 38 if strings.HasPrefix(pair, "f") && len(raw) >= 5 { 39 t = &Trade{ 40 Pair: pair, 41 ID: convert.I64ValOrZero(raw[0]), 42 MTS: convert.I64ValOrZero(raw[1]), 43 Amount: convert.F64ValOrZero(raw[2]), 44 Rate: convert.F64ValOrZero(raw[3]), 45 Period: convert.ToInt(raw[4]), 46 } 47 return 48 } 49 50 err = fmt.Errorf("data slice too short for %s pair: %#v", pair, raw) 51 return 52 } 53 54 func SnapshotFromRaw(pair string, raw [][]interface{}) (*Snapshot, error) { 55 if len(raw) == 0 { 56 return nil, fmt.Errorf("data slice is too short for trade snapshot: %#v", raw) 57 } 58 59 snapshot := make([]*Trade, 0) 60 for _, v := range raw { 61 t, err := FromRaw(pair, v) 62 if err != nil { 63 return nil, err 64 } 65 snapshot = append(snapshot, t) 66 } 67 68 return &Snapshot{Snapshot: snapshot}, nil 69 } 70 71 // FromWSRaw - based on condition will return snapshot of trades or single trade 72 func FromWSRaw(pair string, data []interface{}) (interface{}, error) { 73 if len(data) == 0 { 74 return nil, errors.New("empty data slice") 75 } 76 77 _, isSnapshot := data[0].([]interface{}) 78 if isSnapshot { 79 return SnapshotFromRaw(pair, convert.ToInterfaceArray(data)) 80 } 81 82 return FromRaw(pair, data) 83 }