Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: GetTickerPrice() and GetCandlePrices() in providers still returns available ticker or candle data if not all currency pairs error #1767

Merged
merged 5 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions price-feeder/oracle/provider/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,31 +179,51 @@ func (p *BinanceProvider) SubscribeCurrencyPairs(cps ...types.CurrencyPair) erro
func (p *BinanceProvider) GetTickerPrices(pairs ...types.CurrencyPair) (map[string]types.TickerPrice, error) {
tickerPrices := make(map[string]types.TickerPrice, len(pairs))

tickerErrs := 0
for _, cp := range pairs {
key := cp.String()
price, err := p.getTickerPrice(key)
if err != nil {
return nil, err
p.logger.Warn().Err(err)
tickerErrs++
continue
}
tickerPrices[key] = price
}

if tickerErrs == len(pairs) {
return nil, fmt.Errorf(
types.ErrNoTickers.Error(),
p.endpoints.Name,
pairs,
)
}
return tickerPrices, nil
}

// GetCandlePrices returns the candlePrices based on the provided pairs.
func (p *BinanceProvider) GetCandlePrices(pairs ...types.CurrencyPair) (map[string][]types.CandlePrice, error) {
candlePrices := make(map[string][]types.CandlePrice, len(pairs))

candleErrs := 0
for _, cp := range pairs {
key := cp.String()
prices, err := p.getCandlePrices(key)
if err != nil {
return nil, err
p.logger.Warn().Err(err)
candleErrs++
continue
}
candlePrices[key] = prices
}

if candleErrs == len(pairs) {
return nil, fmt.Errorf(
types.ErrNoCandles.Error(),
p.endpoints.Name,
pairs,
)
}
return candlePrices, nil
}

Expand All @@ -213,7 +233,11 @@ func (p *BinanceProvider) getTickerPrice(key string) (types.TickerPrice, error)

ticker, ok := p.tickers[key]
if !ok {
return types.TickerPrice{}, fmt.Errorf("binance failed to get ticker price for %s", key)
return types.TickerPrice{}, fmt.Errorf(
types.ErrTickerNotFound.Error(),
p.endpoints.Name,
key,
)
}

return ticker.toTickerPrice()
Expand All @@ -225,7 +249,11 @@ func (p *BinanceProvider) getCandlePrices(key string) ([]types.CandlePrice, erro

candles, ok := p.candles[key]
if !ok {
return []types.CandlePrice{}, fmt.Errorf("binance failed to get candle prices for %s", key)
return []types.CandlePrice{}, fmt.Errorf(
types.ErrCandleNotFound.Error(),
p.endpoints.Name,
key,
)
}

candleList := []types.CandlePrice{}
Expand Down
4 changes: 2 additions & 2 deletions price-feeder/oracle/provider/binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestBinanceProvider_GetTickerPrices(t *testing.T) {
context.TODO(),
zerolog.Nop(),
Endpoint{},
false,
true,
types.CurrencyPair{Base: "ATOM", Quote: "USDT"},
)
require.NoError(t, err)
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestBinanceProvider_GetTickerPrices(t *testing.T) {

t.Run("invalid_request_invalid_ticker", func(t *testing.T) {
prices, err := p.GetTickerPrices(types.CurrencyPair{Base: "FOO", Quote: "BAR"})
require.EqualError(t, err, "binance failed to get ticker price for FOOBAR")
require.EqualError(t, err, "binanceus has no ticker data for requested pairs: [FOOBAR]")
require.Nil(t, prices)
})
}
Expand Down
44 changes: 36 additions & 8 deletions price-feeder/oracle/provider/bitget.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,33 +182,53 @@ func (p *BitgetProvider) SubscribeCurrencyPairs(cps ...types.CurrencyPair) error
return nil
}

// GetTickerPrices returns the tickerPrices based on the saved map.
// GetTickerPrices returns the tickerPrices based on the provided pairs.
func (p *BitgetProvider) GetTickerPrices(pairs ...types.CurrencyPair) (map[string]types.TickerPrice, error) {
tickerPrices := make(map[string]types.TickerPrice, len(pairs))

tickerErrs := 0
for _, cp := range pairs {
price, err := p.getTickerPrice(cp)
if err != nil {
return nil, err
p.logger.Warn().Err(err)
tickerErrs++
continue
}
tickerPrices[cp.String()] = price
}

if tickerErrs == len(pairs) {
return nil, fmt.Errorf(
types.ErrNoTickers.Error(),
p.endpoints.Name,
pairs,
)
}
return tickerPrices, nil
}

// GetTickerPrices returns the tickerPrices based on the saved map.
// GetCandlePrices returns the candlePrices based on the provided pairs.
func (p *BitgetProvider) GetCandlePrices(pairs ...types.CurrencyPair) (map[string][]types.CandlePrice, error) {
candlePrices := make(map[string][]types.CandlePrice, len(pairs))

candleErrs := 0
for _, cp := range pairs {
price, err := p.getCandlePrices(cp)
prices, err := p.getCandlePrices(cp)
if err != nil {
return nil, err
p.logger.Warn().Err(err)
candleErrs++
continue
}
candlePrices[cp.String()] = price
candlePrices[cp.String()] = prices
}

if candleErrs == len(pairs) {
return nil, fmt.Errorf(
types.ErrNoCandles.Error(),
p.endpoints.Name,
pairs,
)
}
return candlePrices, nil
}

Expand Down Expand Up @@ -320,7 +340,11 @@ func (p *BitgetProvider) getTickerPrice(cp types.CurrencyPair) (types.TickerPric

ticker, ok := p.tickers[cp.String()]
if !ok {
return types.TickerPrice{}, fmt.Errorf("bitget failed to get ticker price for %s", cp.String())
return types.TickerPrice{}, fmt.Errorf(
types.ErrTickerNotFound.Error(),
p.endpoints.Name,
cp.String(),
)
}

return ticker.toTickerPrice()
Expand All @@ -332,7 +356,11 @@ func (p *BitgetProvider) getCandlePrices(cp types.CurrencyPair) ([]types.CandleP

candles, ok := p.candles[cp.String()]
if !ok {
return []types.CandlePrice{}, fmt.Errorf("failed to get candles price for %s", cp.String())
return []types.CandlePrice{}, fmt.Errorf(
types.ErrCandleNotFound.Error(),
p.endpoints.Name,
cp.String(),
)
}

candleList := []types.CandlePrice{}
Expand Down
4 changes: 2 additions & 2 deletions price-feeder/oracle/provider/bitget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestBitgetProvider_GetTickerPrices(t *testing.T) {

t.Run("invalid_request_invalid_ticker", func(t *testing.T) {
prices, err := p.GetTickerPrices(types.CurrencyPair{Base: "FOO", Quote: "BAR"})
require.EqualError(t, err, "bitget failed to get ticker price for FOOBAR")
require.EqualError(t, err, "bitget has no ticker data for requested pairs: [FOOBAR]")
require.Nil(t, prices)
})
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestBitgetProvider_GetCandlePrices(t *testing.T) {

t.Run("invalid_request_invalid_candle", func(t *testing.T) {
prices, err := p.GetCandlePrices(types.CurrencyPair{Base: "FOO", Quote: "BAR"})
require.EqualError(t, err, "failed to get candles price for FOOBAR")
require.EqualError(t, err, "bitget has no candle data for requested pairs: [FOOBAR]")
require.Nil(t, prices)
})
}
Expand Down
40 changes: 30 additions & 10 deletions price-feeder/oracle/provider/coinbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,28 @@ func (p *CoinbaseProvider) SubscribeCurrencyPairs(cps ...types.CurrencyPair) err
return nil
}

// GetTickerPrices returns the tickerPrices based on the saved map.
// GetTickerPrices returns the tickerPrices based on the provided pairs.
func (p *CoinbaseProvider) GetTickerPrices(pairs ...types.CurrencyPair) (map[string]types.TickerPrice, error) {
tickerPrices := make(map[string]types.TickerPrice, len(pairs))

for _, currencyPair := range pairs {
price, err := p.getTickerPrice(currencyPair)
tickerErrs := 0
for _, cp := range pairs {
price, err := p.getTickerPrice(cp)
if err != nil {
return nil, err
p.logger.Warn().Err(err)
tickerErrs++
continue
}

tickerPrices[currencyPair.String()] = price
tickerPrices[cp.String()] = price
}

if tickerErrs == len(pairs) {
return nil, fmt.Errorf(
types.ErrNoTickers.Error(),
p.endpoints.Name,
pairs,
)
}
return tickerPrices, nil
}

Expand All @@ -193,16 +202,23 @@ func (p *CoinbaseProvider) GetTickerPrices(pairs ...types.CurrencyPair) (map[str
func (p *CoinbaseProvider) GetCandlePrices(pairs ...types.CurrencyPair) (map[string][]types.CandlePrice, error) {
tradeMap := make(map[string][]CoinbaseTrade, len(pairs))

tradeErrs := 0
for _, cp := range pairs {
key := currencyPairToCoinbasePair(cp)
tradeSet, err := p.getTradePrices(key)
if err != nil {
return nil, err
p.logger.Warn().Err(err)
tradeErrs++
continue
}
tradeMap[key] = tradeSet
}
if len(tradeMap) == 0 {
return nil, fmt.Errorf("no trades have been received")
if tradeErrs == len(pairs) {
return nil, fmt.Errorf(
types.ErrNoTickers.Error(),
p.endpoints.Name,
pairs,
)
}

candles := make(map[string][]types.CandlePrice)
Expand Down Expand Up @@ -292,7 +308,11 @@ func (p *CoinbaseProvider) getTickerPrice(cp types.CurrencyPair) (types.TickerPr
return tickerPair.toTickerPrice()
}

return types.TickerPrice{}, fmt.Errorf("coinbase failed to get ticker price for %s", gp)
return types.TickerPrice{}, fmt.Errorf(
types.ErrTickerNotFound.Error(),
p.endpoints.Name,
gp,
)
}

func (p *CoinbaseProvider) getTradePrices(key string) ([]CoinbaseTrade, error) {
Expand Down
2 changes: 1 addition & 1 deletion price-feeder/oracle/provider/coinbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestCoinbaseProvider_GetTickerPrices(t *testing.T) {

t.Run("invalid_request_invalid_ticker", func(t *testing.T) {
prices, err := p.GetTickerPrices(types.CurrencyPair{Base: "FOO", Quote: "BAR"})
require.EqualError(t, err, "coinbase failed to get ticker price for FOO-BAR")
require.EqualError(t, err, "coinbase has no ticker data for requested pairs: [FOOBAR]")
require.Nil(t, prices)
})
}
Expand Down
32 changes: 26 additions & 6 deletions price-feeder/oracle/provider/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,35 +185,55 @@ func (p *CryptoProvider) SubscribeCurrencyPairs(cps ...types.CurrencyPair) error
return nil
}

// GetTickerPrices returns the tickerPrices based on the saved map.
// GetTickerPrices returns the tickerPrices based on the provided pairs.
func (p *CryptoProvider) GetTickerPrices(pairs ...types.CurrencyPair) (map[string]types.TickerPrice, error) {
tickerPrices := make(map[string]types.TickerPrice, len(pairs))

tickerErrs := 0
for _, cp := range pairs {
key := currencyPairToCryptoPair(cp)
price, err := p.getTickerPrice(key)
if err != nil {
return nil, err
p.logger.Warn().Err(err)
tickerErrs++
continue
}
tickerPrices[cp.String()] = price
}

if tickerErrs == len(pairs) {
return nil, fmt.Errorf(
types.ErrNoTickers.Error(),
p.endpoints.Name,
pairs,
)
}
return tickerPrices, nil
}

// GetCandlePrices returns the candlePrices based on the saved map
// GetCandlePrices returns the candlePrices based on the provided pairs.
func (p *CryptoProvider) GetCandlePrices(pairs ...types.CurrencyPair) (map[string][]types.CandlePrice, error) {
candlePrices := make(map[string][]types.CandlePrice, len(pairs))

candleErrs := 0
for _, cp := range pairs {
key := currencyPairToCryptoPair(cp)
prices, err := p.getCandlePrices(key)
if err != nil {
return nil, err
p.logger.Warn().Err(err)
candleErrs++
continue
}
candlePrices[cp.String()] = prices
}

if candleErrs == len(pairs) {
return nil, fmt.Errorf(
types.ErrNoCandles.Error(),
p.endpoints.Name,
pairs,
)
}
return candlePrices, nil
}

Expand All @@ -225,7 +245,7 @@ func (p *CryptoProvider) getTickerPrice(key string) (types.TickerPrice, error) {
if !ok {
return types.TickerPrice{}, fmt.Errorf(
types.ErrTickerNotFound.Error(),
ProviderCrypto,
p.endpoints.Name,
key,
)
}
Expand All @@ -241,7 +261,7 @@ func (p *CryptoProvider) getCandlePrices(key string) ([]types.CandlePrice, error
if !ok {
return []types.CandlePrice{}, fmt.Errorf(
types.ErrCandleNotFound.Error(),
ProviderCrypto,
p.endpoints.Name,
key,
)
}
Expand Down
4 changes: 2 additions & 2 deletions price-feeder/oracle/provider/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestCryptoProvider_GetTickerPrices(t *testing.T) {
t.Run("invalid_request_invalid_ticker", func(t *testing.T) {
prices, err := p.GetTickerPrices(types.CurrencyPair{Base: "FOO", Quote: "BAR"})
require.Error(t, err)
require.Equal(t, "crypto failed to get ticker price for FOO_BAR", err.Error())
require.Equal(t, "crypto has no ticker data for requested pairs: [FOOBAR]", err.Error())
require.Nil(t, prices)
})
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestCryptoProvider_GetCandlePrices(t *testing.T) {

t.Run("invalid_request_invalid_candle", func(t *testing.T) {
prices, err := p.GetCandlePrices(types.CurrencyPair{Base: "FOO", Quote: "BAR"})
require.EqualError(t, err, "crypto failed to get candle price for FOO_BAR")
require.EqualError(t, err, "crypto has no candle data for requested pairs: [FOOBAR]")
require.Nil(t, prices)
})
}
Expand Down
Loading