diff --git a/CHANGELOG.md b/CHANGELOG.md index b2034b7cb..044ca814c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#1058](https://github.com/NibiruChain/nibiru/pull/1058) - feature: use collections external lib * [#1074](https://github.com/NibiruChain/nibiru/pull/1074) - feat(vpool): Add gov proposal for editing the vpool config without changing the reserves. * [#1082](https://github.com/NibiruChain/nibiru/pull/1082) - feat(vpool): Add gov proposal for editing the sswap invariant of a vpool.. +* [#1092](https://github.com/NibiruChain/nibiru/pull/1092) - refactor(dex)!: revive dex module using intermediate test app ### Improvements diff --git a/app/app.go b/app/app.go index 0b962c16a..d9068485e 100644 --- a/app/app.go +++ b/app/app.go @@ -104,6 +104,9 @@ import ( "github.com/CosmWasm/wasmd/x/wasm" "github.com/NibiruChain/nibiru/x/common" + "github.com/NibiruChain/nibiru/x/dex" + dexkeeper "github.com/NibiruChain/nibiru/x/dex/keeper" + dextypes "github.com/NibiruChain/nibiru/x/dex/types" "github.com/NibiruChain/nibiru/x/epochs" epochskeeper "github.com/NibiruChain/nibiru/x/epochs/keeper" epochstypes "github.com/NibiruChain/nibiru/x/epochs/types" @@ -168,6 +171,7 @@ var ( ibc.AppModuleBasic{}, ibctransfer.AppModuleBasic{}, // native x/ + dex.AppModuleBasic{}, pricefeed.AppModuleBasic{}, epochs.AppModuleBasic{}, perp.AppModuleBasic{}, @@ -184,6 +188,7 @@ var ( stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking}, stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking}, govtypes.ModuleName: {authtypes.Burner}, + dextypes.ModuleName: {authtypes.Minter, authtypes.Burner}, ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner}, perptypes.ModuleName: {authtypes.Minter, authtypes.Burner}, perptypes.VaultModuleAccount: {}, @@ -223,8 +228,8 @@ type NibiruApp struct { // accountKeeper encodes/decodes accounts using the go-amino (binary) encoding/decoding library accountKeeper authkeeper.AccountKeeper - // bankKeeper defines a module interface that facilitates the transfer of coins between accounts - bankKeeper bankkeeper.Keeper + // BankKeeper defines a module interface that facilitates the transfer of coins between accounts + BankKeeper bankkeeper.Keeper capabilityKeeper *capabilitykeeper.Keeper stakingKeeper stakingkeeper.Keeper slashingKeeper slashingkeeper.Keeper @@ -262,6 +267,7 @@ type NibiruApp struct { perpKeeper perpkeeper.Keeper pricefeedKeeper pricefeedkeeper.Keeper vpoolKeeper vpoolkeeper.Keeper + DexKeeper dexkeeper.Keeper // WASM keepers wasmKeeper wasm.Keeper @@ -337,6 +343,7 @@ func NewNibiruApp( ibchost.StoreKey, ibctransfertypes.StoreKey, // nibiru x/ keys + dextypes.StoreKey, pricefeedtypes.StoreKey, epochstypes.StoreKey, perptypes.StoreKey, @@ -383,25 +390,25 @@ func NewNibiruApp( app.accountKeeper = authkeeper.NewAccountKeeper( appCodec, keys[authtypes.StoreKey], app.GetSubspace(authtypes.ModuleName), authtypes.ProtoBaseAccount, maccPerms, ) - app.bankKeeper = bankkeeper.NewBaseKeeper( + app.BankKeeper = bankkeeper.NewBaseKeeper( appCodec, keys[banktypes.StoreKey], app.accountKeeper, app.GetSubspace(banktypes.ModuleName), app.ModuleAccountAddrs(), ) stakingKeeper := stakingkeeper.NewKeeper( - appCodec, keys[stakingtypes.StoreKey], app.accountKeeper, app.bankKeeper, app.GetSubspace(stakingtypes.ModuleName), + appCodec, keys[stakingtypes.StoreKey], app.accountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName), ) app.mintKeeper = mintkeeper.NewKeeper( appCodec, keys[minttypes.StoreKey], app.GetSubspace(minttypes.ModuleName), &stakingKeeper, - app.accountKeeper, app.bankKeeper, authtypes.FeeCollectorName, + app.accountKeeper, app.BankKeeper, authtypes.FeeCollectorName, ) app.distrKeeper = distrkeeper.NewKeeper( - appCodec, keys[distrtypes.StoreKey], app.GetSubspace(distrtypes.ModuleName), app.accountKeeper, app.bankKeeper, + appCodec, keys[distrtypes.StoreKey], app.GetSubspace(distrtypes.ModuleName), app.accountKeeper, app.BankKeeper, &stakingKeeper, authtypes.FeeCollectorName, app.ModuleAccountAddrs(), ) app.slashingKeeper = slashingkeeper.NewKeeper( appCodec, keys[slashingtypes.StoreKey], &stakingKeeper, app.GetSubspace(slashingtypes.ModuleName), ) app.crisisKeeper = crisiskeeper.NewKeeper( - app.GetSubspace(crisistypes.ModuleName), invCheckPeriod, app.bankKeeper, authtypes.FeeCollectorName, + app.GetSubspace(crisistypes.ModuleName), invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName, ) app.feeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, keys[feegrant.StoreKey], app.accountKeeper) @@ -424,6 +431,10 @@ func NewNibiruApp( // ---------------------------------- Nibiru Chain x/ keepers + app.DexKeeper = dexkeeper.NewKeeper( + appCodec, keys[dextypes.StoreKey], app.GetSubspace(dextypes.ModuleName), + app.accountKeeper, app.BankKeeper, app.distrKeeper) + app.pricefeedKeeper = pricefeedkeeper.NewKeeper( appCodec, keys[pricefeedtypes.StoreKey], memKeys[pricefeedtypes.MemStoreKey], app.GetSubspace(pricefeedtypes.ModuleName), @@ -442,7 +453,7 @@ func NewNibiruApp( app.perpKeeper = perpkeeper.NewKeeper( appCodec, keys[perptypes.StoreKey], app.GetSubspace(perptypes.ModuleName), - app.accountKeeper, app.bankKeeper, app.pricefeedKeeper, app.vpoolKeeper, app.epochsKeeper, + app.accountKeeper, app.BankKeeper, app.pricefeedKeeper, app.vpoolKeeper, app.epochsKeeper, ) app.epochsKeeper.SetHooks( @@ -476,7 +487,7 @@ func NewNibiruApp( keys[wasm.StoreKey], app.GetSubspace(wasm.ModuleName), app.accountKeeper, - app.bankKeeper, + app.BankKeeper, app.stakingKeeper, app.distrKeeper, app.ibcKeeper.ChannelKeeper, @@ -511,7 +522,7 @@ func NewNibiruApp( /* ibctransfertypes.ChannelKeeper */ app.ibcKeeper.ChannelKeeper, /* ibctransfertypes.PortKeeper */ &app.ibcKeeper.PortKeeper, app.accountKeeper, - app.bankKeeper, + app.BankKeeper, app.scopedTransferKeeper, ) transferModule := ibctransfer.NewAppModule(app.transferKeeper) @@ -539,7 +550,7 @@ func NewNibiruApp( app.govKeeper = govkeeper.NewKeeper( appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), - app.accountKeeper, app.bankKeeper, &app.stakingKeeper, govRouter, + app.accountKeeper, app.BankKeeper, &app.stakingKeeper, govRouter, ) // -------------------------- Module Options -------------------------- @@ -551,17 +562,19 @@ func NewNibiruApp( var skipGenesisInvariants = cast.ToBool( appOpts.Get(crisis.FlagSkipGenesisInvariants)) + dexModule := dex.NewAppModule( + appCodec, app.DexKeeper, app.accountKeeper, app.BankKeeper) pricefeedModule := pricefeed.NewAppModule( - appCodec, app.pricefeedKeeper, app.accountKeeper, app.bankKeeper) + appCodec, app.pricefeedKeeper, app.accountKeeper, app.BankKeeper) epochsModule := epochs.NewAppModule(appCodec, app.epochsKeeper) perpModule := perp.NewAppModule( - appCodec, app.perpKeeper, app.accountKeeper, app.bankKeeper, + appCodec, app.perpKeeper, app.accountKeeper, app.BankKeeper, app.pricefeedKeeper, ) vpoolModule := vpool.NewAppModule( appCodec, app.vpoolKeeper, app.pricefeedKeeper, ) - utilModule := util.NewAppModule(app.bankKeeper) + utilModule := util.NewAppModule(app.BankKeeper) // NOTE: Any module instantiated in the module manager that is later modified // must be passed by reference here. @@ -571,21 +584,22 @@ func NewNibiruApp( encodingConfig.TxConfig, ), auth.NewAppModule(appCodec, app.accountKeeper, authsims.RandomGenesisAccounts), - vesting.NewAppModule(app.accountKeeper, app.bankKeeper), - bank.NewAppModule(appCodec, app.bankKeeper, app.accountKeeper), + vesting.NewAppModule(app.accountKeeper, app.BankKeeper), + bank.NewAppModule(appCodec, app.BankKeeper, app.accountKeeper), capability.NewAppModule(appCodec, *app.capabilityKeeper), crisis.NewAppModule(&app.crisisKeeper, skipGenesisInvariants), - feegrantmodule.NewAppModule(appCodec, app.accountKeeper, app.bankKeeper, app.feeGrantKeeper, app.interfaceRegistry), - gov.NewAppModule(appCodec, app.govKeeper, app.accountKeeper, app.bankKeeper), + feegrantmodule.NewAppModule(appCodec, app.accountKeeper, app.BankKeeper, app.feeGrantKeeper, app.interfaceRegistry), + gov.NewAppModule(appCodec, app.govKeeper, app.accountKeeper, app.BankKeeper), mint.NewAppModule(appCodec, app.mintKeeper, app.accountKeeper), - slashing.NewAppModule(appCodec, app.slashingKeeper, app.accountKeeper, app.bankKeeper, app.stakingKeeper), - distr.NewAppModule(appCodec, app.distrKeeper, app.accountKeeper, app.bankKeeper, app.stakingKeeper), - staking.NewAppModule(appCodec, app.stakingKeeper, app.accountKeeper, app.bankKeeper), + slashing.NewAppModule(appCodec, app.slashingKeeper, app.accountKeeper, app.BankKeeper, app.stakingKeeper), + distr.NewAppModule(appCodec, app.distrKeeper, app.accountKeeper, app.BankKeeper, app.stakingKeeper), + staking.NewAppModule(appCodec, app.stakingKeeper, app.accountKeeper, app.BankKeeper), upgrade.NewAppModule(app.upgradeKeeper), params.NewAppModule(app.paramsKeeper), - authzmodule.NewAppModule(appCodec, app.authzKeeper, app.accountKeeper, app.bankKeeper, app.interfaceRegistry), + authzmodule.NewAppModule(appCodec, app.authzKeeper, app.accountKeeper, app.BankKeeper, app.interfaceRegistry), // native x/ + dexModule, pricefeedModule, epochsModule, vpoolModule, @@ -597,7 +611,7 @@ func NewNibiruApp( ibc.NewAppModule(app.ibcKeeper), transferModule, - wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper, app.accountKeeper, app.bankKeeper), + wasm.NewAppModule(appCodec, &app.wasmKeeper, app.stakingKeeper, app.accountKeeper, app.BankKeeper), ) // During begin block slashing happens after distr.BeginBlocker so that @@ -623,6 +637,7 @@ func NewNibiruApp( vestingtypes.ModuleName, stakingtypes.ModuleName, // native x/ + dextypes.ModuleName, pricefeedtypes.ModuleName, epochstypes.ModuleName, vpooltypes.ModuleName, @@ -652,6 +667,7 @@ func NewNibiruApp( vestingtypes.ModuleName, // native x/ epochstypes.ModuleName, + dextypes.ModuleName, pricefeedtypes.ModuleName, vpooltypes.ModuleName, perptypes.ModuleName, @@ -687,6 +703,7 @@ func NewNibiruApp( // native x/ pricefeedtypes.ModuleName, epochstypes.ModuleName, + dextypes.ModuleName, vpooltypes.ModuleName, perptypes.ModuleName, utiltypes.ModuleName, @@ -719,15 +736,15 @@ func NewNibiruApp( // transactions app.sm = module.NewSimulationManager( auth.NewAppModule(appCodec, app.accountKeeper, authsims.RandomGenesisAccounts), - bank.NewAppModule(appCodec, app.bankKeeper, app.accountKeeper), - feegrantmodule.NewAppModule(appCodec, app.accountKeeper, app.bankKeeper, app.feeGrantKeeper, app.interfaceRegistry), - gov.NewAppModule(appCodec, app.govKeeper, app.accountKeeper, app.bankKeeper), + bank.NewAppModule(appCodec, app.BankKeeper, app.accountKeeper), + feegrantmodule.NewAppModule(appCodec, app.accountKeeper, app.BankKeeper, app.feeGrantKeeper, app.interfaceRegistry), + gov.NewAppModule(appCodec, app.govKeeper, app.accountKeeper, app.BankKeeper), mint.NewAppModule(appCodec, app.mintKeeper, app.accountKeeper), - staking.NewAppModule(appCodec, app.stakingKeeper, app.accountKeeper, app.bankKeeper), - distr.NewAppModule(appCodec, app.distrKeeper, app.accountKeeper, app.bankKeeper, app.stakingKeeper), - slashing.NewAppModule(appCodec, app.slashingKeeper, app.accountKeeper, app.bankKeeper, app.stakingKeeper), + staking.NewAppModule(appCodec, app.stakingKeeper, app.accountKeeper, app.BankKeeper), + distr.NewAppModule(appCodec, app.distrKeeper, app.accountKeeper, app.BankKeeper, app.stakingKeeper), + slashing.NewAppModule(appCodec, app.slashingKeeper, app.accountKeeper, app.BankKeeper, app.stakingKeeper), params.NewAppModule(app.paramsKeeper), - authzmodule.NewAppModule(appCodec, app.authzKeeper, app.accountKeeper, app.bankKeeper, app.interfaceRegistry), + authzmodule.NewAppModule(appCodec, app.authzKeeper, app.accountKeeper, app.BankKeeper, app.interfaceRegistry), // native x/ pricefeedModule, epochsModule, @@ -751,7 +768,7 @@ func NewNibiruApp( anteHandler, err := NewAnteHandler(AnteHandlerOptions{ HandlerOptions: ante.HandlerOptions{ AccountKeeper: app.accountKeeper, - BankKeeper: app.bankKeeper, + BankKeeper: app.BankKeeper, FeegrantKeeper: app.feeGrantKeeper, SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), SigGasConsumer: ante.DefaultSigVerificationGasConsumer, @@ -983,14 +1000,15 @@ func initParamsKeeper( paramsKeeper.Subspace(slashingtypes.ModuleName) paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govtypes.ParamKeyTable()) paramsKeeper.Subspace(crisistypes.ModuleName) - // Native module params keepers + // Native params keepers + paramsKeeper.Subspace(dextypes.ModuleName) paramsKeeper.Subspace(pricefeedtypes.ModuleName) paramsKeeper.Subspace(epochstypes.ModuleName) + paramsKeeper.Subspace(perptypes.ModuleName) // ibc params keepers paramsKeeper.Subspace(ibctransfertypes.ModuleName) paramsKeeper.Subspace(ibchost.ModuleName) - paramsKeeper.Subspace(perptypes.ModuleName) - + // wasm params keepers paramsKeeper.Subspace(wasm.ModuleName) return paramsKeeper diff --git a/simapp/app.go b/simapp/app.go index 2caa2f294..2aaff54ef 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -1074,6 +1074,7 @@ func initParamsKeeper( // ibc params keepers paramsKeeper.Subspace(ibctransfertypes.ModuleName) paramsKeeper.Subspace(ibchost.ModuleName) + // wasm params keepers paramsKeeper.Subspace(wasm.ModuleName) return paramsKeeper diff --git a/x/dex/client/testutil/cli_test.go b/x/dex/client/testutil/cli_test.go index aa1cd61a6..2b678adfa 100644 --- a/x/dex/client/testutil/cli_test.go +++ b/x/dex/client/testutil/cli_test.go @@ -7,9 +7,8 @@ import ( "github.com/stretchr/testify/suite" "github.com/NibiruChain/nibiru/app" - "github.com/NibiruChain/nibiru/simapp" "github.com/NibiruChain/nibiru/x/common" - testutilcli "github.com/NibiruChain/nibiru/x/testutil/cli" + "github.com/NibiruChain/nibiru/x/testutil/testapp" ) func TestIntegrationTestSuite(t *testing.T) { @@ -25,14 +24,14 @@ func TestIntegrationTestSuite(t *testing.T) { } app.SetPrefixes(app.AccountAddressPrefix) - genesisState := simapp.NewTestGenesisStateFromDefault() + genesisState := testapp.NewTestGenesisStateFromDefault() genesisState = WhitelistGenesisAssets( genesisState, coinsFromGenesis, ) - cfg := testutilcli.BuildNetworkConfig(genesisState) + cfg := testapp.BuildNetworkConfig(genesisState) cfg.StartingTokens = sdk.NewCoins( sdk.NewInt64Coin(common.DenomNIBI, 2e12), // for pool creation fee and more for tx fees ) diff --git a/x/dex/client/testutil/test_helpers.go b/x/dex/client/testutil/test_helpers.go index a5ee8c001..c29456818 100644 --- a/x/dex/client/testutil/test_helpers.go +++ b/x/dex/client/testutil/test_helpers.go @@ -13,10 +13,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/NibiruChain/nibiru/app" - "github.com/NibiruChain/nibiru/simapp" "github.com/NibiruChain/nibiru/x/common" "github.com/NibiruChain/nibiru/x/dex/client/cli" "github.com/NibiruChain/nibiru/x/dex/types" + "github.com/NibiruChain/nibiru/x/testutil/testapp" ) // commonArgs is args for CLI test commands. @@ -140,8 +140,8 @@ func ExecMsgSwapAssets( return clitestutil.ExecTestCLICmd(clientCtx, cli.CmdSwapAssets(), args) } -// WhitelistGenesisAssets given a simapp.GenesisState includes the whitelisted assets into Dex Whitelisted assets. -func WhitelistGenesisAssets(state simapp.GenesisState, assets []string) simapp.GenesisState { +// WhitelistGenesisAssets given a testapp.GenesisState includes the whitelisted assets into Dex Whitelisted assets. +func WhitelistGenesisAssets(state testapp.GenesisState, assets []string) testapp.GenesisState { encConfig := app.MakeTestEncodingConfig() jsonState := state[types.ModuleName] diff --git a/x/dex/genesis_test.go b/x/dex/genesis_test.go index fd753ac35..b5a4ba7be 100644 --- a/x/dex/genesis_test.go +++ b/x/dex/genesis_test.go @@ -5,7 +5,7 @@ import ( "github.com/NibiruChain/nibiru/x/testutil" - "github.com/NibiruChain/nibiru/simapp" + "github.com/NibiruChain/nibiru/x/testutil/testapp" "github.com/stretchr/testify/require" @@ -18,7 +18,7 @@ func TestGenesis(t *testing.T) { Params: types.DefaultParams(), } - app, ctx := simapp.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) dex.InitGenesis(ctx, app.DexKeeper, genesisState) got := dex.ExportGenesis(ctx, app.DexKeeper) require.NotNil(t, got) diff --git a/x/dex/keeper/balances_test.go b/x/dex/keeper/balances_test.go index 61e4d4843..ba8e9b838 100644 --- a/x/dex/keeper/balances_test.go +++ b/x/dex/keeper/balances_test.go @@ -5,7 +5,7 @@ import ( "github.com/NibiruChain/nibiru/x/testutil" - simapp2 "github.com/NibiruChain/nibiru/simapp" + "github.com/NibiruChain/nibiru/x/testutil/testapp" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -54,7 +54,7 @@ func TestCheckBalances(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) // fund user account sender := testutil.AccAddress() diff --git a/x/dex/keeper/grpc_query_test.go b/x/dex/keeper/grpc_query_test.go index a0b5b6bb6..11a0aeeec 100644 --- a/x/dex/keeper/grpc_query_test.go +++ b/x/dex/keeper/grpc_query_test.go @@ -5,7 +5,7 @@ import ( "github.com/NibiruChain/nibiru/x/testutil" - simapp2 "github.com/NibiruChain/nibiru/simapp" + "github.com/NibiruChain/nibiru/x/testutil/testapp" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -19,7 +19,7 @@ import ( ) func TestParamsQuery(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) params := types.DefaultParams() app.DexKeeper.SetParams(ctx, params) @@ -91,7 +91,7 @@ func TestQueryPoolHappyPath(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetPool(ctx, tc.existingPool) queryServer := keeper.NewQuerier(app.DexKeeper) @@ -117,7 +117,7 @@ func TestQueryPoolFail(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) queryServer := keeper.NewQuerier(app.DexKeeper) resp, err := queryServer.Pool(sdk.WrapSDKContext(ctx), nil) require.Error(t, err) @@ -234,7 +234,7 @@ func TestQueryPools(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) for _, existingPool := range tc.existingPools { app.DexKeeper.SetPool(ctx, existingPool) } @@ -313,7 +313,7 @@ func TestQueryNumPools(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) sender := testutil.AccAddress() // need funds to create pools require.NoError(t, simapp.FundAccount( @@ -422,7 +422,7 @@ func TestQueryPoolParams(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetPool(ctx, tc.existingPool) queryServer := keeper.NewQuerier(app.DexKeeper) @@ -459,7 +459,7 @@ func TestQueryTotalShares(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetPool(ctx, tc.existingPool) @@ -532,7 +532,7 @@ func TestQuerySpotPrice(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetPool(ctx, tc.existingPool) @@ -594,7 +594,7 @@ func TestQueryEstimateSwapExactAmountIn(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetPool(ctx, tc.existingPool) queryServer := keeper.NewQuerier(app.DexKeeper) @@ -656,7 +656,7 @@ func TestQueryEstimateSwapExactAmountOut(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetPool(ctx, tc.existingPool) queryServer := keeper.NewQuerier(app.DexKeeper) @@ -724,7 +724,7 @@ func TestQueryEstimateJoinExactAmountIn(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetPool(ctx, tc.existingPool) queryServer := keeper.NewQuerier(app.DexKeeper) @@ -789,7 +789,7 @@ func TestQueryEstimateExitExactAmountIn(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetPool(ctx, tc.existingPool) queryServer := keeper.NewQuerier(app.DexKeeper) diff --git a/x/dex/keeper/keeper_test.go b/x/dex/keeper/keeper_test.go index 2a2e8a9b3..ad59654fa 100644 --- a/x/dex/keeper/keeper_test.go +++ b/x/dex/keeper/keeper_test.go @@ -5,7 +5,7 @@ import ( "github.com/NibiruChain/nibiru/x/testutil" - simapp2 "github.com/NibiruChain/nibiru/simapp" + "github.com/NibiruChain/nibiru/x/testutil/testapp" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -18,7 +18,7 @@ import ( ) func TestGetAndSetNextPoolNumber(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) // Write to store app.DexKeeper.SetNextPoolNumber(ctx, 150) @@ -30,7 +30,7 @@ func TestGetAndSetNextPoolNumber(t *testing.T) { } func TestGetNextPoolNumberAndIncrement(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) // Write a pool number app.DexKeeper.SetNextPoolNumber(ctx, 200) @@ -45,7 +45,7 @@ func TestGetNextPoolNumberAndIncrement(t *testing.T) { } func TestSetAndFetchPool(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) pool := types.Pool{ Id: 150, @@ -154,7 +154,7 @@ func TestFetchPoolFromPair(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetPool(ctx, types.Pool{ Id: 1, @@ -216,7 +216,7 @@ func TestFetchPoolFromPair(t *testing.T) { } func TestNewPool(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) poolCreationFeeCoin := sdk.NewInt64Coin(common.DenomNIBI, 1000*common.Precision) app.DexKeeper.SetParams(ctx, types.NewParams( @@ -287,7 +287,7 @@ func TestNewPool(t *testing.T) { } func TestNewPoolNotEnoughFunds(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) app.DexKeeper.SetParams(ctx, types.NewParams( /*startingPoolNumber=*/ 1, @@ -328,7 +328,7 @@ func TestNewPoolNotEnoughFunds(t *testing.T) { } func TestNewPoolTooLittleAssets(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) userAddr, err := sdk.AccAddressFromBech32(testutil.AccAddress().String()) require.NoError(t, err) @@ -349,7 +349,7 @@ func TestNewPoolTooLittleAssets(t *testing.T) { } func TestKeeperNewPoolNotWhitelistedAssets(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) userAddr, err := sdk.AccAddressFromBech32(testutil.AccAddress().String()) require.NoError(t, err) @@ -374,7 +374,7 @@ func TestKeeperNewPoolNotWhitelistedAssets(t *testing.T) { } func TestNewPoolTooManyAssets(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) userAddr, err := sdk.AccAddressFromBech32(testutil.AccAddress().String()) require.NoError(t, err) @@ -419,7 +419,7 @@ func TestNewPoolTooManyAssets(t *testing.T) { } func TestNewPoolDups(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) userAddr, err := sdk.AccAddressFromBech32(testutil.AccAddress().String()) require.NoError(t, err) @@ -574,7 +574,7 @@ func TestJoinPool(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) poolAddr := testutil.AccAddress() tc.initialPool.Address = poolAddr.String() @@ -703,7 +703,7 @@ func TestJoinPoolAllAssets(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) poolAddr := testutil.AccAddress() tc.initialPool.Address = poolAddr.String() @@ -815,7 +815,7 @@ func TestExitPool(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) poolAddr := testutil.AccAddress() tc.initialPool.Address = poolAddr.String() diff --git a/x/dex/keeper/liquidity_test.go b/x/dex/keeper/liquidity_test.go index fc876ea22..357855c48 100644 --- a/x/dex/keeper/liquidity_test.go +++ b/x/dex/keeper/liquidity_test.go @@ -3,14 +3,14 @@ package keeper_test import ( "testing" - "github.com/NibiruChain/nibiru/simapp" + "github.com/NibiruChain/nibiru/x/testutil/testapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" ) func TestGetSetDenomLiquidity(t *testing.T) { - app, ctx := simapp.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) // Write to store app.DexKeeper.SetDenomLiquidity(ctx, "nibi", sdk.NewInt(1000)) @@ -22,7 +22,7 @@ func TestGetSetDenomLiquidity(t *testing.T) { } func TestGetTotalLiquidity(t *testing.T) { - app, ctx := simapp.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) // Write to store app.DexKeeper.SetDenomLiquidity(ctx, "atom", sdk.NewInt(123)) @@ -40,7 +40,7 @@ func TestGetTotalLiquidity(t *testing.T) { } func TestSetTotalLiquidity(t *testing.T) { - app, ctx := simapp.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) // Write to store app.DexKeeper.SetTotalLiquidity(ctx, sdk.NewCoins( @@ -56,7 +56,7 @@ func TestSetTotalLiquidity(t *testing.T) { } func TestRecordTotalLiquidityIncrease(t *testing.T) { - app, ctx := simapp.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) // Write to store app.DexKeeper.SetTotalLiquidity(ctx, sdk.NewCoins( @@ -73,7 +73,7 @@ func TestRecordTotalLiquidityIncrease(t *testing.T) { } func TestRecordTotalLiquidityDecrease(t *testing.T) { - app, ctx := simapp.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) // Write to store app.DexKeeper.SetTotalLiquidity(ctx, sdk.NewCoins( diff --git a/x/dex/keeper/msg_server_test.go b/x/dex/keeper/msg_server_test.go index fbde21d54..d5849afad 100644 --- a/x/dex/keeper/msg_server_test.go +++ b/x/dex/keeper/msg_server_test.go @@ -6,7 +6,7 @@ import ( "github.com/NibiruChain/nibiru/x/testutil" - simapp2 "github.com/NibiruChain/nibiru/simapp" + "github.com/NibiruChain/nibiru/x/testutil/testapp" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -274,7 +274,7 @@ func TestCreatePool(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) msgServer := keeper.NewMsgServerImpl(app.DexKeeper) if tc.creatorAddr == nil { @@ -511,7 +511,7 @@ func TestMsgServerJoinPool(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) poolAddr := testutil.AccAddress() tc.initialPool.Address = poolAddr.String() @@ -713,7 +713,7 @@ func TestMsgServerExitPool(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) poolAddr := testutil.AccAddress() tc.initialPool.Address = poolAddr.String() @@ -1056,7 +1056,7 @@ func TestMsgServerSwapAssets(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) msgServer := keeper.NewMsgServerImpl(app.DexKeeper) // fund pool account diff --git a/x/dex/keeper/params_test.go b/x/dex/keeper/params_test.go index e37eb30cb..3f8924045 100644 --- a/x/dex/keeper/params_test.go +++ b/x/dex/keeper/params_test.go @@ -3,7 +3,7 @@ package keeper_test import ( "testing" - "github.com/NibiruChain/nibiru/simapp" + "github.com/NibiruChain/nibiru/x/testutil/testapp" "github.com/stretchr/testify/require" @@ -11,7 +11,7 @@ import ( ) func TestGetParams(t *testing.T) { - app, ctx := simapp.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) params := types.DefaultParams() app.DexKeeper.SetParams(ctx, params) diff --git a/x/dex/keeper/swap_test.go b/x/dex/keeper/swap_test.go index 76570487e..fd2180819 100644 --- a/x/dex/keeper/swap_test.go +++ b/x/dex/keeper/swap_test.go @@ -5,7 +5,7 @@ import ( "github.com/NibiruChain/nibiru/x/testutil" - simapp2 "github.com/NibiruChain/nibiru/simapp" + "github.com/NibiruChain/nibiru/x/testutil/testapp" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -179,7 +179,7 @@ func TestSwapExactAmountIn(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - app, ctx := simapp2.NewTestNibiruAppAndContext(true) + app, ctx := testapp.NewTestNibiruAppAndContext(true) // fund pool account poolAddr := testutil.AccAddress() diff --git a/x/testutil/testapp/config.go b/x/testutil/testapp/config.go new file mode 100644 index 000000000..9a9e3baeb --- /dev/null +++ b/x/testutil/testapp/config.go @@ -0,0 +1,52 @@ +package testapp + +import ( + "fmt" + "time" + + "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + servertypes "github.com/cosmos/cosmos-sdk/server/types" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + tmrand "github.com/tendermint/tendermint/libs/rand" + + "github.com/NibiruChain/nibiru/app" + "github.com/NibiruChain/nibiru/x/common" + "github.com/NibiruChain/nibiru/x/testutil/cli" +) + +// BuildNetworkConfig returns a configuration for a local in-testing network +func BuildNetworkConfig(appGenesis GenesisState) cli.Config { + encCfg := app.MakeTestEncodingConfig() + + return cli.Config{ + Codec: encCfg.Marshaler, + TxConfig: encCfg.TxConfig, + LegacyAmino: encCfg.Amino, + InterfaceRegistry: encCfg.InterfaceRegistry, + AccountRetriever: authtypes.AccountRetriever{}, + AppConstructor: func(val cli.Validator) servertypes.Application { + return NewTestNibiruAppWithGenesis(appGenesis) + }, + GenesisState: appGenesis, + TimeoutCommit: time.Second / 2, + ChainID: "chain-" + tmrand.NewRand().Str(6), + NumValidators: 1, + BondDenom: common.DenomNIBI, + MinGasPrices: fmt.Sprintf("0.000006%s", common.DenomNIBI), + AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction), + StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction), + BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction), + StartingTokens: sdk.NewCoins( + sdk.NewCoin(common.DenomNUSD, sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)), + sdk.NewCoin(common.DenomNIBI, sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction)), + sdk.NewCoin(common.DenomUSDC, sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)), + ), + PruningStrategy: storetypes.PruningOptionNothing, + CleanupDir: true, + SigningAlgo: string(hd.Secp256k1Type), + KeyringOptions: []keyring.Option{}, + } +} diff --git a/x/testutil/testapp/genesis.go b/x/testutil/testapp/genesis.go new file mode 100644 index 000000000..a7aaea9f7 --- /dev/null +++ b/x/testutil/testapp/genesis.go @@ -0,0 +1,81 @@ +package testapp + +import ( + "encoding/json" + "io" + "os" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + tmjson "github.com/tendermint/tendermint/libs/json" + tmtypes "github.com/tendermint/tendermint/types" + + "github.com/NibiruChain/nibiru/app" +) + +// AppStateFromGenesisFileFn util function to generate the genesis AppState +// from a genesis.json file. +func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (tmtypes.GenesisDoc, []simtypes.Account) { + bytes, err := os.ReadFile(genesisFile) + if err != nil { + panic(err) + } + + var genesis tmtypes.GenesisDoc + // NOTE: Tendermint uses a custom JSON decoder for GenesisDoc + err = tmjson.Unmarshal(bytes, &genesis) + if err != nil { + panic(err) + } + + var appState GenesisState + err = json.Unmarshal(genesis.AppState, &appState) + if err != nil { + panic(err) + } + + var authGenesis authtypes.GenesisState + if appState[authtypes.ModuleName] != nil { + cdc.MustUnmarshalJSON(appState[authtypes.ModuleName], &authGenesis) + } + + newAccs := make([]simtypes.Account, len(authGenesis.Accounts)) + for i, acc := range authGenesis.Accounts { + // Pick a random private key, since we don't know the actual key + // This should be fine as it's only used for mock Tendermint validators + // and these keys are never actually used to sign by mock Tendermint. + privkeySeed := make([]byte, 15) + if _, err := r.Read(privkeySeed); err != nil { + panic(err) + } + + privKey := secp256k1.GenPrivKeyFromSecret(privkeySeed) + + a, ok := acc.GetCachedValue().(authtypes.AccountI) + if !ok { + panic("expected account") + } + + // create simulator accounts + simAcc := simtypes.Account{PrivKey: privKey, PubKey: privKey.PubKey(), Address: a.GetAddress()} + newAccs[i] = simAcc + } + + return genesis, newAccs +} + +// GenesisState of the blockchain is represented here as a map of raw json +// messages key'd by a identifier string. +// The identifier is used to determine which module genesis information belongs +// to so it may be appropriately routed during init chain. +// Within this application default genesis information is retrieved from +// the ModuleBasicManager which populates json from each BasicModule +// object provided to it during init. +type GenesisState map[string]json.RawMessage + +// NewDefaultGenesisState generates the default state for the application. +func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState { + return app.ModuleBasics.DefaultGenesis(cdc) +} diff --git a/x/testutil/testapp/testapp.go b/x/testutil/testapp/testapp.go new file mode 100644 index 000000000..e06539d53 --- /dev/null +++ b/x/testutil/testapp/testapp.go @@ -0,0 +1,167 @@ +package testapp + +import ( + "encoding/json" + "os" + "path/filepath" + "time" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/simapp" + sdk "github.com/cosmos/cosmos-sdk/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmdb "github.com/tendermint/tm-db" + + "github.com/NibiruChain/nibiru/app" + + "github.com/NibiruChain/nibiru/x/common" + pricefeedtypes "github.com/NibiruChain/nibiru/x/pricefeed/types" +) + +// NewTestNibiruApp creates an application instance ('app.NibiruApp') with an in-memory +// database ('tmdb.MemDB') and disabled logging. It either uses the application's +// default genesis state or a blank one. +func NewTestNibiruApp(shouldUseDefaultGenesis bool) *app.NibiruApp { + encoding := simapp.MakeTestEncodingConfig() + var appGenesis GenesisState + if shouldUseDefaultGenesis { + appGenesis = NewDefaultGenesisState(encoding.Marshaler) + } + return NewTestNibiruAppWithGenesis(appGenesis) +} + +// NewTestNibiruAppAndContext creates an 'app.NibiruApp' instance with an in-memory +// 'tmdb.MemDB' and fresh 'sdk.Context'. +func NewTestNibiruAppAndContext(shouldUseDefaultGenesis bool) (*app.NibiruApp, sdk.Context) { + newNibiruApp := NewTestNibiruApp(shouldUseDefaultGenesis) + ctx := newNibiruApp.NewContext(false, tmproto.Header{}) + + return newNibiruApp, ctx +} + +// NewTestNibiruAppWithGenesis initializes a chain with the given genesis state to +// creates an application instance ('app.NibiruApp'). This app uses an +// in-memory database ('tmdb.MemDB') and has logging disabled. +func NewTestNibiruAppWithGenesis(gen GenesisState) *app.NibiruApp { + userHomeDir, err := os.UserHomeDir() + if err != nil { + panic(err) + } + + nodeHome := filepath.Join(userHomeDir, ".nibid") + db := tmdb.NewMemDB() + logger := log.NewNopLogger() + + encoding := app.MakeTestEncodingConfig() + + nibiruApp := app.NewNibiruApp( + logger, + db, + /*traceStore=*/ nil, + /*loadLatest=*/ true, + /*skipUpgradeHeights=*/ map[int64]bool{}, + /*homePath=*/ nodeHome, + /*invCheckPeriod=*/ 0, + /*encodingConfig=*/ encoding, + /*appOpts=*/ simapp.EmptyAppOptions{}, + ) + + stateBytes, err := json.MarshalIndent(gen, "", " ") + if err != nil { + panic(err) + } + + nibiruApp.InitChain(abci.RequestInitChain{ + ConsensusParams: simapp.DefaultConsensusParams, + AppStateBytes: stateBytes, + }) + + return nibiruApp +} + +// ---------------------------------------------------------------------------- +// Genesis +// ---------------------------------------------------------------------------- + +const ( + GenOracleAddress = "nibi1zuxt7fvuxgj69mjxu3auca96zemqef5u2yemly" +) + +/* + NewTestGenesisStateFromDefault returns 'NewGenesisState' using the default + +genesis as input. The blockchain genesis state is represented as a map from module +identifier strings to raw json messages. +*/ +func NewTestGenesisStateFromDefault() GenesisState { + encodingConfig := app.MakeTestEncodingConfig() + codec := encodingConfig.Marshaler + genState := NewDefaultGenesisState(codec) + return NewTestGenesisState(codec, genState) +} + +/* +NewTestGenesisState transforms 'inGenState' to add genesis parameter changes +that are well suited to integration testing, then returns the transformed genesis. +The blockchain genesis state is represented as a map from module identifier strings +to raw json messages. + +Args: +- codec: Serializer for the module genesis state proto.Messages +- inGenState: Input genesis state before the custom test setup is applied +*/ +func NewTestGenesisState(codec codec.Codec, inGenState GenesisState, +) (testGenState GenesisState) { + testGenState = inGenState + + // Set short voting period to allow fast gov proposals in tests + var govGenState govtypes.GenesisState + codec.MustUnmarshalJSON(testGenState[govtypes.ModuleName], &govGenState) + govGenState.VotingParams.VotingPeriod = time.Second * 20 + govGenState.DepositParams.MinDeposit = sdk.NewCoins(sdk.NewInt64Coin(common.DenomNIBI, 1_000_000)) // min deposit of 1 NIBI + testGenState[govtypes.ModuleName] = codec.MustMarshalJSON(&govGenState) + + // pricefeed genesis state + pfGenState := PricefeedGenesis() + testGenState[pricefeedtypes.ModuleName] = codec.MustMarshalJSON(&pfGenState) + + return testGenState +} + +// ---------------------------------------------------------------------------- +// Module types.GenesisState functions + +/* + PricefeedGenesis returns an x/pricefeed GenesisState with additional + +configuration for convenience during integration tests. +*/ +func PricefeedGenesis() pricefeedtypes.GenesisState { + oracle := sdk.MustAccAddressFromBech32(GenOracleAddress) + oracleStrings := []string{oracle.String()} + + var gen pricefeedtypes.GenesisState + pairs := pricefeedtypes.DefaultPairs + gen.Params.Pairs = pairs + gen.Params.TwapLookbackWindow = 15 * time.Minute + gen.PostedPrices = []pricefeedtypes.PostedPrice{ + { + PairID: pairs[0].String(), // PairGovStable + Oracle: oracle.String(), + Price: sdk.NewDec(10), + Expiry: time.Now().Add(1 * time.Hour), + }, + { + PairID: pairs[1].String(), // PairCollStable + Oracle: oracle.String(), + Price: sdk.OneDec(), + Expiry: time.Now().Add(1 * time.Hour), + }, + } + gen.GenesisOracles = oracleStrings + + return gen +} diff --git a/x/testutil/testapp/testapp_test.go b/x/testutil/testapp/testapp_test.go new file mode 100644 index 000000000..c50b0927d --- /dev/null +++ b/x/testutil/testapp/testapp_test.go @@ -0,0 +1,99 @@ +package testapp_test + +import ( + "testing" + "time" + + "github.com/NibiruChain/nibiru/simapp" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/suite" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + "github.com/NibiruChain/nibiru/app" + "github.com/NibiruChain/nibiru/x/common" + pricefeedtypes "github.com/NibiruChain/nibiru/x/pricefeed/types" +) + +type TestappSuite struct { + suite.Suite + + genOracle sdk.AccAddress + pairs common.AssetPairs + twapLookbackWindow time.Duration +} + +func (s *TestappSuite) SetupSuite() { + app.SetPrefixes(app.AccountAddressPrefix) + s.genOracle = sdk.MustAccAddressFromBech32(simapp.GenOracleAddress) + s.pairs = pricefeedtypes.DefaultPairs + s.twapLookbackWindow = pricefeedtypes.DefaultLookbackWindow +} + +// TestPricefeedGenesis verifies that the expected pricefeed state for integration tests +func (s *TestappSuite) TestPricefeedGenesis() { + genPf := simapp.PricefeedGenesis() + s.Assert().EqualValues(pricefeedtypes.NewParams(s.pairs, s.twapLookbackWindow), genPf.Params) + s.Assert().EqualValues(pricefeedtypes.NewParams(s.pairs, s.twapLookbackWindow), genPf.Params) + s.Assert().EqualValues(s.pairs[0].String(), genPf.PostedPrices[0].PairID) + s.Assert().EqualValues(s.pairs[1].String(), genPf.PostedPrices[1].PairID) + expectedGenesisOracles := []string{s.genOracle.String()} + for _, oracleStr := range expectedGenesisOracles { + s.Assert().Contains(genPf.GenesisOracles, oracleStr) + } +} + +func (s *TestappSuite) TestNewTestGenesisState() { + encodingConfig := app.MakeTestEncodingConfig() + codec := encodingConfig.Marshaler + + defaultGenState := app.NewDefaultGenesisState(codec) + testGenState := simapp.NewTestGenesisStateFromDefault() + + var testGenPfState pricefeedtypes.GenesisState + testGenPfStateJSON := testGenState[pricefeedtypes.ModuleName] + codec.MustUnmarshalJSON(testGenPfStateJSON, &testGenPfState) + bzTest := codec.MustMarshalJSON(&testGenPfState) + + var defaultGenPfState pricefeedtypes.GenesisState + defaultGenPfStateJSON := defaultGenState[pricefeedtypes.ModuleName] + codec.MustUnmarshalJSON(defaultGenPfStateJSON, &defaultGenPfState) + bzDefault := codec.MustMarshalJSON(&defaultGenPfState) + + s.Assert().NotEqualValues(bzTest, bzDefault) + s.Assert().NotEqualValues(testGenPfState, defaultGenPfState) + + s.Assert().EqualValues(pricefeedtypes.NewParams(s.pairs, s.twapLookbackWindow), testGenPfState.Params) + s.Assert().EqualValues(pricefeedtypes.NewParams(s.pairs, s.twapLookbackWindow), testGenPfState.Params) + s.Assert().EqualValues(s.pairs[0].String(), testGenPfState.PostedPrices[0].PairID) + s.Assert().EqualValues(s.pairs[1].String(), testGenPfState.PostedPrices[1].PairID) + expectedGenesisOracles := []string{s.genOracle.String()} + for _, oracleStr := range expectedGenesisOracles { + s.Assert().Contains(testGenPfState.GenesisOracles, oracleStr) + } +} + +func (s *TestappSuite) TestPricefeedGenesis_PostedPrices() { + s.T().Log("no prices posted for default genesis") + nibiruApp := simapp.NewTestNibiruApp(true) + ctx := nibiruApp.NewContext(false, tmproto.Header{}) + currentPrices := nibiruApp.PricefeedKeeper.GetCurrentPrices(ctx) + s.Assert().Len(currentPrices, 0) + + s.T().Log("prices posted for testing genesis") + nibiruApp = simapp.NewTestNibiruAppWithGenesis(simapp.NewTestGenesisStateFromDefault()) + ctx = nibiruApp.NewContext(false, tmproto.Header{}) + oracles := []sdk.AccAddress{s.genOracle} + oracleMap := nibiruApp.PricefeedKeeper.GetOraclesForPairs(ctx, s.pairs) + for _, pair := range s.pairs { + s.Assert().EqualValues(oracles, oracleMap[pair]) + } + currentPrices = nibiruApp.PricefeedKeeper.GetCurrentPrices(ctx) + s.Assert().Len(currentPrices, 2) + s.Assert().Equal(common.Pair_NIBI_NUSD.String(), currentPrices[0].PairID) + s.Assert().Equal(common.Pair_USDC_NUSD.String(), currentPrices[1].PairID) +} + +func TestTestappSuite(t *testing.T) { + suite.Run(t, new(TestappSuite)) +}