hitbtc: optimise fetching tradable pairs and tickers (#1184)

* hitbtc: fix and optimise tradable pairs and tickers fetching

* Update exchanges/hitbtc/hitbtc_test.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/hitbtc/hitbtc_test.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* thrasher: nits

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2023-05-09 10:09:24 +10:00
committed by GitHub
parent ef3337deee
commit 2d2d115ac9
2 changed files with 66 additions and 43 deletions

View File

@@ -56,6 +56,12 @@ func TestMain(m *testing.M) {
if err != nil {
log.Fatal("HitBTC setup error", err)
}
err = h.UpdateTradablePairs(context.Background(), false)
if err != nil {
log.Fatal("HitBTC setup error", err)
}
os.Exit(m.Run())
}
@@ -161,7 +167,7 @@ func TestGetFeeByTypeOfflineTradeFee(t *testing.T) {
}
func TestUpdateTicker(t *testing.T) {
pairs, err := currency.NewPairsFromStrings([]string{"BTC-USD", "XRP-USD"})
pairs, err := currency.NewPairsFromStrings([]string{"BTC-USD", "XRP-USDT"})
if err != nil {
t.Fatal(err)
}
@@ -169,25 +175,38 @@ func TestUpdateTicker(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = h.UpdateTicker(context.Background(),
currency.NewPair(currency.BTC, currency.USD),
asset.Spot)
_, err = h.UpdateTicker(context.Background(), pairs[0], asset.Spot)
if err != nil {
t.Error(err)
}
_, err = h.FetchTicker(context.Background(),
currency.NewPair(currency.XRP, currency.USD), asset.Spot)
_, err = h.FetchTicker(context.Background(), pairs[1], asset.Spot)
if err != nil {
t.Error(err)
}
}
func TestUpdateTickers(t *testing.T) {
err := h.UpdateTickers(context.Background(), asset.Spot)
avail, err := h.GetAvailablePairs(asset.Spot)
if err != nil {
t.Fatal(err)
}
err = h.CurrencyPairs.StorePairs(asset.Spot, avail, true)
if err != nil {
t.Fatal(err)
}
err = h.UpdateTickers(context.Background(), asset.Spot)
if err != nil {
t.Error(err)
}
for j := range avail {
_, err = h.FetchTicker(context.Background(), avail[j], asset.Spot)
if err != nil {
t.Error(err)
}
}
}
func TestGetAllTickers(t *testing.T) {
@@ -1050,3 +1069,10 @@ func TestGetHistoricTrades(t *testing.T) {
t.Error(err)
}
}
func TestFetchTradablePairs(t *testing.T) {
t.Parallel()
if _, err := h.FetchTradablePairs(context.Background(), asset.Spot); err != nil {
t.Fatal(err)
}
}

View File

@@ -291,8 +291,9 @@ func (h *HitBTC) FetchTradablePairs(ctx context.Context, _ asset.Item) (currency
pairs := make([]currency.Pair, len(symbols))
for x := range symbols {
quote := strings.Replace(symbols[x].ID, symbols[x].BaseCurrency, "", 1)
var pair currency.Pair
pair, err = currency.NewPairFromStrings(symbols[x].BaseCurrency, symbols[x].QuoteCurrency)
pair, err = currency.NewPairFromStrings(symbols[x].BaseCurrency, quote)
if err != nil {
return nil, err
}
@@ -317,45 +318,41 @@ func (h *HitBTC) UpdateTickers(ctx context.Context, a asset.Item) error {
if err != nil {
return err
}
pairs, err := h.GetEnabledPairs(a)
avail, err := h.GetAvailablePairs(a)
if err != nil {
return err
}
for i := range pairs {
for j := range tick {
pairFmt, err := h.FormatExchangeCurrency(pairs[i], a)
if err != nil {
return err
}
if tick[j].Symbol != pairFmt.String() {
found := false
if strings.Contains(tick[j].Symbol, "USDT") {
if pairFmt.String() == tick[j].Symbol[0:len(tick[j].Symbol)-1] {
found = true
}
}
if !found {
continue
}
}
enabled, err := h.GetEnabledPairs(a)
if err != nil {
return err
}
err = ticker.ProcessTicker(&ticker.Price{
Last: tick[j].Last,
High: tick[j].High,
Low: tick[j].Low,
Bid: tick[j].Bid,
Ask: tick[j].Ask,
Volume: tick[j].Volume,
QuoteVolume: tick[j].VolumeQuote,
Open: tick[j].Open,
Pair: pairs[i],
LastUpdated: tick[j].Timestamp,
ExchangeName: h.Name,
AssetType: a})
if err != nil {
return err
}
for x := range tick {
pair, err := avail.DeriveFrom(tick[x].Symbol)
if err != nil {
return err
}
if !enabled.Contains(pair, true) {
continue
}
err = ticker.ProcessTicker(&ticker.Price{
Last: tick[x].Last,
High: tick[x].High,
Low: tick[x].Low,
Bid: tick[x].Bid,
Ask: tick[x].Ask,
Volume: tick[x].Volume,
QuoteVolume: tick[x].VolumeQuote,
Open: tick[x].Open,
Pair: pair,
LastUpdated: tick[x].Timestamp,
ExchangeName: h.Name,
AssetType: a})
if err != nil {
return err
}
}
return nil