Skip to content

Commit

Permalink
fix(priceprovider-bybit): add special exception for blocked regions i…
Browse files Browse the repository at this point in the history
…n bybit test
  • Loading branch information
Unique-Divine committed Oct 9, 2024
1 parent 6f1804f commit 74dfe89
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions feeder/priceprovider/sources/bybit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package sources

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"

"github.com/NibiruChain/nibiru/x/common/set"
"github.com/NibiruChain/pricefeeder/metrics"
Expand All @@ -27,6 +29,8 @@ type BybitResponse struct {
} `json:"result"`
}

const ErrBybitBlockAccess = "configured to block access from your country"

// BybitPriceUpdate returns the prices for given symbols or an error.
// Uses BYBIT API at https://bybit-exchange.github.io/docs/v5/market/tickers.
func BybitPriceUpdate(symbols set.Set[types.Symbol], logger zerolog.Logger) (rawPrices map[types.Symbol]float64, err error) {
Expand All @@ -40,16 +44,19 @@ func BybitPriceUpdate(symbols set.Set[types.Symbol], logger zerolog.Logger) (raw
}
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
logger.Err(err).Msg("failed to read response body from Bybit")
metrics.PriceSourceCounter.WithLabelValues(Bybit, "false").Inc()
return nil, err
}

var response BybitResponse
err = json.Unmarshal(b, &response)
err = json.Unmarshal(respBody, &response)
if err != nil {
if strings.Contains(string(respBody), ErrBybitBlockAccess) {
err = fmt.Errorf("%s: %w", ErrBybitBlockAccess, err)
}
logger.Err(err).Msg("failed to unmarshal response body from Bybit")
metrics.PriceSourceCounter.WithLabelValues(Bybit, "false").Inc()
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions feeder/priceprovider/sources/bybit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
func TestBybitPriceUpdate(t *testing.T) {
t.Run("success", func(t *testing.T) {
rawPrices, err := BybitPriceUpdate(set.New[types.Symbol]("BTCUSDT", "ETHUSDT"), zerolog.New(io.Discard))
if err != nil {
require.ErrorContains(t, err, ErrBybitBlockAccess)
return
}
require.NoError(t, err)
require.Equal(t, 2, len(rawPrices))
require.NotZero(t, rawPrices["BTCUSDT"])
Expand Down

0 comments on commit 74dfe89

Please sign in to comment.