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-508] Require that market exists in market map when creating new oracle market #1960

Merged
merged 26 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
4 changes: 2 additions & 2 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,7 @@ func New(
feegrant.ModuleName,
consensusparamtypes.ModuleName,
icatypes.ModuleName,
marketmapmoduletypes.ModuleName, // must be before prices
pricesmoduletypes.ModuleName,
assetsmoduletypes.ModuleName,
blocktimemoduletypes.ModuleName,
Expand All @@ -1393,7 +1394,6 @@ func New(
listingmoduletypes.ModuleName,
revsharemoduletypes.ModuleName,
accountplusmoduletypes.ModuleName,
marketmapmoduletypes.ModuleName,
authz.ModuleName,
)

Expand All @@ -1419,6 +1419,7 @@ func New(
feegrant.ModuleName,
consensusparamtypes.ModuleName,
icatypes.ModuleName,
marketmapmoduletypes.ModuleName,
pricesmoduletypes.ModuleName,
assetsmoduletypes.ModuleName,
blocktimemoduletypes.ModuleName,
Expand All @@ -1437,7 +1438,6 @@ func New(
listingmoduletypes.ModuleName,
revsharemoduletypes.ModuleName,
accountplusmoduletypes.ModuleName,
marketmapmoduletypes.ModuleName,
authz.ModuleName,

// Auth must be migrated after staking.
Expand Down
4 changes: 2 additions & 2 deletions protocol/app/prepare/prepare_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func TestPrepareProposalHandler(t *testing.T) {
mockClobKeeper.On("GetOperations", mock.Anything, mock.Anything).
Return(tc.clobResp)

ctx, _, _, _, _, _ := keepertest.PricesKeepers(t)
ctx, _, _, _, _, _, _ := keepertest.PricesKeepers(t)

handler := prepare.PrepareProposalHandler(
mockTxConfig,
Expand Down Expand Up @@ -440,7 +440,7 @@ func TestPrepareProposalHandler_OtherTxs(t *testing.T) {
mockBridgeKeeper.On("GetAcknowledgeBridges", mock.Anything, mock.Anything).
Return(constants.MsgAcknowledgeBridges_Ids0_1_Height0)

ctx, _, _, _, _, _ := keepertest.PricesKeepers(t)
ctx, _, _, _, _, _, _ := keepertest.PricesKeepers(t)

handler := prepare.PrepareProposalHandler(
encodingCfg.TxConfig,
Expand Down
2 changes: 1 addition & 1 deletion protocol/app/process/full_node_process_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestFullNodeProcessProposalHandler(t *testing.T) {
// Setup.
_, bridgeKeeper, _, _, _, _, _ := keepertest.BridgeKeepers(t)

ctx, pricesKeeper, _, indexPriceCache, mockTimeProvider, _ := keepertest.PricesKeepers(t)
ctx, pricesKeeper, _, indexPriceCache, mockTimeProvider, _, _ := keepertest.PricesKeepers(t)
mockTimeProvider.On("Now").Return(constants.TimeT)
keepertest.CreateTestMarkets(t, ctx, pricesKeeper)
indexPriceCache.UpdatePrices(constants.AtTimeTSingleExchangePriceUpdate)
Expand Down
6 changes: 3 additions & 3 deletions protocol/app/process/market_prices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestDecodeUpdateMarketPricesTx(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx, k, _, _, _, _ := keepertest.PricesKeepers(t)
ctx, k, _, _, _, _, _ := keepertest.PricesKeepers(t)
pricesTxDecoder := process.NewDefaultUpdateMarketPriceTxDecoder(k, encodingCfg.TxConfig.TxDecoder())
umpt, err := pricesTxDecoder.DecodeUpdateMarketPricesTx(ctx, [][]byte{tc.txBytes})
if tc.expectedErr != nil {
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestUpdateMarketPricesTx_Validate(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Setup.
ctx, k, _, indexPriceCache, mockTimeProvider, _ := keepertest.PricesKeepers(t)
ctx, k, _, indexPriceCache, mockTimeProvider, _, _ := keepertest.PricesKeepers(t)
mockTimeProvider.On("Now").Return(constants.TimeT)
keepertest.CreateTestMarkets(t, ctx, k)
indexPriceCache.UpdatePrices(tc.indexPrices)
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestUpdateMarketPricesTx_GetMsg(t *testing.T) {
t.Run(name, func(t *testing.T) {
var msg sdk.Msg
if tc.txBytes != nil {
ctx, k, _, _, _, _ := keepertest.PricesKeepers(t)
ctx, k, _, _, _, _, _ := keepertest.PricesKeepers(t)

// Decode.
pricesTxDecoder := process.NewDefaultUpdateMarketPriceTxDecoder(k, constants.TestEncodingCfg.TxConfig.TxDecoder())
Expand Down
2 changes: 1 addition & 1 deletion protocol/app/process/process_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestProcessProposalHandler_Error(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Setup.
ctx, pricesKeeper, _, indexPriceCache, mockTimeProvider, _ := keepertest.PricesKeepers(t)
ctx, pricesKeeper, _, indexPriceCache, mockTimeProvider, _, _ := keepertest.PricesKeepers(t)
mockTimeProvider.On("Now").Return(constants.TimeT)
keepertest.CreateTestMarkets(t, ctx, pricesKeeper)
indexPriceCache.UpdatePrices(constants.AtTimeTSingleExchangePriceUpdate)
Expand Down
8 changes: 4 additions & 4 deletions protocol/app/process/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestDecodeProcessProposalTxs_Error(t *testing.T) {
t.Run(name, func(t *testing.T) {
// Setup.
_, bridgeKeeper, _, _, _, _, _ := keepertest.BridgeKeepers(t)
ctx, pricesKeeper, _, _, _, _ := keepertest.PricesKeepers(t)
ctx, pricesKeeper, _, _, _, _, _ := keepertest.PricesKeepers(t)

// Run.
_, err := process.DecodeProcessProposalTxs(
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestDecodeProcessProposalTxs_Valid(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Setup.
ctx, pricesKeeper, _, _, _, _ := keepertest.PricesKeepers(t)
ctx, pricesKeeper, _, _, _, _, _ := keepertest.PricesKeepers(t)
_, bridgeKeeper, _, _, _, _, _ := keepertest.BridgeKeepers(t)

// Run.
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestProcessProposalTxs_Validate_Error(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Setup.
ctx, pricesKeeper, _, indexPriceCache, mockTimeProvider, _ := keepertest.PricesKeepers(t)
ctx, pricesKeeper, _, indexPriceCache, mockTimeProvider, _, _ := keepertest.PricesKeepers(t)
mockTimeProvider.On("Now").Return(constants.TimeT)
keepertest.CreateTestMarkets(t, ctx, pricesKeeper)
indexPriceCache.UpdatePrices(constants.AtTimeTSingleExchangePriceUpdate)
Expand Down Expand Up @@ -425,7 +425,7 @@ func TestProcessProposalTxs_Validate_Valid(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Setup.
ctx, pricesKeeper, _, indexPriceCache, mockTimeProvider, _ := keepertest.PricesKeepers(t)
ctx, pricesKeeper, _, indexPriceCache, mockTimeProvider, _, _ := keepertest.PricesKeepers(t)
mockTimeProvider.On("Now").Return(constants.TimeT)
keepertest.CreateTestMarkets(t, ctx, pricesKeeper)
indexPriceCache.UpdatePrices(constants.AtTimeTSingleExchangePriceUpdate)
Expand Down
8 changes: 4 additions & 4 deletions protocol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/cometbft/cometbft v0.38.9
github.com/cometbft/cometbft-db v0.10.0 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/cosmos-sdk v0.50.7
github.com/cosmos/cosmos-sdk v0.50.8
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogoproto v1.5.0
github.com/go-playground/validator/v10 v10.14.0
Expand Down Expand Up @@ -67,7 +67,7 @@ require (
github.com/pelletier/go-toml v1.9.5
github.com/rs/zerolog v1.32.0
github.com/shopspring/decimal v1.3.1
github.com/skip-mev/slinky v1.0.4-0.20240715165738-8218f83b6eb1
github.com/skip-mev/slinky v1.0.5-0.20240723200102-e4ba512cc7cc
github.com/spf13/viper v1.19.0
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d
go.uber.org/zap v1.27.0
Expand Down Expand Up @@ -423,15 +423,15 @@ require (
go.uber.org/ratelimit v0.2.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20240314144324-c7f7c6466f7f // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.22.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/api v0.171.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions protocol/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1754,8 +1754,8 @@ github.com/sivchari/tenv v1.7.1 h1:PSpuD4bu6fSmtWMxSGWcvqUUgIn7k3yOJhOIzVWn8Ak=
github.com/sivchari/tenv v1.7.1/go.mod h1:64yStXKSOxDfX47NlhVwND4dHwfZDdbp2Lyl018Icvg=
github.com/skip-mev/chaintestutil v0.0.0-20240116134208-3e49bf514803 h1:VRRVYN3wsOIOqVT3e3nDh3vyUl6RvF9QwdK4BvgPP9c=
github.com/skip-mev/chaintestutil v0.0.0-20240116134208-3e49bf514803/go.mod h1:LF2koCTmygQnz11yjSfHvNP8axdyZ2lTEw0EwI+dnno=
github.com/skip-mev/slinky v1.0.4-0.20240715165738-8218f83b6eb1 h1:eIWyyjfpFdgodF/v9C7onUUXQxSSyDOAGKFcgRAVgMo=
github.com/skip-mev/slinky v1.0.4-0.20240715165738-8218f83b6eb1/go.mod h1:+iC+HmAIqJHqeka5f96sI0bqsiXfcAjZ7sQk7nF78co=
github.com/skip-mev/slinky v1.0.5-0.20240723200102-e4ba512cc7cc h1:WeaCDvlxK+cWjlFza3pyJVAF0kV/IVS5LqZbPcVbMYY=
github.com/skip-mev/slinky v1.0.5-0.20240723200102-e4ba512cc7cc/go.mod h1:+PP4u5CivZforSB2r/EwhQRuZZwqdcoRFA0J3NsZNDA=
github.com/sonatard/noctx v0.0.2 h1:L7Dz4De2zDQhW8S0t+KUjY0MAQJd6SgVwhzNIc4ok00=
github.com/sonatard/noctx v0.0.2/go.mod h1:kzFz+CzWSjQ2OzIm46uJZoXuBpa2+0y3T36U18dWqIo=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
Expand Down Expand Up @@ -2063,8 +2063,8 @@ golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -2426,8 +2426,8 @@ golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
37 changes: 37 additions & 0 deletions protocol/mocks/OracleClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions protocol/testing/e2e/gov/add_new_market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"time"

"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/lib/slinky"
marketmaptypes "github.com/skip-mev/slinky/x/marketmap/types"

"github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -14,6 +16,7 @@ import (
clobtest "github.com/dydxprotocol/v4-chain/protocol/testutil/clob"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/dydxprotocol/v4-chain/protocol/testutil/encoding"
keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper"
perptest "github.com/dydxprotocol/v4-chain/protocol/testutil/perpetuals"
pricestest "github.com/dydxprotocol/v4-chain/protocol/testutil/prices"
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
Expand Down Expand Up @@ -51,6 +54,7 @@ func TestAddNewMarketProposal(t *testing.T) {
testMarketParam := pricestest.GenerateMarketParamPrice(
pricestest.WithId(TestMarketId),
)

testClobPair := clobtest.GenerateClobPair(
clobtest.WithId(TestMarketId),
clobtest.WithPerpetualId(TestMarketId),
Expand Down Expand Up @@ -298,11 +302,26 @@ func TestAddNewMarketProposal(t *testing.T) {
genesisState.Params.VotingPeriod = &testapp.TestVotingPeriod
},
)
testapp.UpdateGenesisDocWithAppStateForModule(
&genesis,
func(genesisState *marketmaptypes.GenesisState) {
// Add test market to market map genesis
marketMap := keepertest.ConstructMarketMapFromParams(t, []pricestypes.MarketParam{testMarketParam.Param})
for ticker, market := range marketMap.Markets {
market.Ticker.Enabled = false
genesisState.MarketMap.Markets[ticker] = market
}
},
)
genesis.GenesisTime = GenesisTime
return genesis
}).Build()
ctx := tApp.InitChain()

currencyPair, _ := slinky.MarketPairToCurrencyPair(testMarketParam.Param.Pair)
market, _ := tApp.App.MarketMapKeeper.GetMarket(ctx, currencyPair.String())
chenyaoy marked this conversation as resolved.
Show resolved Hide resolved
require.False(t, market.Ticker.Enabled)

initMarketParams := tApp.App.PricesKeeper.GetAllMarketParams(ctx)
initPerpetuals := tApp.App.PerpetualsKeeper.GetAllPerpetuals(ctx)
initClobPairs := tApp.App.ClobKeeper.GetAllClobPairs(ctx)
Expand Down Expand Up @@ -335,6 +354,9 @@ func TestAddNewMarketProposal(t *testing.T) {
require.Equal(t, initMarketParams, tApp.App.PricesKeeper.GetAllMarketParams(ctx))
require.Equal(t, initPerpetuals, tApp.App.PerpetualsKeeper.GetAllPerpetuals(ctx))
require.Equal(t, initClobPairs, tApp.App.ClobKeeper.GetAllClobPairs(ctx))
// Check that market is still disabled in market map.
market, _ := tApp.App.MarketMapKeeper.GetMarket(ctx, currencyPair.String())
require.False(t, market.Ticker.Enabled)
case govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED:
// Proposal passed and successfully executed, check states are updated.
// Check market
Expand Down Expand Up @@ -375,6 +397,10 @@ func TestAddNewMarketProposal(t *testing.T) {
// Check that clob pair is updated.
require.Equal(t, msgUpdateClobPairToActive.ClobPair, clobPair)

// Check that market is enabled in market map.
market, _ := tApp.App.MarketMapKeeper.GetMarket(ctx, currencyPair.String())
require.True(t, market.Ticker.Enabled)

// Advance to some blocks after, and place an order on the market.
ctx = tApp.AdvanceToBlock(uint32(ctx.BlockHeight())+NumBlocksAfterTradingEnabled, testapp.AdvanceToBlockOptions{})
price, err := tApp.App.PricesKeeper.GetMarketPrice(ctx, testMarketParam.Param.Id)
Expand Down
31 changes: 31 additions & 0 deletions protocol/testing/e2e/gov/perpetuals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
slinkytypes "github.com/skip-mev/slinky/pkg/types"
marketmaptypes "github.com/skip-mev/slinky/x/marketmap/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -274,6 +276,35 @@ func TestUpdatePerpetualsParams(t *testing.T) {
genesisState.Params.VotingPeriod = &testapp.TestVotingPeriod
},
)
// Initialize marketmap module with genesis markets.
testapp.UpdateGenesisDocWithAppStateForModule(
&genesis,
func(genesisState *marketmaptypes.GenesisState) {
markets := make(map[string]marketmaptypes.Market)
marketIds := append(tc.genesisMarketIds, TEST_PERPETUAL_PARAMS.MarketId)
for i := range marketIds {
ticker := marketmaptypes.Ticker{
CurrencyPair: slinkytypes.CurrencyPair{Base: fmt.Sprintf("%d", i), Quote: fmt.Sprintf("%d", i)},
Decimals: 8,
MinProviderCount: 3,
Enabled: true,
Metadata_JSON: "",
}
markets[fmt.Sprintf("%d/%d", i, i)] = marketmaptypes.Market{
Ticker: ticker,
ProviderConfigs: []marketmaptypes.ProviderConfig{
{Name: "binance_ws", OffChainTicker: "test"},
{Name: "bybit_ws", OffChainTicker: "test"},
{Name: "coinbase_ws", OffChainTicker: "test"},
},
}
}
marketMap := marketmaptypes.MarketMap{
Markets: markets,
}
genesisState.MarketMap = marketMap
},
)
// Initialize prices module with genesis markets.
testapp.UpdateGenesisDocWithAppStateForModule(
&genesis,
Expand Down
Loading
Loading