github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/order/order.go (about) 1 package order 2 3 import ( 4 "fmt" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/convert" 7 ) 8 9 type Order struct { 10 ID int64 11 GID int64 12 CID int64 13 Symbol string 14 MTSCreated int64 15 MTSUpdated int64 16 Amount float64 17 AmountOrig float64 18 Type string 19 TypePrev string 20 MTSTif int64 21 Flags int64 22 Status string 23 Price float64 24 PriceAvg float64 25 PriceTrailing float64 26 PriceAuxLimit float64 27 Notify bool 28 Hidden bool 29 PlacedID int64 30 Routing string 31 Meta map[string]interface{} 32 } 33 34 // Snapshot is a collection of Orders that would usually be sent on 35 // inital connection. 36 type Snapshot struct { 37 Snapshot []*Order 38 } 39 40 // Update is an Order that gets sent out after every change to an order. 41 type Update Order 42 43 // New gets sent out after an Order was created successfully. 44 type New Order 45 46 // Cancel gets sent out after an Order was cancelled successfully. 47 type Cancel Order 48 49 // FromRaw takes the raw list of values as returned from the websocket 50 // service and tries to convert it into an Order. 51 func FromRaw(raw []interface{}) (o *Order, err error) { 52 if len(raw) < 32 { 53 return o, fmt.Errorf("data slice too short for order: %#v", raw) 54 } 55 56 o = &Order{ 57 ID: convert.I64ValOrZero(raw[0]), 58 GID: convert.I64ValOrZero(raw[1]), 59 CID: convert.I64ValOrZero(raw[2]), 60 Symbol: convert.SValOrEmpty(raw[3]), 61 MTSCreated: convert.I64ValOrZero(raw[4]), 62 MTSUpdated: convert.I64ValOrZero(raw[5]), 63 Amount: convert.F64ValOrZero(raw[6]), 64 AmountOrig: convert.F64ValOrZero(raw[7]), 65 Type: convert.SValOrEmpty(raw[8]), 66 TypePrev: convert.SValOrEmpty(raw[9]), 67 MTSTif: convert.I64ValOrZero(raw[10]), 68 Flags: convert.I64ValOrZero(raw[12]), 69 Status: convert.SValOrEmpty(raw[13]), 70 Price: convert.F64ValOrZero(raw[16]), 71 PriceAvg: convert.F64ValOrZero(raw[17]), 72 PriceTrailing: convert.F64ValOrZero(raw[18]), 73 PriceAuxLimit: convert.F64ValOrZero(raw[19]), 74 Notify: convert.BValOrFalse(raw[23]), 75 Hidden: convert.BValOrFalse(raw[24]), 76 PlacedID: convert.I64ValOrZero(raw[25]), 77 Routing: convert.SValOrEmpty(raw[28]), 78 } 79 80 if meta, ok := raw[31].(map[string]interface{}); ok { 81 o.Meta = meta 82 } 83 84 return 85 } 86 87 // NewFromRaw reds "on" type message from data stream and 88 // maps it to order.New data structure 89 func NewFromRaw(raw []interface{}) (New, error) { 90 o, err := FromRaw(raw) 91 if err != nil { 92 return New{}, err 93 } 94 return New(*o), nil 95 } 96 97 // UpdateFromRaw reds "ou" type message from data stream and 98 // maps it to order.Update data structure 99 func UpdateFromRaw(raw []interface{}) (Update, error) { 100 o, err := FromRaw(raw) 101 if err != nil { 102 return Update{}, err 103 } 104 return Update(*o), nil 105 } 106 107 // CancelFromRaw reds "oc" type message from data stream and 108 // maps it to order.Cancel data structure 109 func CancelFromRaw(raw []interface{}) (Cancel, error) { 110 o, err := FromRaw(raw) 111 if err != nil { 112 return Cancel{}, err 113 } 114 return Cancel(*o), nil 115 } 116 117 // SnapshotFromRaw takes a raw list of values as returned from the websocket 118 // service and tries to convert it into an Snapshot. 119 func SnapshotFromRaw(raw []interface{}) (s *Snapshot, err error) { 120 if len(raw) == 0 { 121 return s, fmt.Errorf("data slice too short for order: %#v", raw) 122 } 123 124 os := make([]*Order, 0) 125 switch raw[0].(type) { 126 case []interface{}: 127 for _, v := range raw { 128 if l, ok := v.([]interface{}); ok { 129 o, err := FromRaw(l) 130 if err != nil { 131 return s, err 132 } 133 os = append(os, o) 134 } 135 } 136 default: 137 return s, fmt.Errorf("not an order snapshot") 138 } 139 s = &Snapshot{Snapshot: os} 140 141 return 142 }