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

     1  package trades
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
     7  )
     8  
     9  // FundingTrade data structure for mapping funding/margin
    10  // currency raw data with "f" prefix in SYMBOL from public feed
    11  type FundingTrade struct {
    12  	Symbol string
    13  	ID     int64
    14  	MTS    int64
    15  	Amount float64
    16  	Rate   float64
    17  	Period int
    18  }
    19  
    20  type FundingTradeExecutionUpdate FundingTrade
    21  type FundingTradeExecuted FundingTrade
    22  
    23  type FundingTradeSnapshot struct {
    24  	Snapshot []FundingTrade
    25  }
    26  
    27  // FTFromRaw maps raw data slice to instance of FundingTrade
    28  func FTFromRaw(pair string, raw []interface{}) (t FundingTrade, err error) {
    29  	if len(raw) >= 5 {
    30  		t = FundingTrade{
    31  			Symbol: pair,
    32  			ID:     convert.I64ValOrZero(raw[0]),
    33  			MTS:    convert.I64ValOrZero(raw[1]),
    34  			Amount: convert.F64ValOrZero(raw[2]),
    35  			Rate:   convert.F64ValOrZero(raw[3]),
    36  			Period: convert.ToInt(raw[4]),
    37  		}
    38  		return
    39  	}
    40  
    41  	err = fmt.Errorf("data slice too short: %#v", raw)
    42  	return
    43  }
    44  
    45  // FTEFromRaw maps raw data slice to instance of FundingTradeExecuted
    46  func FTEFromRaw(pair string, raw []interface{}) (FundingTradeExecuted, error) {
    47  	ft, err := FTFromRaw(pair, raw)
    48  	if err != nil {
    49  		return FundingTradeExecuted{}, err
    50  	}
    51  
    52  	return FundingTradeExecuted(ft), nil
    53  }
    54  
    55  // FTEUFromRaw maps raw data slice to instance of FundingTradeExecutionUpdate
    56  func FTEUFromRaw(pair string, raw []interface{}) (FundingTradeExecutionUpdate, error) {
    57  	ft, err := FTFromRaw(pair, raw)
    58  	if err != nil {
    59  		return FundingTradeExecutionUpdate{}, err
    60  	}
    61  
    62  	return FundingTradeExecutionUpdate(ft), nil
    63  }
    64  
    65  // FTSnapshotFromRaw maps raw data slice to funding trade data structures
    66  func FTSnapshotFromRaw(pair string, raw [][]interface{}) (FundingTradeSnapshot, error) {
    67  	if len(raw) == 0 {
    68  		return FundingTradeSnapshot{}, fmt.Errorf("funding trade snapshot data slice too short:%#v", raw)
    69  	}
    70  
    71  	snapshot := make([]FundingTrade, 0)
    72  	for _, v := range raw {
    73  		t, err := FTFromRaw(pair, v)
    74  		if err != nil {
    75  			return FundingTradeSnapshot{}, err
    76  		}
    77  		snapshot = append(snapshot, t)
    78  	}
    79  
    80  	return FundingTradeSnapshot{Snapshot: snapshot}, nil
    81  }