diff --git a/protocol/lib/slinky/utils.go b/protocol/lib/slinky/utils.go new file mode 100644 index 0000000000..1a3417a9cc --- /dev/null +++ b/protocol/lib/slinky/utils.go @@ -0,0 +1,29 @@ +package slinky + +import ( + "fmt" + "strings" + + "github.com/skip-mev/slinky/pkg/types" +) + +/* + * Slinky utility functions + * + * This file contains functions for converting between x/prices types and slinky's x/oracle equivalents. + */ + +// MarketPairToCurrencyPair converts a base and quote pair from MarketPrice format (for example BTC-ETH) +// to a currency pair type. Returns an error if unable to convert. +func MarketPairToCurrencyPair(marketPair string) (types.CurrencyPair, error) { + split := strings.Split(marketPair, "-") + if len(split) != 2 { + return types.CurrencyPair{}, fmt.Errorf("incorrectly formatted CurrencyPair: %s", marketPair) + } + cp := types.CurrencyPair{ + Base: strings.ToUpper(split[0]), + Quote: strings.ToUpper(split[1]), + } + + return cp, cp.ValidateBasic() +} diff --git a/protocol/lib/slinky/utils_test.go b/protocol/lib/slinky/utils_test.go new file mode 100644 index 0000000000..a2ee405e4b --- /dev/null +++ b/protocol/lib/slinky/utils_test.go @@ -0,0 +1,34 @@ +package slinky_test + +import ( + "fmt" + "testing" + + "github.com/skip-mev/slinky/pkg/types" + "github.com/stretchr/testify/require" + + "github.com/dydxprotocol/v4-chain/protocol/lib/slinky" +) + +func TestMarketPairToCurrencyPair(t *testing.T) { + testCases := []struct { + mp string + cp types.CurrencyPair + err error + }{ + {mp: "FOO-BAR", cp: types.CurrencyPair{Base: "FOO", Quote: "BAR"}, err: nil}, + {mp: "FOOBAR", cp: types.CurrencyPair{}, err: fmt.Errorf("incorrectly formatted CurrencyPair: FOOBAR")}, + {mp: "FOO/BAR", cp: types.CurrencyPair{}, err: fmt.Errorf("incorrectly formatted CurrencyPair: FOOBAR")}, + } + for _, tc := range testCases { + t.Run(fmt.Sprintf("TestMarketPair %s", tc.mp), func(t *testing.T) { + cp, err := slinky.MarketPairToCurrencyPair(tc.mp) + if tc.err != nil { + require.Error(t, err, tc.err) + } else { + require.NoError(t, err) + require.Equal(t, tc.cp, cp) + } + }) + } +} diff --git a/protocol/x/prices/keeper/slinky_adapter.go b/protocol/x/prices/keeper/slinky_adapter.go index a64e72b2a6..bdc735cebb 100644 --- a/protocol/x/prices/keeper/slinky_adapter.go +++ b/protocol/x/prices/keeper/slinky_adapter.go @@ -1,12 +1,15 @@ package keeper import ( - "cosmossdk.io/math" "fmt" + "strings" + + "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" slinkytypes "github.com/skip-mev/slinky/pkg/types" oracletypes "github.com/skip-mev/slinky/x/oracle/types" - "strings" + + "github.com/dydxprotocol/v4-chain/protocol/lib/slinky" ) /* @@ -22,7 +25,7 @@ func (k Keeper) GetCurrencyPairFromID(ctx sdk.Context, id uint64) (cp slinkytype if !found { return cp, false } - cp, err := MarketPairToCurrencyPair(mp.Pair) + cp, err := slinky.MarketPairToCurrencyPair(mp.Pair) if err != nil { k.Logger(ctx).Error("CurrencyPairFromString", "error", err) return cp, false @@ -33,7 +36,7 @@ func (k Keeper) GetCurrencyPairFromID(ctx sdk.Context, id uint64) (cp slinkytype func (k Keeper) GetIDForCurrencyPair(ctx sdk.Context, cp slinkytypes.CurrencyPair) (uint64, bool) { mps := k.GetAllMarketParams(ctx) for _, mp := range mps { - mpCp, err := MarketPairToCurrencyPair(mp.Pair) + mpCp, err := slinky.MarketPairToCurrencyPair(mp.Pair) if err != nil { k.Logger(ctx).Error("market param pair invalid format", "pair", mp.Pair) continue @@ -58,16 +61,3 @@ func (k Keeper) GetPriceForCurrencyPair(ctx sdk.Context, cp slinkytypes.Currency Price: math.NewIntFromUint64(mp.Price), }, nil } - -func MarketPairToCurrencyPair(marketPair string) (slinkytypes.CurrencyPair, error) { - split := strings.Split(marketPair, "-") - if len(split) != 2 { - return slinkytypes.CurrencyPair{}, fmt.Errorf("incorrectly formatted CurrencyPair: %s", marketPair) - } - cp := slinkytypes.CurrencyPair{ - Base: strings.ToUpper(split[0]), - Quote: strings.ToUpper(split[1]), - } - - return cp, cp.ValidateBasic() -} diff --git a/protocol/x/prices/keeper/slinky_adapter_test.go b/protocol/x/prices/keeper/slinky_adapter_test.go index 03373c85d0..b25fa9ed66 100644 --- a/protocol/x/prices/keeper/slinky_adapter_test.go +++ b/protocol/x/prices/keeper/slinky_adapter_test.go @@ -2,6 +2,7 @@ package keeper_test import ( "fmt" + "github.com/dydxprotocol/v4-chain/protocol/lib/slinky" "testing" oracletypes "github.com/skip-mev/slinky/pkg/types" @@ -9,7 +10,6 @@ import ( "github.com/dydxprotocol/v4-chain/protocol/testutil/constants" keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper" - "github.com/dydxprotocol/v4-chain/protocol/x/prices/keeper" "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" ) @@ -117,7 +117,7 @@ func TestMarketPairToCurrencyPair(t *testing.T) { } for _, tc := range testCases { t.Run(tc.marketPair, func(t *testing.T) { - cp, err := keeper.MarketPairToCurrencyPair(tc.marketPair) + cp, err := slinky.MarketPairToCurrencyPair(tc.marketPair) if tc.shouldFail { require.Error(t, err) } else {