From 86b254a1a958bf2aa17e7311573cddad1f1232aa Mon Sep 17 00:00:00 2001 From: BURNING Date: Sat, 18 Mar 2023 13:08:43 +0700 Subject: [PATCH 1/2] Add Bybit datasource --- feeder/priceprovider/priceprovider.go | 2 + feeder/priceprovider/sources/bybit.go | 68 ++++++++++++++++++++++ feeder/priceprovider/sources/bybit_test.go | 24 ++++++++ 3 files changed, 94 insertions(+) create mode 100644 feeder/priceprovider/sources/bybit.go create mode 100644 feeder/priceprovider/sources/bybit_test.go diff --git a/feeder/priceprovider/priceprovider.go b/feeder/priceprovider/priceprovider.go index f55eb4f..5b4159a 100644 --- a/feeder/priceprovider/priceprovider.go +++ b/feeder/priceprovider/priceprovider.go @@ -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) default: panic("unknown price provider: " + sourceName) } diff --git a/feeder/priceprovider/sources/bybit.go b/feeder/priceprovider/sources/bybit.go new file mode 100644 index 0000000..fc469b8 --- /dev/null +++ b/feeder/priceprovider/sources/bybit.go @@ -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 { + + 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 +} diff --git a/feeder/priceprovider/sources/bybit_test.go b/feeder/priceprovider/sources/bybit_test.go new file mode 100644 index 0000000..ba18d8a --- /dev/null +++ b/feeder/priceprovider/sources/bybit_test.go @@ -0,0 +1,24 @@ +package sources + +import ( + "fmt" + "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"]) + fmt.Println(rawPrices) + fmt.Printf("BTC Price: %f\n", rawPrices["BTCUSDT"]) + fmt.Printf("ETH Price: %f\n", rawPrices["ETHUSDT"]) + }) +} From 5f38b1b398391c609cd5aea8362e1f610de29437 Mon Sep 17 00:00:00 2001 From: BURNING Date: Sat, 18 Mar 2023 13:09:30 +0700 Subject: [PATCH 2/2] Remove redundant prints --- feeder/priceprovider/sources/bybit_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/feeder/priceprovider/sources/bybit_test.go b/feeder/priceprovider/sources/bybit_test.go index ba18d8a..63deb22 100644 --- a/feeder/priceprovider/sources/bybit_test.go +++ b/feeder/priceprovider/sources/bybit_test.go @@ -1,7 +1,6 @@ package sources import ( - "fmt" "testing" "github.com/NibiruChain/nibiru/x/common/set" @@ -17,8 +16,5 @@ func TestBybitPriceUpdate(t *testing.T) { require.Equal(t, 2, len(rawPrices)) require.NotZero(t, rawPrices["BTCUSDT"]) require.NotZero(t, rawPrices["ETHUSDT"]) - fmt.Println(rawPrices) - fmt.Printf("BTC Price: %f\n", rawPrices["BTCUSDT"]) - fmt.Printf("ETH Price: %f\n", rawPrices["ETHUSDT"]) }) }