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

     1  package bitfinex
     2  
     3  import "strconv"
     4  
     5  type OffersService struct {
     6  	client *Client
     7  }
     8  
     9  const (
    10  	LEND = "lend"
    11  	LOAN = "loan"
    12  )
    13  
    14  type Offer struct {
    15  	Id              int64
    16  	Currency        string
    17  	Rate            string
    18  	Period          int64
    19  	Direction       string
    20  	Timestamp       string
    21  	IsLive          bool   `json:"is_live"`
    22  	IsCancelled     bool   `json:"is_cancelled"`
    23  	OriginalAmount  string `json:"original_amount:string"`
    24  	RemainingAmount string `json:"remaining_amount:string"`
    25  	ExecutedAmount  string `json:"executed_amount:string"`
    26  	OfferId         int64  `json:"offer_id"`
    27  }
    28  
    29  // Create new offer for LEND or LOAN a currency, use LEND or LOAN constants as direction
    30  func (s *OffersService) New(currency string, amount, rate float64, period int64, direction string) (Offer, error) {
    31  
    32  	payload := map[string]interface{}{
    33  		"currency":  currency,
    34  		"amount":    strconv.FormatFloat(amount, 'f', -1, 32),
    35  		"rate":      strconv.FormatFloat(rate, 'f', -1, 32),
    36  		"period":    strconv.FormatInt(period, 10),
    37  		"direction": direction,
    38  	}
    39  
    40  	req, err := s.client.newAuthenticatedRequest("POST", "offers/new", payload)
    41  
    42  	if err != nil {
    43  		return Offer{}, err
    44  	}
    45  
    46  	var offer = &Offer{}
    47  	_, err = s.client.do(req, offer)
    48  
    49  	if err != nil {
    50  		return Offer{}, err
    51  	}
    52  
    53  	return *offer, nil
    54  
    55  }
    56  
    57  func (s *OffersService) Cancel(offerId int64) (Offer, error) {
    58  
    59  	payload := map[string]interface{}{
    60  		"offer_id": strconv.FormatInt(offerId, 10),
    61  	}
    62  
    63  	req, err := s.client.newAuthenticatedRequest("POST", "offers/cancel", payload)
    64  
    65  	if err != nil {
    66  		return Offer{}, err
    67  	}
    68  
    69  	var offer = &Offer{}
    70  
    71  	_, err = s.client.do(req, offer)
    72  
    73  	if err != nil {
    74  		return Offer{}, err
    75  	}
    76  
    77  	return *offer, nil
    78  }
    79  
    80  func (s *OffersService) Status(offerId int64) (Offer, error) {
    81  
    82  	payload := map[string]interface{}{
    83  		"offer_id": strconv.FormatInt(offerId, 10),
    84  	}
    85  
    86  	req, err := s.client.newAuthenticatedRequest("POST", "offers/status", payload)
    87  
    88  	if err != nil {
    89  		return Offer{}, err
    90  	}
    91  
    92  	var offer = &Offer{}
    93  
    94  	_, err = s.client.do(req, offer)
    95  
    96  	if err != nil {
    97  		return Offer{}, err
    98  	}
    99  
   100  	return *offer, nil
   101  }