Skip to content

Commit

Permalink
[TRA-531] When market pair changes, disable old market and enable new…
Browse files Browse the repository at this point in the history
… market (#2276)
  • Loading branch information
chenyaoy authored Sep 17, 2024
1 parent 5f97bdd commit 7faee36
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
27 changes: 20 additions & 7 deletions protocol/x/prices/keeper/market_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,39 @@ func (k Keeper) ModifyMarketParam(

// if the market pair has been changed, we need to update the in-memory market pair cache
if existingParam.Pair != updatedMarketParam.Pair {
// remove the old cache entry and disable the old market
oldCurrencyPair, err := slinky.MarketPairToCurrencyPair(existingParam.Pair)
if err == nil {
k.RemoveCurrencyPairFromStore(ctx, oldCurrencyPair)
} else {
if err != nil {
return types.MarketParam{}, errorsmod.Wrap(
types.ErrMarketPairConversionFailed,
existingParam.Pair,
)
}

// add the new cache entry
k.RemoveCurrencyPairFromStore(ctx, oldCurrencyPair)
if err = k.MarketMapKeeper.DisableMarket(ctx, oldCurrencyPair.String()); err != nil {
return types.MarketParam{}, errorsmod.Wrap(
types.ErrMarketCouldNotBeDisabled,
existingParam.Pair,
)
}

// add the new cache entry and enable the new market
newCurrencyPair, err := slinky.MarketPairToCurrencyPair(updatedMarketParam.Pair)
if err == nil {
k.AddCurrencyPairIDToStore(ctx, updatedMarketParam.Id, newCurrencyPair)
} else {
if err != nil {
return types.MarketParam{}, errorsmod.Wrap(
types.ErrMarketPairConversionFailed,
updatedMarketParam.Pair,
)
}

k.AddCurrencyPairIDToStore(ctx, updatedMarketParam.Id, newCurrencyPair)
if err = k.MarketMapKeeper.EnableMarket(ctx, newCurrencyPair.String()); err != nil {
return types.MarketParam{}, errorsmod.Wrap(
types.ErrMarketCouldNotBeEnabled,
existingParam.Pair,
)
}
}

// Generate indexer event.
Expand Down
63 changes: 62 additions & 1 deletion protocol/x/prices/keeper/market_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func TestModifyMarketParamUpdatesCache(t *testing.T) {
cp, err := slinky.MarketPairToCurrencyPair(mp.Pair)
require.NoError(t, err)

// check that the existing entry exists
cpID, found := keeper.GetIDForCurrencyPair(ctx, cp)
require.True(t, found)
require.Equal(t, uint64(id), cpID)
Expand Down Expand Up @@ -104,6 +103,68 @@ func TestModifyMarketParamUpdatesCache(t *testing.T) {
require.Equal(t, uint64(id), cpID)
}

func TestModifyMarketParamUpdatesMarketmap(t *testing.T) {
ctx, keeper, _, _, mockTimeProvider, _, marketMapKeeper := keepertest.PricesKeepers(t)
mockTimeProvider.On("Now").Return(constants.TimeT)
ctx = ctx.WithTxBytes(constants.TestTxBytes)

id := uint32(1)
oldParam := types.MarketParam{
Id: id,
Pair: "foo-bar",
MinExchanges: uint32(2),
Exponent: -8,
MinPriceChangePpm: uint32(50),
ExchangeConfigJson: `{"id":"1"}`,
}
mp, err := keepertest.CreateTestMarket(t, ctx, keeper, oldParam, types.MarketPrice{
Id: id,
Exponent: -8,
Price: 1,
})
require.NoError(t, err)

// check that the market is enabled for the existing pair
oldCp, err := slinky.MarketPairToCurrencyPair(mp.Pair)
require.NoError(t, err)

oldMarket, err := marketMapKeeper.GetMarket(ctx, oldCp.String())
require.NoError(t, err)
require.True(t, oldMarket.Ticker.Enabled)

// 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"
newCp, err := slinky.MarketPairToCurrencyPair(newParam.Pair)
require.NoError(t, err)
keepertest.CreateMarketsInMarketMapFromParams(
t,
ctx,
keeper.MarketMapKeeper.(*marketmapkeeper.Keeper),
[]types.MarketParam{newParam},
)

// check that the new market is disabled to start
newMarket, err := marketMapKeeper.GetMarket(ctx, newCp.String())
require.NoError(t, err)
require.False(t, newMarket.Ticker.Enabled)

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

// check that old market is disabled
oldMarket, err = marketMapKeeper.GetMarket(ctx, oldCp.String())
require.NoError(t, err)
require.False(t, oldMarket.Ticker.Enabled)

// check that the new market is enabled
newMarket, err = marketMapKeeper.GetMarket(ctx, newCp.String())
require.NoError(t, err)
require.True(t, newMarket.Ticker.Enabled)
}

func TestModifyMarketParam_Errors(t *testing.T) {
validExchangeConfigJson := `{"exchanges":[{"exchangeName":"Binance","ticker":"BTCUSDT"}]}`
invalidUpdatePair := "nil-nil"
Expand Down
2 changes: 2 additions & 0 deletions protocol/x/prices/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
"Market pair conversion to currency pair failed",
)
ErrTickerNotFoundInMarketMap = errorsmod.Register(ModuleName, 207, "Ticker not found in market map")
ErrMarketCouldNotBeDisabled = errorsmod.Register(ModuleName, 208, "Market could not be disabled")
ErrMarketCouldNotBeEnabled = errorsmod.Register(ModuleName, 209, "Market could not be enabled")

// 300 - 399: Price related errors.
ErrIndexPriceNotAvailable = errorsmod.Register(ModuleName, 300, "Index price is not available")
Expand Down
1 change: 1 addition & 0 deletions protocol/x/prices/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ type MarketMapKeeper interface {
GetAllMarkets(ctx sdk.Context) (map[string]marketmaptypes.Market, error)
GetMarket(ctx sdk.Context, tickerStr string) (marketmaptypes.Market, error)
EnableMarket(ctx sdk.Context, tickerStr string) error
DisableMarket(ctx sdk.Context, tickerStr string) error
}

0 comments on commit 7faee36

Please sign in to comment.