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

feat: add bybit data source #39

Merged
merged 2 commits into from
Mar 21, 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
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ var defaultExchangeSymbolsMap = map[string]map[asset.Pair]types.Symbol{
"uusdt:uusd": "USDT-USDC",
"uatom:uusd": "ATOM-USDT",
},
// https://api.bybit.com/v5/market/tickers?category=spot
sources.Bybit: {
"ubtc:uusd": "BTCUSDT",
"ueth:uusd": "ETHUSDT",
"uusdc:uusd": "USDCUSDT",
"uatom:uusd": "ATOMUSDT",
"unibi:uusd": "NIBIUSDT",
},
}

func MustGet() *Config {
Expand Down
2 changes: 2 additions & 0 deletions feeder/priceprovider/priceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func NewPriceProvider(
source = sources.NewTickSource(mapValues(pairToSymbolMap), sources.GateIoPriceUpdate, logger)
case sources.CoinMarketCap:
source = sources.NewTickSource(mapValues(pairToSymbolMap), sources.CoinmarketcapPriceUpdate(config), logger)
case sources.Bybit:
source = sources.NewTickSource(mapValues(pairToSymbolMap), sources.BybitPriceUpdate, logger)
default:
panic("unknown price provider: " + sourceName)
}
Expand Down
67 changes: 67 additions & 0 deletions feeder/priceprovider/sources/bybit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package sources

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

"github.com/NibiruChain/nibiru/x/common/set"
"github.com/NibiruChain/pricefeeder/types"
"github.com/rs/zerolog"
)

const (
Bybit = "bybit"
)

var _ types.FetchPricesFunc = BybitPriceUpdate

type BybitResponse struct {
Data struct {
List []struct {
Symbol string `json:"symbol"`
Price string `json:"lastPrice"`
} `json:"list"`
} `json:"result"`
}

// 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) {
url := "https://api.bybit.com/v5/market/tickers?category=spot"

resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var response BybitResponse
err = json.Unmarshal(b, &response)
if err != nil {
return nil, err
}

rawPrices = make(map[types.Symbol]float64)

for _, ticker := range response.Data.List {
symbol := types.Symbol(ticker.Symbol)
price, err := strconv.ParseFloat(ticker.Price, 64)
if err != nil {
logger.Err(err).Msgf("failed to parse price for %s on data source %s", symbol, Bybit)
continue
}

if _, ok := symbols[symbol]; ok {
rawPrices[symbol] = price
}
}
logger.Debug().Msgf("fetched prices for %s on data source %s: %v", symbols, Bybit, rawPrices)
return rawPrices, nil
}
21 changes: 21 additions & 0 deletions feeder/priceprovider/sources/bybit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sources

import (
"io"
"testing"

"github.com/NibiruChain/nibiru/x/common/set"
"github.com/NibiruChain/pricefeeder/types"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

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))
require.NoError(t, err)
require.Equal(t, 2, len(rawPrices))
require.NotZero(t, rawPrices["BTCUSDT"])
require.NotZero(t, rawPrices["ETHUSDT"])
})
}
4 changes: 2 additions & 2 deletions feeder/priceprovider/sources/okex.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type OkexTicker struct {
Price string `json:"last"`
}

type Response struct {
type OkexResponse struct {
Data []OkexTicker `json:"data"`
}

Expand All @@ -43,7 +43,7 @@ func OkexPriceUpdate(symbols set.Set[types.Symbol], logger zerolog.Logger) (rawP
return nil, err
}

var response Response
var response OkexResponse
err = json.Unmarshal(b, &response)
if err != nil {
return nil, err
Expand Down
Loading