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

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