-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters