github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/notification/notification.go (about) 1 package notification 2 3 import ( 4 "fmt" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/convert" 7 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingoffer" 8 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/order" 9 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/position" 10 ) 11 12 type Notification struct { 13 MTS int64 14 Type string 15 MessageID int64 16 NotifyInfo interface{} 17 Code int64 18 Status string 19 Text string 20 } 21 22 func FromRaw(raw []interface{}) (n *Notification, err error) { 23 if len(raw) < 8 { 24 return n, fmt.Errorf("data slice too short for notification: %#v", raw) 25 } 26 27 n = &Notification{ 28 MTS: convert.I64ValOrZero(raw[0]), 29 Type: convert.SValOrEmpty(raw[1]), 30 MessageID: convert.I64ValOrZero(raw[2]), 31 Code: convert.I64ValOrZero(raw[5]), 32 Status: convert.SValOrEmpty(raw[6]), 33 Text: convert.SValOrEmpty(raw[7]), 34 } 35 36 // raw[4] = notify info 37 if raw[4] == nil { 38 return 39 } 40 41 nraw := raw[4].([]interface{}) 42 if len(nraw) == 0 { 43 return 44 } 45 46 switch n.Type { 47 case "on-req": 48 // will be a set of orders if created via rest 49 // this is to accommodate OCO orders 50 if _, isSnapshot := nraw[0].([]interface{}); isSnapshot { 51 n.NotifyInfo, err = order.SnapshotFromRaw(nraw) 52 return 53 } 54 55 n.NotifyInfo, err = order.NewFromRaw(nraw) 56 return 57 case "ou-req", "ou": 58 n.NotifyInfo, err = order.UpdateFromRaw(nraw) 59 return 60 case "oc-req": 61 n.NotifyInfo, err = order.CancelFromRaw(nraw) 62 return 63 case "fon-req": 64 n.NotifyInfo, err = fundingoffer.NewFromRaw(nraw) 65 return 66 case "foc-req": 67 n.NotifyInfo, err = fundingoffer.CancelFromRaw(nraw) 68 return 69 case "pm-req", "pc": 70 n.NotifyInfo, err = position.CancelFromRaw(nraw) 71 return 72 default: 73 n.NotifyInfo = raw[4] 74 } 75 76 return 77 }