github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/status/derivatives.go (about) 1 package status 2 3 import ( 4 "fmt" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/convert" 7 ) 8 9 type DerivativesSnapshot struct { 10 Snapshot []*Derivative 11 } 12 13 type Derivative 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 ClampMIN float64 26 ClampMAX float64 27 } 28 29 func DerivFromRaw(symbol string, raw []interface{}) (*Derivative, error) { 30 if len(raw) < 22 { 31 return nil, fmt.Errorf("data slice too short for derivative status: %#v", raw) 32 } 33 34 return &Derivative{ 35 Symbol: symbol, 36 MTS: convert.I64ValOrZero(raw[0]), 37 Price: convert.F64ValOrZero(raw[2]), 38 SpotPrice: convert.F64ValOrZero(raw[3]), 39 InsuranceFundBalance: convert.F64ValOrZero(raw[5]), 40 FundingEventMTS: convert.I64ValOrZero(raw[7]), 41 FundingAccrued: convert.F64ValOrZero(raw[8]), 42 FundingStep: convert.F64ValOrZero(raw[9]), 43 CurrentFunding: convert.F64ValOrZero(raw[11]), 44 MarkPrice: convert.F64ValOrZero(raw[14]), 45 OpenInterest: convert.F64ValOrZero(raw[17]), 46 ClampMIN: convert.F64ValOrZero(raw[21]), 47 ClampMAX: convert.F64ValOrZero(raw[22]), 48 }, nil 49 } 50 51 func DerivSnapshotFromRaw(symbol string, raw [][]interface{}) (*DerivativesSnapshot, error) { 52 if len(raw) == 0 { 53 return nil, fmt.Errorf("empty data slice") 54 } 55 56 snapshot := make([]*Derivative, len(raw)) 57 for i, r := range raw { 58 d, err := DerivFromRaw(symbol, r) 59 if err != nil { 60 return nil, err 61 } 62 snapshot[i] = d 63 } 64 return &DerivativesSnapshot{Snapshot: snapshot}, nil 65 } 66 67 func DerivFromRestRaw(raw []interface{}) (t *Derivative, err error) { 68 if len(raw) < 2 { 69 return t, fmt.Errorf("data slice too short for derivatives: %#v", raw) 70 } 71 72 return DerivFromRaw(raw[0].(string), raw[1:]) 73 }