github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/derivatives/derivatives.go (about) 1 package derivatives 2 3 import ( 4 "fmt" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/convert" 7 ) 8 9 type Snapshot struct { 10 Snapshot []*DerivativeStatus 11 } 12 13 type DerivativeStatus struct { 14 Symbol string 15 MTS int64 16 Price float64 17 SpotPrice float64 18 InsuranceFundBalance float64 19 FundingEventMTS int64 20 FundingAccrued float64 21 FundingStep float64 22 CurrentFunding float64 23 MarkPrice float64 24 OpenInterest float64 25 } 26 27 func FromWsRaw(symbol string, raw []interface{}) (*DerivativeStatus, error) { 28 if len(raw) < 18 { 29 return nil, fmt.Errorf("unexpected data slice length for derivative status: %#v", raw) 30 } 31 32 ds := &DerivativeStatus{ 33 Symbol: symbol, 34 MTS: convert.I64ValOrZero(raw[0]), 35 // placeholder 36 Price: convert.F64ValOrZero(raw[2]), 37 SpotPrice: convert.F64ValOrZero(raw[3]), 38 // placeholder 39 InsuranceFundBalance: convert.F64ValOrZero(raw[5]), 40 // placeholder 41 FundingEventMTS: convert.I64ValOrZero(raw[7]), 42 FundingAccrued: convert.F64ValOrZero(raw[8]), 43 FundingStep: convert.F64ValOrZero(raw[9]), 44 // placeholder 45 CurrentFunding: convert.F64ValOrZero(raw[11]), 46 // placeholder 47 // placeholder 48 MarkPrice: convert.F64ValOrZero(raw[14]), 49 // placeholder 50 // placeholder 51 OpenInterest: convert.F64ValOrZero(raw[17]), 52 } 53 54 return ds, nil 55 } 56 57 func FromRaw(raw []interface{}) (*DerivativeStatus, error) { 58 if len(raw) < 19 { 59 return nil, fmt.Errorf("unexpected data slice length for derivative status: %#v", raw) 60 } 61 62 ds := &DerivativeStatus{ 63 Symbol: convert.SValOrEmpty(raw[0]), 64 MTS: convert.I64ValOrZero(raw[1]), 65 // placeholder 66 Price: convert.F64ValOrZero(raw[3]), 67 SpotPrice: convert.F64ValOrZero(raw[4]), 68 // placeholder 69 InsuranceFundBalance: convert.F64ValOrZero(raw[6]), 70 // placeholder 71 FundingEventMTS: convert.I64ValOrZero(raw[8]), 72 FundingAccrued: convert.F64ValOrZero(raw[9]), 73 FundingStep: convert.F64ValOrZero(raw[10]), 74 // placeholder 75 CurrentFunding: convert.F64ValOrZero(raw[12]), 76 // placeholder 77 // placeholder 78 MarkPrice: convert.F64ValOrZero(raw[15]), 79 // placeholder 80 // placeholder 81 OpenInterest: convert.F64ValOrZero(raw[18]), 82 } 83 84 return ds, nil 85 } 86 87 func SnapshotFromRaw(raw [][]interface{}) (*Snapshot, error) { 88 snapshot := make([]*DerivativeStatus, len(raw)) 89 for i, rStatus := range raw { 90 pStatus, err := FromRaw(rStatus) 91 if err != nil { 92 return nil, err 93 } 94 snapshot[i] = pStatus 95 } 96 return &Snapshot{Snapshot: snapshot}, nil 97 }