github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/trades.go (about)

     1  package rest
     2  
     3  import (
     4  	"net/url"
     5  	"path"
     6  	"strconv"
     7  
     8  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
     9  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/common"
    10  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/trade"
    11  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/tradeexecutionupdate"
    12  )
    13  
    14  // TradeService manages the Trade endpoint.
    15  type TradeService struct {
    16  	requestFactory
    17  	Synchronous
    18  }
    19  
    20  // All returns all orders for the authenticated account.
    21  // left this in her
    22  func (s *TradeService) allAccountWithSymbol(symbol string) (*tradeexecutionupdate.Snapshot, error) {
    23  	req, err := s.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("trades", symbol, "hist"))
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	raw, err := s.Request(req)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	return parseRawPrivateToSnapshot(raw)
    32  }
    33  
    34  func (s *TradeService) allAccount() (*tradeexecutionupdate.Snapshot, error) {
    35  	req, err := s.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("trades", "hist"))
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	raw, err := s.Request(req)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return parseRawPrivateToSnapshot(raw)
    44  }
    45  
    46  // Retrieves all matched trades for the account
    47  // see https://docs.bitfinex.com/reference#rest-auth-trades-hist for more info
    48  func (s *TradeService) AccountAll() (*tradeexecutionupdate.Snapshot, error) {
    49  	return s.allAccount()
    50  }
    51  
    52  // Retrieves all matched trades with the given symbol for the account
    53  // see https://docs.bitfinex.com/reference#rest-auth-trades-hist for more info
    54  func (s *TradeService) AccountAllWithSymbol(symbol string) (*tradeexecutionupdate.Snapshot, error) {
    55  	return s.allAccountWithSymbol(symbol)
    56  }
    57  
    58  // Queries all matched trades with group of optional parameters
    59  // see https://docs.bitfinex.com/reference#rest-auth-trades-hist for more info
    60  func (s *TradeService) AccountHistoryWithQuery(
    61  	symbol string,
    62  	start common.Mts,
    63  	end common.Mts,
    64  	limit common.QueryLimit,
    65  	sort common.SortOrder,
    66  ) (*tradeexecutionupdate.Snapshot, error) {
    67  	req, err := s.requestFactory.NewAuthenticatedRequest(common.PermissionRead, path.Join("trades", symbol, "hist"))
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	req.Params = make(url.Values)
    72  	req.Params.Add("end", strconv.FormatInt(int64(end), 10))
    73  	req.Params.Add("start", strconv.FormatInt(int64(start), 10))
    74  	req.Params.Add("limit", strconv.FormatInt(int64(limit), 10))
    75  	req.Params.Add("sort", strconv.FormatInt(int64(sort), 10))
    76  	raw, err := s.Request(req)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	return parseRawPrivateToSnapshot(raw)
    81  }
    82  
    83  // Queries all public trades with a group of optional paramters
    84  // see https://docs.bitfinex.com/reference#rest-public-trades for more info
    85  func (s *TradeService) PublicHistoryWithQuery(
    86  	symbol string,
    87  	start common.Mts,
    88  	end common.Mts,
    89  	limit common.QueryLimit,
    90  	sort common.SortOrder,
    91  ) (*trade.Snapshot, error) {
    92  	req := NewRequestWithMethod(path.Join("trades", symbol, "hist"), "GET")
    93  	req.Params = make(url.Values)
    94  	req.Params.Add("end", strconv.FormatInt(int64(end), 10))
    95  	req.Params.Add("start", strconv.FormatInt(int64(start), 10))
    96  	req.Params.Add("limit", strconv.FormatInt(int64(limit), 10))
    97  	req.Params.Add("sort", strconv.FormatInt(int64(sort), 10))
    98  	raw, err := s.Request(req)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	if len(raw) <= 0 {
   104  		return &trade.Snapshot{Snapshot: make([]*trade.Trade, 0)}, nil
   105  	}
   106  
   107  	return trade.SnapshotFromRaw(symbol, convert.ToInterfaceArray(raw))
   108  }
   109  
   110  func parseRawPrivateToSnapshot(raw []interface{}) (*tradeexecutionupdate.Snapshot, error) {
   111  	if len(raw) <= 0 {
   112  		return &tradeexecutionupdate.Snapshot{Snapshot: make([]*tradeexecutionupdate.TradeExecutionUpdate, 0)}, nil
   113  	}
   114  	tradeExecutions, err := tradeexecutionupdate.SnapshotFromRaw(raw)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	return tradeExecutions, nil
   119  }