From 942171db487dce8f5e9690e1769c9a809e7da43e Mon Sep 17 00:00:00 2001 From: Igor Mikushkin Date: Tue, 16 Oct 2018 19:22:27 +0400 Subject: [PATCH] implement v2 rest tickers --- v2/rest/client.go | 2 + v2/rest/ticker.go | 91 ++++++++++++++++++++++++++++++++++++++++++ v2/rest/ticker_test.go | 40 +++++++++++++++++++ v2/types.go | 1 + 4 files changed, 134 insertions(+) create mode 100644 v2/rest/ticker.go create mode 100644 v2/rest/ticker_test.go diff --git a/v2/rest/client.go b/v2/rest/client.go index 73b12ee97..c2c6759cc 100644 --- a/v2/rest/client.go +++ b/v2/rest/client.go @@ -38,6 +38,7 @@ type Client struct { Platform PlatformService Book BookService Wallet WalletService + Ticker TickerService Synchronous } @@ -95,6 +96,7 @@ func NewClientWithSynchronousURLNonce(sync Synchronous, url string, nonce utils. c.Platform = PlatformService{Synchronous: c} c.Positions = PositionService{Synchronous: c, requestFactory: c} c.Wallet = WalletService{Synchronous: c, requestFactory: c} + c.Ticker = TickerService{Synchronous: c} return c } diff --git a/v2/rest/ticker.go b/v2/rest/ticker.go new file mode 100644 index 000000000..71afc9ae0 --- /dev/null +++ b/v2/rest/ticker.go @@ -0,0 +1,91 @@ +package rest + +import ( + "errors" + "fmt" + "net/url" + "strings" + + bitfinex "github.com/bitfinexcom/bitfinex-api-go/v2" +) + +type TickerService struct { + Synchronous +} + +func (t *TickerService) All() (*bitfinex.TickerSnapshot, error) { + req := NewRequestWithMethod("tickers", "GET") + req.Params = make(url.Values) + req.Params.Add("symbols", "ALL") + raw, err := t.Request(req) + + if err != nil { + return nil, err + } + + tickers := make([]*bitfinex.Ticker, len(raw)) + for i, ifacearr := range raw { + arr, ok := ifacearr.([]interface{}) + if !ok { + return nil, fmt.Errorf("expecting array, got %T", ifacearr) + } + symbol, ok := arr[0].(string) + if !ok { + return nil, fmt.Errorf("expecting string, got %T", arr[0]) + } + if len(symbol) <= 1 || (symbol[0] != 't' && symbol[0] != 'f') { + return nil, errors.New("invalid symbol") + } + if (symbol[0] == 't' && len(arr) < 11) || (symbol[0] == 'f' && len(arr) < 14) { + return nil, errors.New("invalid length of ticker") + } + sub := make([]float64, len(arr)-1) + for j, iface := range arr[1:] { + if iface == nil { + sub[j] = 0 + continue + } + flt, ok := iface.(float64) + if !ok { + return nil, fmt.Errorf("expecting float64, got %T", iface) + } + sub[j] = flt + } + var entry *bitfinex.Ticker + switch symbol[0] { + case 't': + entry = &bitfinex.Ticker{ + Symbol: strings.ToLower(symbol[1:]), + Bid: sub[0], + BidSize: sub[1], + Ask: sub[2], + AskSize: sub[3], + DailyChange: sub[4], + DailyChangePerc: sub[5], + LastPrice: sub[6], + Volume: sub[7], + High: sub[8], + Low: sub[9], + } + case 'f': + entry = &bitfinex.Ticker{ + Symbol: strings.ToLower(symbol[1:]), + FRR: sub[0], + Bid: sub[1], + BidSize: sub[2], + BidPeriod: int64(sub[3]), + Ask: sub[4], + AskSize: sub[5], + AskPeriod: int64(sub[6]), + DailyChange: sub[7], + DailyChangePerc: sub[8], + LastPrice: sub[9], + Volume: sub[10], + High: sub[11], + Low: sub[12], + } + } + tickers[i] = entry + } + return &bitfinex.TickerSnapshot{Snapshot: tickers}, nil +} diff --git a/v2/rest/ticker_test.go b/v2/rest/ticker_test.go new file mode 100644 index 000000000..06146da6b --- /dev/null +++ b/v2/rest/ticker_test.go @@ -0,0 +1,40 @@ +package rest + +import ( + "bytes" + "io/ioutil" + "net/http" + "testing" +) + +func TestTickerAll(t *testing.T) { + httpDo := func(_ *http.Client, req *http.Request) (*http.Response, error) { + msg := `[["fSYMBOL1",3.00,0.01,0.02,4,0.03,0.04,5,0.05,0.06,0.07,0.08,0.09,0.10,null],["tSYMBOL2",0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.19,0.50]]` + resp := http.Response{ + Body: ioutil.NopCloser(bytes.NewBufferString(msg)), + StatusCode: 200, + } + return &resp, nil + } + + ticker, err := NewClientWithHttpDo(httpDo).Ticker.All() + + if err != nil { + t.Fatal(err) + } + if len(ticker.Snapshot) != 2 { + t.Fatalf("expected 2 ticker entries, but got %d", len(ticker.Snapshot)) + } + if ticker.Snapshot[1].Symbol != "symbol2" { + t.Fatalf("expected symbol2 symbol, but got %s", ticker.Snapshot[1].Symbol) + } + if ticker.Snapshot[1].Low != 0.5 { + t.Fatalf("expected low equal to 0.5, but got %f", ticker.Snapshot[1].Low) + } + if ticker.Snapshot[0].BidPeriod != 4 { + t.Fatalf("expected bit period equal to 4, but got %d", ticker.Snapshot[0].BidPeriod) + } + if ticker.Snapshot[0].AskPeriod != 5 { + t.Fatalf("expected ask period equal to 5, but got %d", ticker.Snapshot[0].AskPeriod) + } +} diff --git a/v2/types.go b/v2/types.go index 84a209ad4..aa033c5d4 100644 --- a/v2/types.go +++ b/v2/types.go @@ -1214,6 +1214,7 @@ func NewNotificationFromRaw(raw []interface{}) (o *Notification, err error) { type Ticker struct { Symbol string + FRR float64 Bid float64 BidPeriod int64 BidSize float64