Skip to content

Commit

Permalink
feat: add logging to price sources
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Feb 24, 2024
1 parent 4646886 commit 54b1151
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 41 deletions.
3 changes: 2 additions & 1 deletion feeder/priceprovider/sources/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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

const (
Expand All @@ -31,7 +32,7 @@ func BinanceSymbolCsv(symbols set.Set[types.Symbol]) string {

// BinancePriceUpdate returns the prices given the symbols or an error.
// Uses the Binance API at https://docs.binance.us/#price-data.
func BinancePriceUpdate(symbols set.Set[types.Symbol]) (rawPrices map[types.Symbol]float64, err error) {
func BinancePriceUpdate(symbols set.Set[types.Symbol], logger zerolog.Logger) (rawPrices map[types.Symbol]float64, err error) {
url := "https://api.binance.us/api/v3/ticker/price?symbols=%5B" + BinanceSymbolCsv(symbols) + "%5D"
resp, err := http.Get(url)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions feeder/priceprovider/sources/binance_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +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 TestBinanceSource(t *testing.T) {
t.Run("success", func(t *testing.T) {
rawPrices, err := BinancePriceUpdate(set.New[types.Symbol]("BTCUSD", "ETHUSD"))
rawPrices, err := BinancePriceUpdate(set.New[types.Symbol]("BTCUSD", "ETHUSD"), zerolog.New(io.Discard))
require.NoError(t, err)
require.Equal(t, 2, len(rawPrices))
require.NotZero(t, rawPrices["BTCUSD"])
require.NotZero(t, rawPrices["ETHUSD"])
})

}
5 changes: 3 additions & 2 deletions feeder/priceprovider/sources/bitfinex.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (

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

const (
Bitfinex = "bitfinex"
)

var _ types.FetchPricesFunc = BinancePriceUpdate
var _ types.FetchPricesFunc = BitfinexPriceUpdate

func BitfinexSymbolCsv(symbols set.Set[types.Symbol]) string {
s := ""
Expand All @@ -25,7 +26,7 @@ func BitfinexSymbolCsv(symbols set.Set[types.Symbol]) string {
}

// BitfinexPriceUpdate returns the prices given the symbols or an error.
func BitfinexPriceUpdate(symbols set.Set[types.Symbol]) (rawPrices map[types.Symbol]float64, err error) {
func BitfinexPriceUpdate(symbols set.Set[types.Symbol], logger zerolog.Logger) (rawPrices map[types.Symbol]float64, err error) {
type ticker []interface{}
const size = 11
const lastPriceIndex = 7
Expand Down
5 changes: 3 additions & 2 deletions feeder/priceprovider/sources/bitfinex_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +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 TestBitfinexSource(t *testing.T) {
t.Run("success", func(t *testing.T) {
rawPrices, err := BitfinexPriceUpdate(set.New[types.Symbol]("tBTCUSD", "tETHUSD"))
rawPrices, err := BitfinexPriceUpdate(set.New[types.Symbol]("tBTCUSD", "tETHUSD"), zerolog.New(io.Discard))
require.NoError(t, err)
require.Equal(t, 2, len(rawPrices))
require.NotZero(t, rawPrices["tBTCUSD"])
require.NotZero(t, rawPrices["tETHUSD"])
})

}
18 changes: 9 additions & 9 deletions feeder/priceprovider/sources/coingecko.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

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

const (
Expand All @@ -27,15 +28,13 @@ type CoingeckoConfig struct {
}

func CoingeckoPriceUpdate(sourceConfig json.RawMessage) types.FetchPricesFunc {
return func(symbols set.Set[types.Symbol]) (map[types.Symbol]float64, error) {
return func(symbols set.Set[types.Symbol], logger zerolog.Logger) (map[types.Symbol]float64, error) {
c, err := extractConfig(sourceConfig)
if err != nil {
return nil, err
}

baseURL := buildURL(symbols, c)

res, err := http.Get(baseURL)
res, err := http.Get(buildURL(symbols, c))
if err != nil {
return nil, err
}
Expand All @@ -46,7 +45,7 @@ func CoingeckoPriceUpdate(sourceConfig json.RawMessage) types.FetchPricesFunc {
return nil, err
}

rawPrices, err := extractPricesFromResponse(symbols, response)
rawPrices, err := extractPricesFromResponse(symbols, response, logger)
if err != nil {
return nil, err
}
Expand All @@ -67,7 +66,7 @@ func extractConfig(jsonConfig json.RawMessage) (*CoingeckoConfig, error) {
return c, nil
}

func extractPricesFromResponse(symbols set.Set[types.Symbol], response []byte) (map[types.Symbol]float64, error) {
func extractPricesFromResponse(symbols set.Set[types.Symbol], response []byte, logger zerolog.Logger) (map[types.Symbol]float64, error) {
var result map[string]CoingeckoTicker
err := json.Unmarshal(response, &result)
if err != nil {
Expand All @@ -79,7 +78,8 @@ func extractPricesFromResponse(symbols set.Set[types.Symbol], response []byte) (
if price, ok := result[string(symbol)]; ok {
rawPrices[symbol] = price.Price
} else {
return nil, fmt.Errorf("symbol %s not found in response: %s\n", symbol, response)
logger.Err(err).Msg(fmt.Sprintf("failed to parse price for %s on data source %s", symbol, Coingecko))
continue
}
}

Expand All @@ -101,8 +101,8 @@ func buildURL(symbols set.Set[types.Symbol], c *CoingeckoConfig) string {
params.Add(ApiKeyParam, c.ApiKey)
}

baseURL = baseURL + params.Encode()
return baseURL
url := baseURL + params.Encode()
return url
}

// coingeckoSymbolCsv returns the symbols as a comma separated string.
Expand Down
5 changes: 5 additions & 0 deletions feeder/priceprovider/sources/coingecko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package sources

import (
"encoding/json"
"io"
"testing"

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

Expand All @@ -24,6 +26,7 @@ func TestCoingeckoPriceUpdate(t *testing.T) {
"bitcoin",
"ethereum",
),
zerolog.New(io.Discard),
)
require.NoError(t, err)

Expand Down Expand Up @@ -59,6 +62,7 @@ func TestCoingeckoWithConfig(t *testing.T) {
"bitcoin",
"ethereum",
),
zerolog.New(io.Discard),
)
require.NoError(t, err)

Expand All @@ -83,6 +87,7 @@ func TestCoingeckoWithConfig(t *testing.T) {
"bitcoin",
"ethereum",
),
zerolog.New(io.Discard),
)
require.NoError(t, err)

Expand Down
14 changes: 8 additions & 6 deletions feeder/priceprovider/sources/coinmarketcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

const (
Expand Down Expand Up @@ -39,9 +40,9 @@ type CoinmarketcapConfig struct {
ApiKey string `json:"api_key"`
}

func CoinmarketcapPriceUpdate(rawConfig json.RawMessage) types.FetchPricesFunc {
return func(symbols set.Set[types.Symbol]) (map[types.Symbol]float64, error) {
config, err := getConfig(rawConfig)
func CoinmarketcapPriceUpdate(coinmarketcapConfig json.RawMessage) types.FetchPricesFunc {
return func(symbols set.Set[types.Symbol], logger zerolog.Logger) (map[types.Symbol]float64, error) {
config, err := getConfig(coinmarketcapConfig)
if err != nil {
return nil, err
}
Expand All @@ -63,7 +64,7 @@ func CoinmarketcapPriceUpdate(rawConfig json.RawMessage) types.FetchPricesFunc {
return nil, err
}

rawPrices, err := getPricesFromResponse(symbols, response)
rawPrices, err := getPricesFromResponse(symbols, response, logger)
if err != nil {
return nil, err
}
Expand All @@ -84,7 +85,7 @@ func getConfig(jsonConfig json.RawMessage) (*CoinmarketcapConfig, error) {
return c, nil
}

func getPricesFromResponse(symbols set.Set[types.Symbol], response []byte) (map[types.Symbol]float64, error) {
func getPricesFromResponse(symbols set.Set[types.Symbol], response []byte, logger zerolog.Logger) (map[types.Symbol]float64, error) {
var respCmc CmcResponse
err := json.Unmarshal(response, &respCmc)
if err != nil {
Expand All @@ -101,7 +102,8 @@ func getPricesFromResponse(symbols set.Set[types.Symbol], response []byte) (map[
if price, ok := cmcPrice[string(symbol)]; ok {
rawPrices[symbol] = price
} else {
return nil, fmt.Errorf("symbol %s not found in response: %s\n", symbol, response)
logger.Err(err).Msg(fmt.Sprintf("failed to parse price for %s on data source %s", symbol, CoinMarketCap))
continue
}
}

Expand Down
3 changes: 3 additions & 0 deletions feeder/priceprovider/sources/coinmarketcap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package sources

import (
"encoding/json"
"io"
"testing"

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

Expand All @@ -24,6 +26,7 @@ func TestCoinmarketcapPriceUpdate(t *testing.T) {
"bitcoin",
"ethereum",
),
zerolog.New(io.Discard),
)
require.NoError(t, err)

Expand Down
7 changes: 5 additions & 2 deletions feeder/priceprovider/sources/gateio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package sources

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

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

const (
Expand All @@ -18,7 +20,7 @@ var _ types.FetchPricesFunc = GateIoPriceUpdate

// GateIoPriceUpdate returns the prices given the symbols or an error.
// Uses the GateIo API at https://www.gate.io/docs/developers/apiv4/en/#get-details-of-a-specifc-currency-pair.
func GateIoPriceUpdate(symbols set.Set[types.Symbol]) (rawPrices map[types.Symbol]float64, err error) {
func GateIoPriceUpdate(symbols set.Set[types.Symbol], logger zerolog.Logger) (rawPrices map[types.Symbol]float64, err error) {
url := "https://api.gateio.ws/api/v4/spot/tickers"
resp, err := http.Get(url)
if err != nil {
Expand Down Expand Up @@ -46,7 +48,8 @@ func GateIoPriceUpdate(symbols set.Set[types.Symbol]) (rawPrices map[types.Symbo

price, err := strconv.ParseFloat(ticker["last"].(string), 64)
if err != nil {
price = -1
logger.Err(err).Msg(fmt.Sprintf("failed to parse price for %s on data source %s", symbol, GateIo))
continue
}

rawPrices[symbol] = price
Expand Down
4 changes: 3 additions & 1 deletion feeder/priceprovider/sources/gateio_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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 TestGateIoSource(t *testing.T) {
t.Run("success", func(t *testing.T) {
rawPrices, err := GateIoPriceUpdate(set.New[types.Symbol]("BTC_USDT", "ETH_USDT"))
rawPrices, err := GateIoPriceUpdate(set.New[types.Symbol]("BTC_USDT", "ETH_USDT"), zerolog.New(io.Discard))
require.NoError(t, err)
require.Equal(t, 2, len(rawPrices))
require.NotZero(t, rawPrices["BTC_USDT"])
Expand Down
7 changes: 5 additions & 2 deletions feeder/priceprovider/sources/okex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package sources

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

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

const (
Expand All @@ -27,7 +29,7 @@ type Response struct {

// 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) {
func OkexPriceUpdate(symbols set.Set[types.Symbol], logger zerolog.Logger) (rawPrices map[types.Symbol]float64, err error) {
url := "https://www.okx.com/api/v5/market/tickers?instType=SPOT"

resp, err := http.Get(url)
Expand Down Expand Up @@ -57,7 +59,8 @@ func OkexPriceUpdate(symbols set.Set[types.Symbol]) (rawPrices map[types.Symbol]

price, err := strconv.ParseFloat(ticker.Price, 64)
if err != nil {
price = -1
logger.Err(err).Msg(fmt.Sprintf("failed to parse price for %s on data source %s", symbol, Okex))
continue
}

rawPrices[symbol] = price
Expand Down
4 changes: 3 additions & 1 deletion feeder/priceprovider/sources/okex_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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 TestOKexPriceUpdate(t *testing.T) {
t.Run("success", func(t *testing.T) {
rawPrices, err := OkexPriceUpdate(set.New[types.Symbol]("BTC-USDT", "ETH-USDT"))
rawPrices, err := OkexPriceUpdate(set.New[types.Symbol]("BTC-USDT", "ETH-USDT"), zerolog.New(io.Discard))
require.NoError(t, err)
require.Equal(t, 2, len(rawPrices))
require.NotZero(t, rawPrices["BTC-USDT"])
Expand Down
Loading

0 comments on commit 54b1151

Please sign in to comment.