Skip to content

Commit

Permalink
[TRA-530] When changing market pair name, validate that new pair exis…
Browse files Browse the repository at this point in the history
…ts in the market map (#2151)
  • Loading branch information
hwray authored Sep 12, 2024
1 parent 10189d6 commit 9fd093a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
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)
if err != nil {
return types.MarketParam{}, errorsmod.Wrapf(types.ErrMarketPairConversionFailed, updatedMarketParam.Pair)
}
if _, err := k.MarketMapKeeper.GetMarket(ctx, cp.String()); err != nil {
return types.MarketParam{}, errorsmod.Wrapf(types.ErrTickerNotFoundInMarketMap, cp.String())
}

// 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

0 comments on commit 9fd093a

Please sign in to comment.