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

Add Bybit datasource #30

Closed
Closed
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
2 changes: 2 additions & 0 deletions feeder/priceprovider/priceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func NewPriceProvider(
source = sources.NewTickSource(symbolsFromPairToSymbolMapping(pairToSymbolMap), sources.BinancePriceUpdate, logger)
case sources.Coingecko:
source = sources.NewTickSource(symbolsFromPairToSymbolMapping(pairToSymbolMap), sources.CoingeckoPriceUpdate(config), logger)
case sources.Bybit:
source = sources.NewTickSource(symbolsFromPairToSymbolMapping(pairToSymbolMap), sources.BybitPriceUpdate, logger)
case sources.GateIo:
source = sources.NewTickSource(symbolsFromPairToSymbolMapping(pairToSymbolMap), sources.GateIoPriceUpdate, logger)
case sources.CoinMarketCap:
Expand Down
68 changes: 68 additions & 0 deletions feeder/priceprovider/sources/bybit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package sources

import (
"encoding/json"
"github.com/NibiruChain/nibiru/x/common/set"
"github.com/NibiruChain/pricefeeder/types"
"io"
"net/http"
"strconv"
)

const (
Bybit = "bybit"
)

var _ types.FetchPricesFunc = BybitPriceUpdate

type BybitTicker struct {
Symbol string `json:"symbol"`
Price string `json:"lastPrice"`
}

type Response struct {
Data struct {
List []BybitTicker `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]) (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 Response
err = json.Unmarshal(b, &response)
if err != nil {
return nil, err
}

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

for _, ticker := range response.Data.List {
k-yang marked this conversation as resolved.
Show resolved Hide resolved

symbol := types.Symbol(ticker.Symbol)
price, err := strconv.ParseFloat(ticker.Price, 64)
if err != nil {
return rawPrices, err
}

if _, ok := symbols[symbol]; ok {
rawPrices[symbol] = price
}

}
return rawPrices, nil
}
20 changes: 20 additions & 0 deletions feeder/priceprovider/sources/bybit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sources

import (
"testing"

"github.com/NibiruChain/nibiru/x/common/set"
"github.com/NibiruChain/pricefeeder/types"
"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"))
require.NoError(t, err)
require.Equal(t, 2, len(rawPrices))
require.NotZero(t, rawPrices["BTCUSDT"])
require.NotZero(t, rawPrices["ETHUSDT"])
})
}
Loading