Address review feedbacks

This commit is contained in:
ycchen 2021-02-07 22:58:30 +01:00
parent 288f7257eb
commit 7a67083fbe
4 changed files with 35 additions and 41 deletions

View File

@ -5,7 +5,6 @@ import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/adshao/go-binance/v2"
@ -62,29 +61,30 @@ func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[stri
return nil, err
}
m := make(map[string]bool)
m := make(map[string]struct{})
exists := struct{}{}
for _, s := range symbol {
m[strings.ToUpper(s)] = true
m[s] = exists
}
for _, retrievedStats := range changeStats {
if _, ok := m[retrievedStats.Symbol]; len(symbol) != 0 && !ok {
for _, stats := range changeStats {
if _, ok := m[stats.Symbol]; len(symbol) != 0 && !ok {
continue
}
tick := types.Ticker{
Volume: util.MustParseFloat(retrievedStats.Volume),
Last: util.MustParseFloat(retrievedStats.LastPrice),
Open: util.MustParseFloat(retrievedStats.OpenPrice),
High: util.MustParseFloat(retrievedStats.HighPrice),
Low: util.MustParseFloat(retrievedStats.LowPrice),
Buy: util.MustParseFloat(retrievedStats.BidPrice),
Sell: util.MustParseFloat(retrievedStats.AskPrice),
Time: time.Unix(retrievedStats.CloseTime, 0),
Volume: util.MustParseFloat(stats.Volume),
Last: util.MustParseFloat(stats.LastPrice),
Open: util.MustParseFloat(stats.OpenPrice),
High: util.MustParseFloat(stats.HighPrice),
Low: util.MustParseFloat(stats.LowPrice),
Buy: util.MustParseFloat(stats.BidPrice),
Sell: util.MustParseFloat(stats.AskPrice),
Time: time.Unix(0, stats.CloseTime*int64(time.Millisecond)),
}
ret[retrievedStats.Symbol] = tick
ret[stats.Symbol] = tick
}
return ret, nil

View File

@ -3,15 +3,16 @@ package binance
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAllSymbols(t *testing.T) {
e := New("mock_key", "mock_secret")
got, err := e.QueryTickers(context.Background())
if err != nil {
t.Errorf("Binance Exchange: Fail to get ticker for all symbols: %s", err)
return
}
assert.NoError(t, err)
if len(got) <= 1 {
t.Errorf("Binance Exchange: Attempting to get all symbol tickers, but get 1 or less")
}
@ -22,9 +23,7 @@ func TestSomeSymbols(t *testing.T) {
e := New("mock_key", "mock_secret")
got, err := e.QueryTickers(context.Background(), "BTCUSDT", "ETHUSDT")
if err != nil {
t.Errorf("Binance Exchange: Fail to get ticker for some symbols: %s", err)
}
assert.NoError(t, err)
if len(got) != 2 {
t.Errorf("Binance Exchange: Attempting to get two symbols, but number of tickers do not match")
@ -35,9 +34,8 @@ func TestSomeSymbols(t *testing.T) {
func TestSingleSymbol(t *testing.T) {
e := New("mock_key", "mock_secret")
got, err := e.QueryTickers(context.Background(), "BTCUSDT")
if err != nil {
t.Errorf("Binance Exchange: Fail to get ticker for single symbol: %s", err)
}
assert.NoError(t, err)
if len(got) != 1 {
t.Errorf("Binance Exchange: Attempting to get one symbol, but number of tickers do not match")

View File

@ -5,7 +5,6 @@ import (
"fmt"
"math"
"os"
"strings"
"time"
"github.com/google/uuid"
@ -48,11 +47,11 @@ func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[stri
var ret = make(map[string]types.Ticker)
if len(symbol) == 1 {
ticker, err := e.client.PublicService.Ticker(strings.ToLower(symbol[0]))
ticker, err := e.client.PublicService.Ticker(toLocalSymbol(symbol[0]))
if err != nil {
return nil, err
}
ret[strings.ToUpper(symbol[0])] = types.Ticker{
ret[toGlobalSymbol(symbol[0])] = types.Ticker{
Time: ticker.Time,
Volume: util.MustParseFloat(ticker.Volume),
Last: util.MustParseFloat(ticker.Last),
@ -68,16 +67,17 @@ func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[stri
return nil, err
}
m := make(map[string]bool)
m := make(map[string]struct{})
exists := struct{}{}
for _, s := range symbol {
m[strings.ToUpper(s)] = true
m[toGlobalSymbol(s)] = exists
}
for k, v := range tickers {
if _, ok := m[strings.ToUpper(k)]; len(symbol) != 0 && !ok {
if _, ok := m[toGlobalSymbol(k)]; len(symbol) != 0 && !ok {
continue
}
ret[strings.ToUpper(k)] = types.Ticker{
ret[toGlobalSymbol(k)] = types.Ticker{
Time: v.Time,
Volume: util.MustParseFloat(v.Volume),
Last: util.MustParseFloat(v.Last),

View File

@ -3,14 +3,15 @@ package max
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAllSymbols(t *testing.T) {
e := New("mock_key", "mock_secret")
got, err := e.QueryTickers(context.Background())
if err != nil {
t.Errorf("Max Exchange: Fail to get ticker for all symbols: %s", err)
}
assert.NoError(t, err)
if len(got) <= 1 {
t.Errorf("Max Exchange: Attempting to get all symbol tickers, but get 1 or less")
}
@ -20,10 +21,7 @@ func TestAllSymbols(t *testing.T) {
func TestSomeSymbols(t *testing.T) {
e := New("mock_key", "mock_secret")
got, err := e.QueryTickers(context.Background(), "BTCUSDT", "ETHUSDT")
if err != nil {
t.Errorf("Max Exchange: Fail to get ticker for some symbols: %s", err)
}
assert.NoError(t, err)
if len(got) != 2 {
t.Errorf("Max Exchange: Attempting to get two symbols, but number of tickers do not match")
@ -34,9 +32,7 @@ func TestSomeSymbols(t *testing.T) {
func TestSingleSymbol(t *testing.T) {
e := New("mock_key", "mock_secret")
got, err := e.QueryTickers(context.Background(), "BTCUSDT")
if err != nil {
t.Errorf("Max Exchange: Fail to get ticker for single symbol: %s", err)
}
assert.NoError(t, err)
if len(got) != 1 {
t.Errorf("Max Exchange: Attempting to get one symbol, but number of tickers do not match")