github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/wallet/wallet.go (about)

     1  package wallet
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
     7  )
     8  
     9  type Wallet struct {
    10  	Type              string
    11  	Currency          string
    12  	Balance           float64
    13  	UnsettledInterest float64
    14  	BalanceAvailable  float64
    15  	LastChange        string
    16  	TradeDetails      map[string]interface{}
    17  }
    18  
    19  type Update Wallet
    20  
    21  type Snapshot struct {
    22  	Snapshot []*Wallet
    23  }
    24  
    25  func FromRaw(raw []interface{}) (w *Wallet, err error) {
    26  	if len(raw) < 7 {
    27  		err = fmt.Errorf("data slice too short for wallet: %#v", raw)
    28  		return
    29  	}
    30  
    31  	w = &Wallet{
    32  		Type:              convert.SValOrEmpty(raw[0]),
    33  		Currency:          convert.SValOrEmpty(raw[1]),
    34  		Balance:           convert.F64ValOrZero(raw[2]),
    35  		UnsettledInterest: convert.F64ValOrZero(raw[3]),
    36  		BalanceAvailable:  convert.F64ValOrZero(raw[4]),
    37  		LastChange:        convert.SValOrEmpty(raw[5]),
    38  	}
    39  
    40  	if meta, ok := raw[6].(map[string]interface{}); ok {
    41  		w.TradeDetails = meta
    42  	}
    43  
    44  	return
    45  }
    46  
    47  // UpdateFromRaw reds "wu" type message from authenticated data
    48  // sream and maps it to wallet.Update data structure
    49  func UpdateFromRaw(raw []interface{}) (Update, error) {
    50  	w, err := FromRaw(raw)
    51  	if err != nil {
    52  		return Update{}, err
    53  	}
    54  
    55  	return Update(*w), nil
    56  }
    57  
    58  func SnapshotFromRaw(raw []interface{}) (s *Snapshot, err error) {
    59  	if len(raw) == 0 {
    60  		return s, fmt.Errorf("data slice too short for wallet: %#v", raw)
    61  	}
    62  
    63  	ws := make([]*Wallet, 0)
    64  	switch raw[0].(type) {
    65  	case []interface{}:
    66  		for _, v := range raw {
    67  			if l, ok := v.([]interface{}); ok {
    68  				w, err := FromRaw(l)
    69  				if err != nil {
    70  					return s, err
    71  				}
    72  				ws = append(ws, w)
    73  			}
    74  		}
    75  	default:
    76  		return s, fmt.Errorf("not an wallet snapshot")
    77  	}
    78  	s = &Snapshot{Snapshot: ws}
    79  
    80  	return
    81  }