github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/ledger/ledger.go (about) 1 package ledger 2 3 import ( 4 "fmt" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/convert" 7 ) 8 9 type Ledger struct { 10 ID int64 11 Currency string 12 // placeholder 13 MTS int64 14 // placeholder 15 Amount float64 16 Balance float64 17 // placeholder 18 Description string 19 } 20 21 type Snapshot struct { 22 Snapshot []*Ledger 23 } 24 25 type transformerFn func(raw []interface{}) (w *Ledger, err error) 26 27 // FromRaw takes the raw list of values as returned from the websocket 28 // service and tries to convert it into an Ledger. 29 func FromRaw(raw []interface{}) (o *Ledger, err error) { 30 if len(raw) < 9 { 31 return o, fmt.Errorf("data slice too short for ledger: %#v", raw) 32 } 33 34 o = &Ledger{ 35 ID: convert.I64ValOrZero(raw[0]), 36 Currency: convert.SValOrEmpty(raw[1]), 37 MTS: convert.I64ValOrZero(raw[3]), 38 Amount: convert.F64ValOrZero(raw[5]), 39 Balance: convert.F64ValOrZero(raw[6]), 40 Description: convert.SValOrEmpty(raw[8]), 41 } 42 43 return 44 } 45 46 // SnapshotFromRaw takes a raw list of values as returned from the websocket 47 // service and tries to convert it into an Snapshot. 48 func SnapshotFromRaw(raw []interface{}, t transformerFn) (s *Snapshot, err error) { 49 if len(raw) == 0 { 50 return s, fmt.Errorf("data slice too short for ledgers: %#v", raw) 51 } 52 53 lss := make([]*Ledger, 0) 54 switch raw[0].(type) { 55 case []interface{}: 56 for _, v := range raw { 57 if l, ok := v.([]interface{}); ok { 58 o, err := t(l) 59 if err != nil { 60 return s, err 61 } 62 lss = append(lss, o) 63 } 64 } 65 default: 66 return s, fmt.Errorf("not an ledger snapshot") 67 } 68 s = &Snapshot{Snapshot: lss} 69 return 70 }