github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v1/margin_funding.go (about)

     1  package bitfinex
     2  
     3  import "strconv"
     4  
     5  type MarginFundingService struct {
     6  	client *Client
     7  }
     8  
     9  type MarginOffer struct {
    10  	ID              int64
    11  	Currency        string
    12  	Rate            string
    13  	Period          int
    14  	Direction       string
    15  	Timestamp       string
    16  	IsLive          bool   `json:"is_live"`
    17  	IsCancelled     bool   `json:"is_cancelled"`
    18  	OriginalAmount  string `json:"original_amount"`
    19  	RemainingAmount string `json:"remaining_amount"`
    20  	ExecutedAmount  string `json:"executed_amount"`
    21  	OfferId         int
    22  }
    23  
    24  func (s *MarginFundingService) new(currency, direction string, amount, rate float64, period int) (MarginOffer, error) {
    25  	payload := map[string]interface{}{
    26  		"currency":  currency,
    27  		"amount":    strconv.FormatFloat(amount, 'f', -1, 32),
    28  		"rate":      strconv.FormatFloat(rate, 'f', -1, 32),
    29  		"period":    period,
    30  		"direction": direction,
    31  	}
    32  
    33  	req, err := s.client.newAuthenticatedRequest("POST", "offer/new", payload)
    34  
    35  	if err != nil {
    36  		return MarginOffer{}, err
    37  	}
    38  
    39  	var v MarginOffer
    40  	_, err = s.client.do(req, &v)
    41  
    42  	if err != nil {
    43  		return MarginOffer{}, err
    44  	}
    45  
    46  	return v, nil
    47  }
    48  
    49  func (s *MarginFundingService) NewLend(currency string, amount, rate float64, period int) (MarginOffer, error) {
    50  	return s.new(currency, "lend", amount, rate, period)
    51  }
    52  func (s *MarginFundingService) NewLoan(currency string, amount, rate float64, period int) (MarginOffer, error) {
    53  	return s.new(currency, "loan", amount, rate, period)
    54  }
    55  
    56  func (s *MarginFundingService) Cancel(offerId int64) (MarginOffer, error) {
    57  	payload := map[string]interface{}{"offer_id": offerId}
    58  
    59  	req, err := s.client.newAuthenticatedRequest("POST", "offer/cancel", payload)
    60  
    61  	if err != nil {
    62  		return MarginOffer{}, err
    63  	}
    64  
    65  	var v MarginOffer
    66  	_, err = s.client.do(req, &v)
    67  
    68  	if err != nil {
    69  		return MarginOffer{}, err
    70  	}
    71  	return v, nil
    72  }
    73  
    74  func (s *MarginFundingService) Status(offerId int64) (MarginOffer, error) {
    75  	payload := map[string]interface{}{"offer_id": offerId}
    76  
    77  	req, err := s.client.newAuthenticatedRequest("POST", "offer/status", payload)
    78  
    79  	if err != nil {
    80  		return MarginOffer{}, err
    81  	}
    82  
    83  	var v MarginOffer
    84  	_, err = s.client.do(req, &v)
    85  
    86  	if err != nil {
    87  		return MarginOffer{}, err
    88  	}
    89  	return v, nil
    90  }
    91  
    92  type ActiveOffer struct {
    93  	ID              int64
    94  	Currency        string
    95  	Rate            string
    96  	Period          int
    97  	Direction       string
    98  	Timestamp       string
    99  	IsLive          bool   `json:"is_live"`
   100  	IsCancelled     bool   `json:"is_cancelled"`
   101  	OriginalAmount  string `json:"original_amount"`
   102  	RemainingAmount string `json:"remaining_amount"`
   103  	ExecutedAmount  string `json:"executed_amount"`
   104  }
   105  
   106  func (s *MarginFundingService) Credits() ([]ActiveOffer, error) {
   107  
   108  	req, err := s.client.newAuthenticatedRequest("POST", "credits", nil)
   109  
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	var v []ActiveOffer
   115  	_, err = s.client.do(req, &v)
   116  
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	return v, nil
   121  }
   122  
   123  func (s *MarginFundingService) Offers() ([]ActiveOffer, error) {
   124  
   125  	req, err := s.client.newAuthenticatedRequest("POST", "offers", nil)
   126  
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	var v []ActiveOffer
   132  	_, err = s.client.do(req, &v)
   133  
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	return v, nil
   138  }