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

     1  package fundingloan
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
     7  )
     8  
     9  type Loan struct {
    10  	ID            int64
    11  	Symbol        string
    12  	Side          int
    13  	MTSCreated    int64
    14  	MTSUpdated    int64
    15  	Amount        float64
    16  	Flags         map[string]interface{}
    17  	Status        string
    18  	RateType      string
    19  	Rate          float64
    20  	Period        int64
    21  	MTSOpened     int64
    22  	MTSLastPayout int64
    23  	Notify        bool
    24  	Hidden        bool
    25  	Insure        bool
    26  	Renew         bool
    27  	RateReal      float64
    28  	NoClose       bool
    29  }
    30  
    31  type New Loan
    32  type Update Loan
    33  type Cancel Loan
    34  
    35  type Snapshot struct {
    36  	Snapshot []*Loan
    37  }
    38  
    39  func FromRaw(raw []interface{}) (l *Loan, err error) {
    40  	if len(raw) < 21 {
    41  		return l, fmt.Errorf("data slice too short (len=%d) for loan: %#v", len(raw), raw)
    42  	}
    43  
    44  	l = &Loan{
    45  		ID:            convert.I64ValOrZero(raw[0]),
    46  		Symbol:        convert.SValOrEmpty(raw[1]),
    47  		Side:          convert.ToInt(raw[2]),
    48  		MTSCreated:    convert.I64ValOrZero(raw[3]),
    49  		MTSUpdated:    convert.I64ValOrZero(raw[4]),
    50  		Amount:        convert.F64ValOrZero(raw[5]),
    51  		Status:        convert.SValOrEmpty(raw[7]),
    52  		RateType:      convert.SValOrEmpty(raw[8]),
    53  		Rate:          convert.F64ValOrZero(raw[11]),
    54  		Period:        convert.I64ValOrZero(raw[12]),
    55  		MTSOpened:     convert.I64ValOrZero(raw[13]),
    56  		MTSLastPayout: convert.I64ValOrZero(raw[14]),
    57  		Notify:        convert.BValOrFalse(raw[15]),
    58  		Hidden:        convert.BValOrFalse(raw[16]),
    59  		Insure:        convert.BValOrFalse(raw[17]), // DS: marked as _PLACEHOLDER in docs WS and REST
    60  		Renew:         convert.BValOrFalse(raw[18]),
    61  		RateReal:      convert.F64ValOrZero(raw[19]),
    62  		NoClose:       convert.BValOrFalse(raw[20]),
    63  	}
    64  
    65  	if flags, ok := raw[6].(map[string]interface{}); ok {
    66  		l.Flags = flags
    67  	}
    68  
    69  	return
    70  }
    71  
    72  func NewFromRaw(raw []interface{}) (New, error) {
    73  	r, err := FromRaw(raw)
    74  	if err != nil {
    75  		return New{}, err
    76  	}
    77  	return New(*r), nil
    78  }
    79  
    80  func UpdateFromRaw(raw []interface{}) (Update, error) {
    81  	r, err := FromRaw(raw)
    82  	if err != nil {
    83  		return Update{}, err
    84  	}
    85  	return Update(*r), nil
    86  }
    87  
    88  func CancelFromRaw(raw []interface{}) (Cancel, error) {
    89  	r, err := FromRaw(raw)
    90  	if err != nil {
    91  		return Cancel{}, err
    92  	}
    93  	return Cancel(*r), nil
    94  }
    95  
    96  func SnapshotFromRaw(raw []interface{}) (snap *Snapshot, err error) {
    97  	if len(raw) == 0 {
    98  		return snap, fmt.Errorf("data slice too short for funding loan: %#v", raw)
    99  	}
   100  
   101  	loans := make([]*Loan, 0)
   102  	switch raw[0].(type) {
   103  	case []interface{}:
   104  		for _, v := range raw {
   105  			if l, ok := v.([]interface{}); ok {
   106  				o, err := FromRaw(l)
   107  				if err != nil {
   108  					return snap, err
   109  				}
   110  				loans = append(loans, o)
   111  			}
   112  		}
   113  	default:
   114  		return snap, fmt.Errorf("not a funding loan snapshot")
   115  	}
   116  
   117  	snap = &Snapshot{Snapshot: loans}
   118  
   119  	return
   120  }