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

[TRA-530] When changing market pair name, validate that new pair exists in the market map #2151

Merged
merged 7 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 10 additions & 6 deletions protocol/x/prices/keeper/market_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ func (k Keeper) ModifyMarketParam(
}
}

// Validate that modified market param has a corresponding ticker in MarketMap
cp, err := slinky.MarketPairToCurrencyPair(updatedMarketParam.Pair)
hwray marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
hwray marked this conversation as resolved.
Show resolved Hide resolved
return types.MarketParam{}, errorsmod.Wrapf(types.ErrMarketPairConversionFailed, updatedMarketParam.Pair)
}
hwray marked this conversation as resolved.
Show resolved Hide resolved
if _, err := k.MarketMapKeeper.GetMarket(ctx, cp.String()); err != nil {
return types.MarketParam{}, errorsmod.Wrapf(types.ErrTickerNotFoundInMarketMap, cp.String())
}
hwray marked this conversation as resolved.
Show resolved Hide resolved

// Store the modified market param.
marketParamStore := k.getMarketParamStore(ctx)
b := k.cdc.MustMarshal(&updatedMarketParam)
Expand All @@ -61,12 +70,7 @@ func (k Keeper) ModifyMarketParam(
k.currencyPairIDCache.Remove(uint64(existingParam.Id))

// add the new cache entry
cp, err := slinky.MarketPairToCurrencyPair(updatedMarketParam.Pair)
if err == nil {
k.currencyPairIDCache.AddCurrencyPair(uint64(updatedMarketParam.Id), cp.String())
} else {
k.Logger(ctx).Error("failed to add currency pair to cache", "pair", updatedMarketParam.Pair)
}
k.currencyPairIDCache.AddCurrencyPair(uint64(updatedMarketParam.Id), cp.String())
}

// Generate indexer event.
Expand Down
39 changes: 28 additions & 11 deletions protocol/x/prices/keeper/market_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/metrics"
"github.com/dydxprotocol/v4-chain/protocol/lib/slinky"
marketmapkeeper "github.com/skip-mev/slinky/x/marketmap/keeper"

errorsmod "cosmossdk.io/errors"

Expand Down Expand Up @@ -76,18 +77,19 @@ func TestModifyMarketParamUpdatesCache(t *testing.T) {
require.True(t, found)
require.Equal(t, uint64(id), cpID)

// modify the market param
newParam, err := keeper.ModifyMarketParam(
// create new ticker in MarketMap for newParam
// (new ticker must exist in MarketMap before MarketParam.Pair can be updated)
newParam := oldParam
newParam.Pair = "bar-foo"
keepertest.CreateMarketsInMarketMapFromParams(
t,
ctx,
types.MarketParam{
Id: id,
Pair: "bar-foo",
MinExchanges: uint32(2),
Exponent: -8,
MinPriceChangePpm: uint32(50),
ExchangeConfigJson: `{"id":"1"}`,
},
keeper.MarketMapKeeper.(*marketmapkeeper.Keeper),
[]types.MarketParam{newParam},
)

// modify the market param
newParam, err = keeper.ModifyMarketParam(ctx, newParam)
require.NoError(t, err)

// check that the existing entry does not exist
Expand All @@ -104,6 +106,10 @@ func TestModifyMarketParamUpdatesCache(t *testing.T) {

func TestModifyMarketParam_Errors(t *testing.T) {
validExchangeConfigJson := `{"exchanges":[{"exchangeName":"Binance","ticker":"BTCUSDT"}]}`
invalidUpdatePair := "nil-nil"
invalidUpdateCurrencyPair, err := slinky.MarketPairToCurrencyPair(invalidUpdatePair)
require.NoError(t, err)

tests := map[string]struct {
// Setup
targetId uint32
Expand Down Expand Up @@ -166,7 +172,7 @@ func TestModifyMarketParam_Errors(t *testing.T) {
"",
).Error(),
},
"Updating pair fails": {
"Updating pair fails due to pair already existing": {
targetId: 0,
pair: "1-1",
minExchanges: uint32(1),
Expand All @@ -177,6 +183,17 @@ func TestModifyMarketParam_Errors(t *testing.T) {
"1-1",
).Error(),
},
"Updating pair fails due to no ticker in MarketMap": {
targetId: 0,
pair: invalidUpdatePair,
minExchanges: uint32(1),
minPriceChangePpm: uint32(50),
exchangeConfigJson: validExchangeConfigJson,
expectedErr: errorsmod.Wrapf(
types.ErrTickerNotFoundInMarketMap,
invalidUpdateCurrencyPair.String(),
).Error(),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
Expand Down
Loading