Skip to content

Commit

Permalink
Okex datasource (#38)
Browse files Browse the repository at this point in the history
* Add OKEX data source

* Add OKEX data source to list of providers

---------

Co-authored-by: Uladzislau K <uladzislau.kuzmin@gmail.com>
  • Loading branch information
k-yang and hdmiimdh authored Feb 24, 2024
1 parent c7528cd commit 146a622
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
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.Okex:
source = sources.NewTickSource(symbolsFromPairToSymbolMapping(pairToSymbolMap), sources.OkexPriceUpdate, logger)
case sources.GateIo:
source = sources.NewTickSource(symbolsFromPairToSymbolMapping(pairToSymbolMap), sources.GateIoPriceUpdate, logger)
case sources.CoinMarketCap:
Expand Down
67 changes: 67 additions & 0 deletions feeder/priceprovider/sources/okex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package sources

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

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

const (
Okex = "okex"
)

var _ types.FetchPricesFunc = OkexPriceUpdate

type OkexTicker struct {
Symbol string `json:"instId"`
Price string `json:"last"`
}

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

// OkexPriceUpdate returns the prices for given symbols or an error.
// Uses OKEX API at https://www.okx.com/docs-v5/en/#rest-api-market-data.
func OkexPriceUpdate(symbols set.Set[types.Symbol]) (rawPrices map[types.Symbol]float64, err error) {

url := "https://www.okx.com/api/v5/market/tickers?instType=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 {

symbol := types.Symbol(strings.Replace(ticker.Symbol, "-", "", -1))
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/okex_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 TestOKexPriceUpdate(t *testing.T) {

t.Run("success", func(t *testing.T) {
rawPrices, err := OkexPriceUpdate(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"])
})
}

0 comments on commit 146a622

Please sign in to comment.