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

chore: Refactor utils into pure functions #634

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 1 deletion providers/apis/dydx/api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (h *APIHandler) ParseResponse(
}

// Convert the dydx market params to a market map.
marketResp, err := h.ConvertMarketParamsToMarketMap(params)
marketResp, err := ConvertMarketParamsToMarketMap(params, h.logger)
if err != nil {
h.logger.Debug(
"failed to convert dydx market params to market map",
Expand Down
66 changes: 38 additions & 28 deletions providers/apis/dydx/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,44 +54,51 @@
}

// ConvertMarketParamsToMarketMap converts a dYdX market params response to a slinky market map response.
func (h *APIHandler) ConvertMarketParamsToMarketMap(
func ConvertMarketParamsToMarketMap(
params dydxtypes.QueryAllMarketParamsResponse,
logger *zap.Logger,
) (mmtypes.MarketMapResponse, error) {
marketMap := mmtypes.MarketMap{
Markets: make(map[string]mmtypes.Market),
}

for _, market := range params.MarketParams {
ticker, err := h.CreateTickerFromMarket(market)
ticker, err := CreateTickerFromMarket(market)
if err != nil {
h.logger.Debug(
"failed to create ticker from market",
zap.String("market", market.Pair),
zap.Error(err),
)
if logger != nil {
logger.Debug(
"failed to create ticker from market",
zap.String("market", market.Pair),
zap.Error(err),
)
}

return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to create ticker from market: %w", err)
}

var exchangeConfigJSON dydxtypes.ExchangeConfigJson
if err := json.Unmarshal([]byte(market.ExchangeConfigJson), &exchangeConfigJSON); err != nil {
h.logger.Debug(
"failed to unmarshal exchange json config",
zap.String("ticker", ticker.String()),
zap.Error(err),
)
if logger != nil {
logger.Debug(
"failed to unmarshal exchange json config",
zap.String("ticker", ticker.String()),
zap.Error(err),
)

Check warning on line 86 in providers/apis/dydx/parse.go

View check run for this annotation

Codecov / codecov/patch

providers/apis/dydx/parse.go#L81-L86

Added lines #L81 - L86 were not covered by tests
}

return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to unmarshal exchange json config: %w", err)
}

// Convert the exchange config JSON to a set of paths and providers.
providers, err := h.ConvertExchangeConfigJSON(exchangeConfigJSON)
providers, err := ConvertExchangeConfigJSON(exchangeConfigJSON, logger)
if err != nil {
h.logger.Debug(
"failed to convert exchange config json",
zap.String("ticker", ticker.String()),
zap.Error(err),
)
if logger != nil {
logger.Debug(
"failed to convert exchange config json",
zap.String("ticker", ticker.String()),
zap.Error(err),
)

Check warning on line 100 in providers/apis/dydx/parse.go

View check run for this annotation

Codecov / codecov/patch

providers/apis/dydx/parse.go#L95-L100

Added lines #L95 - L100 were not covered by tests
}

return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to convert exchange config json: %w", err)
}
Expand All @@ -108,8 +115,8 @@
}

// CreateTickerFromMarket creates a ticker from a dYdX market.
func (h *APIHandler) CreateTickerFromMarket(market dydxtypes.MarketParam) (mmtypes.Ticker, error) {
cp, err := h.CreateCurrencyPairFromPair(market.Pair)
func CreateTickerFromMarket(market dydxtypes.MarketParam) (mmtypes.Ticker, error) {
cp, err := CreateCurrencyPairFromPair(market.Pair)
if err != nil {
return mmtypes.Ticker{}, err
}
Expand All @@ -125,7 +132,7 @@
}

// CreateCurrencyPairFromPair creates a currency pair from a dYdX market.
func (h *APIHandler) CreateCurrencyPairFromPair(pair string) (slinkytypes.CurrencyPair, error) {
func CreateCurrencyPairFromPair(pair string) (slinkytypes.CurrencyPair, error) {
split := strings.Split(pair, Delimiter)
if len(split) != 2 {
return slinkytypes.CurrencyPair{}, fmt.Errorf("expected pair (%s) to have 2 elements, got %d", pair, len(split))
Expand All @@ -142,8 +149,9 @@
// ConvertExchangeConfigJSON creates a set of paths and providers for a given ticker
// from a dYdX market. These paths represent the different ways to convert a currency
// pair using the dYdX market.
func (h *APIHandler) ConvertExchangeConfigJSON(
func ConvertExchangeConfigJSON(
config dydxtypes.ExchangeConfigJson,
logger *zap.Logger,
) ([]mmtypes.ProviderConfig, error) {
var (
providers = make([]mmtypes.ProviderConfig, 0, len(config.Exchanges))
Expand All @@ -161,19 +169,21 @@
exchange, ok := ProviderMapping[cfg.ExchangeName]
if !ok {
// ignore unsupported exchanges
h.logger.Debug(
"skipping unsupported exchange",
zap.String("exchange", cfg.ExchangeName),
zap.String("ticker", cfg.Ticker),
)
if logger != nil {
logger.Debug(
"skipping unsupported exchange",
zap.String("exchange", cfg.ExchangeName),
zap.String("ticker", cfg.Ticker),
)
}

continue
}

// Determine if the exchange needs to have an normalizeByPair.
var normalizeByPair *slinkytypes.CurrencyPair
if len(cfg.AdjustByMarket) > 0 {
temp, err := h.CreateCurrencyPairFromPair(cfg.AdjustByMarket)
temp, err := CreateCurrencyPairFromPair(cfg.AdjustByMarket)
if err != nil {
return nil, fmt.Errorf(
"failed to create normalize by pair for %s: %w",
Expand Down
24 changes: 6 additions & 18 deletions providers/apis/dydx/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ func TestConvertMarketParamsToMarketMap(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
handler, err := dydx.NewAPIHandler(zap.NewNop(), dydx.DefaultAPIConfig)
require.NoError(t, err)

resp, err := handler.ConvertMarketParamsToMarketMap(tc.params)
resp, err := dydx.ConvertMarketParamsToMarketMap(tc.params, zap.NewNop())
if tc.err {
require.Error(t, err)
} else {
Expand All @@ -83,26 +80,23 @@ func TestConvertMarketParamsToMarketMap(t *testing.T) {
}

func TestCreateCurrencyPairFromMarket(t *testing.T) {
handler, err := dydx.NewAPIHandler(zap.NewNop(), dydx.DefaultAPIConfig)
require.NoError(t, err)

t.Run("good ticker", func(t *testing.T) {
pair := "BTC-USD"
cp, err := handler.CreateCurrencyPairFromPair(pair)
cp, err := dydx.CreateCurrencyPairFromPair(pair)
require.NoError(t, err)
require.Equal(t, cp.Base, "BTC")
require.Equal(t, cp.Quote, "USD")
})

t.Run("bad ticker", func(t *testing.T) {
pair := "BTCUSD"
_, err := handler.CreateCurrencyPairFromPair(pair)
_, err := dydx.CreateCurrencyPairFromPair(pair)
require.Error(t, err)
})

t.Run("lower casing still corrects", func(t *testing.T) {
pair := "btc-usd"
cp, err := handler.CreateCurrencyPairFromPair(pair)
cp, err := dydx.CreateCurrencyPairFromPair(pair)
require.NoError(t, err)
require.Equal(t, cp.Base, "BTC")
require.Equal(t, cp.Quote, "USD")
Expand Down Expand Up @@ -165,10 +159,7 @@ func TestCreateTickerFromMarket(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
handler, err := dydx.NewAPIHandler(zap.NewNop(), dydx.DefaultAPIConfig)
require.NoError(t, err)

ticker, err := handler.CreateTickerFromMarket(tc.market)
ticker, err := dydx.CreateTickerFromMarket(tc.market)
if tc.err {
require.Error(t, err)
} else {
Expand Down Expand Up @@ -401,10 +392,7 @@ func TestConvertExchangeConfigJSON(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
handler, err := dydx.NewAPIHandler(zap.NewNop(), dydx.DefaultAPIConfig)
require.NoError(t, err)

providers, err := handler.ConvertExchangeConfigJSON(tc.config)
providers, err := dydx.ConvertExchangeConfigJSON(tc.config, zap.NewNop())
if tc.expectedErr {
require.Error(t, err)
return
Expand Down
2 changes: 1 addition & 1 deletion providers/apis/dydx/research_api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (h *ResearchAPIHandler) ParseResponse(
}

// convert the response to a market-map
marketMap, err := h.ConvertMarketParamsToMarketMap(respMarketParams)
marketMap, err := ConvertMarketParamsToMarketMap(respMarketParams, h.logger)
if err != nil {
h.logger.Error("failed to convert QueryAllMarketsParamsResponse into MarketMap", zap.Error(err))
return types.NewMarketMapResponseWithErr(
Expand Down
Loading