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(priceprovider-bybit): add special exception for blocked regions in bybit test #55

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading