github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/trades/trade.go (about) 1 package trades 2 3 import ( 4 "fmt" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/convert" 7 ) 8 9 // Trade data structure for mapping trading pair currency raw 10 // data with "t" prefix in SYMBOL from public feed 11 type Trade struct { 12 Pair string 13 ID int64 14 MTS int64 15 Amount float64 16 Price float64 17 } 18 19 type TradeExecutionUpdate Trade 20 type TradeExecuted Trade 21 22 type TradeSnapshot struct { 23 Snapshot []Trade 24 } 25 26 // TFromRaw maps raw data slice to instance of Trade 27 func TFromRaw(pair string, raw []interface{}) (t Trade, err error) { 28 if len(raw) >= 4 { 29 t = Trade{ 30 Pair: pair, 31 ID: convert.I64ValOrZero(raw[0]), 32 MTS: convert.I64ValOrZero(raw[1]), 33 Amount: convert.F64ValOrZero(raw[2]), 34 Price: convert.F64ValOrZero(raw[3]), 35 } 36 return 37 } 38 39 err = fmt.Errorf("data slice too short: %#v", raw) 40 return 41 } 42 43 // TEUFromRaw maps raw data slice to instance of TradeExecutionUpdate 44 func TEUFromRaw(pair string, raw []interface{}) (TradeExecutionUpdate, error) { 45 t, err := TFromRaw(pair, raw) 46 if err != nil { 47 return TradeExecutionUpdate{}, err 48 } 49 50 return TradeExecutionUpdate(t), nil 51 } 52 53 // TEFromRaw maps raw data slice to instance of TradeExecuted 54 func TEFromRaw(pair string, raw []interface{}) (TradeExecuted, error) { 55 t, err := TFromRaw(pair, raw) 56 if err != nil { 57 return TradeExecuted{}, err 58 } 59 60 return TradeExecuted(t), nil 61 } 62 63 // TSnapshotFromRaw maps raw data slice to trading data structures 64 func TSnapshotFromRaw(pair string, raw [][]interface{}) (TradeSnapshot, error) { 65 if len(raw) == 0 { 66 return TradeSnapshot{}, fmt.Errorf("trade snapshot data slice too short:%#v", raw) 67 } 68 69 snapshot := make([]Trade, 0) 70 for _, v := range raw { 71 t, err := TFromRaw(pair, v) 72 if err != nil { 73 return TradeSnapshot{}, err 74 } 75 snapshot = append(snapshot, t) 76 } 77 78 return TradeSnapshot{Snapshot: snapshot}, nil 79 }