From fba3a4615fa6b6fd0852db1e0176aa787db433a6 Mon Sep 17 00:00:00 2001 From: iulianmoraru Date: Thu, 14 Oct 2021 20:53:47 +0300 Subject: [PATCH 1/2] Add Symbols Details functionality: Get a list of valid symbol IDs and the pair details --- v1/client.go | 38 ++++++++++++++++++++------------------ v1/symbols_details.go | 36 ++++++++++++++++++++++++++++++++++++ v1/symbols_details_test.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 v1/symbols_details.go create mode 100644 v1/symbols_details_test.go diff --git a/v1/client.go b/v1/client.go index 7ec122737..3dd582064 100644 --- a/v1/client.go +++ b/v1/client.go @@ -34,24 +34,25 @@ type Client struct { APISecret string // Services - Pairs *PairsService - Stats *StatsService - Ticker *TickerService - Account *AccountService - Balances *BalancesService - Offers *OffersService - Credits *CreditsService - Deposit *DepositService - Lendbook *LendbookService - MarginInfo *MarginInfoService - MarginFunding *MarginFundingService - OrderBook *OrderBookService - Orders *OrderService - Trades *TradesService - Positions *PositionsService - History *HistoryService - WebSocket *WebSocketService - Wallet *WalletService + Pairs *PairsService + Stats *StatsService + Ticker *TickerService + Account *AccountService + Balances *BalancesService + Offers *OffersService + Credits *CreditsService + Deposit *DepositService + Lendbook *LendbookService + MarginInfo *MarginInfoService + MarginFunding *MarginFundingService + OrderBook *OrderBookService + Orders *OrderService + Trades *TradesService + Positions *PositionsService + History *HistoryService + WebSocket *WebSocketService + Wallet *WalletService + SymbolsDetails *SymbolsDetailsService } // NewClient creates new Bitfinex.com API client. @@ -76,6 +77,7 @@ func NewClient() *Client { c.Trades = &TradesService{client: c} c.Positions = &PositionsService{client: c} c.Wallet = &WalletService{client: c} + c.SymbolsDetails = &SymbolsDetailsService{client: c} c.WebSocket = NewWebSocketService(c) c.WebSocketTLSSkipVerify = false diff --git a/v1/symbols_details.go b/v1/symbols_details.go new file mode 100644 index 000000000..13680eca1 --- /dev/null +++ b/v1/symbols_details.go @@ -0,0 +1,36 @@ +package bitfinex + +import ( + "net/url" +) + +type SymbolsDetailsService struct { + client *Client +} + +type SymbolDetail struct { + Pair string `json:"pair"` + PricePrecision int `json:"price_precision"` + InitialMargin string `json:"initial_margin"` + MinimumMargin string `json:"minimum_margin"` + MaximumOrderSize string `json:"maximum_order_size"` + MinimumOrderSize string `json:"minimum_order_size"` + Expiration string `json:"expiration"` +} + +func (s *SymbolsDetailsService) GetSymbolsDetails() ([]SymbolDetail, error) { + params := url.Values{} + req, err := s.client.newRequest("GET", "symbols_details", params) + if err != nil { + return nil, err + } + + var v []SymbolDetail + + _, err = s.client.do(req, &v) + if err != nil { + return nil, err + } + + return v, nil +} diff --git a/v1/symbols_details_test.go b/v1/symbols_details_test.go new file mode 100644 index 000000000..07b81e89b --- /dev/null +++ b/v1/symbols_details_test.go @@ -0,0 +1,36 @@ +package bitfinex + +import ( + "bytes" + "io/ioutil" + "net/http" + "testing" +) + +func TestSymbolsDetailsGet(t *testing.T) { + httpDo = func(req *http.Request) (*http.Response, error) { + msg := `{ + "pair":"btcusd", + "price_precision":5, + "initial_margin":"30.0", + "minimum_margin":"15.0", + "maximum_order_size":"2000.0", + "minimum_order_size":"0.01", + "expiration":"NA" + }` + resp := http.Response{ + Body: ioutil.NopCloser(bytes.NewBufferString(msg)), + StatusCode: 200, + } + return &resp, nil + } + + symbolsDetails, err := NewClient().SymbolsDetails.GetSymbolsDetails() + if err != nil { + t.Error(err) + } + if len(symbolsDetails) != 1 { + t.Error("Expected", 1) + t.Error("Actual ", len(symbolsDetails)) + } +} From 20c317139991e98aa065a2f58f944fcd01f6caf0 Mon Sep 17 00:00:00 2001 From: iulianmoraru Date: Thu, 14 Oct 2021 22:21:22 +0300 Subject: [PATCH 2/2] Add Symbols functionality: A list of symbol names --- v1/client.go | 2 ++ v1/symbols.go | 26 ++++++++++++++++++++++++++ v1/symbols_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 v1/symbols.go create mode 100644 v1/symbols_test.go diff --git a/v1/client.go b/v1/client.go index 3dd582064..5f45c2e9c 100644 --- a/v1/client.go +++ b/v1/client.go @@ -52,6 +52,7 @@ type Client struct { History *HistoryService WebSocket *WebSocketService Wallet *WalletService + Symbols *SymbolsService SymbolsDetails *SymbolsDetailsService } @@ -77,6 +78,7 @@ func NewClient() *Client { c.Trades = &TradesService{client: c} c.Positions = &PositionsService{client: c} c.Wallet = &WalletService{client: c} + c.Symbols = &SymbolsService{client: c} c.SymbolsDetails = &SymbolsDetailsService{client: c} c.WebSocket = NewWebSocketService(c) c.WebSocketTLSSkipVerify = false diff --git a/v1/symbols.go b/v1/symbols.go new file mode 100644 index 000000000..2ed26c892 --- /dev/null +++ b/v1/symbols.go @@ -0,0 +1,26 @@ +package bitfinex + +import ( + "net/url" +) + +type SymbolsService struct { + client *Client +} + +func (s *SymbolsService) GetSymbols() ([]string, error) { + params := url.Values{} + req, err := s.client.newRequest("GET", "symbols", params) + if err != nil { + return nil, err + } + + var v []string + + _, err = s.client.do(req, &v) + if err != nil { + return nil, err + } + + return v, nil +} diff --git a/v1/symbols_test.go b/v1/symbols_test.go new file mode 100644 index 000000000..4d4c9a264 --- /dev/null +++ b/v1/symbols_test.go @@ -0,0 +1,32 @@ +package bitfinex + +import ( + "bytes" + "io/ioutil" + "net/http" + "testing" +) + +func TestSymbolsGet(t *testing.T) { + httpDo = func(req *http.Request) (*http.Response, error) { + msg := `[ + "btcusd", + "ltcusd", + "ltcbtc", + ]` + resp := http.Response{ + Body: ioutil.NopCloser(bytes.NewBufferString(msg)), + StatusCode: 200, + } + return &resp, nil + } + + symbols, err := NewClient().Symbols.GetSymbols() + if err != nil { + t.Error(err) + } + if len(symbols) != 3 { + t.Error("Expected", 3) + t.Error("Actual ", len(symbols)) + } +}