Skip to content

Commit

Permalink
refactor!: revive dex module using intermediate test app (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Nov 30, 2022
1 parent 2e8b4ed commit 1168754
Show file tree
Hide file tree
Showing 17 changed files with 505 additions and 87 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
86 changes: 52 additions & 34 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -168,6 +171,7 @@ var (
ibc.AppModuleBasic{},
ibctransfer.AppModuleBasic{},
// native x/
dex.AppModuleBasic{},
pricefeed.AppModuleBasic{},
epochs.AppModuleBasic{},
perp.AppModuleBasic{},
Expand All @@ -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: {},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -262,6 +267,7 @@ type NibiruApp struct {
perpKeeper perpkeeper.Keeper
pricefeedKeeper pricefeedkeeper.Keeper
vpoolKeeper vpoolkeeper.Keeper
DexKeeper dexkeeper.Keeper

// WASM keepers
wasmKeeper wasm.Keeper
Expand Down Expand Up @@ -337,6 +343,7 @@ func NewNibiruApp(
ibchost.StoreKey,
ibctransfertypes.StoreKey,
// nibiru x/ keys
dextypes.StoreKey,
pricefeedtypes.StoreKey,
epochstypes.StoreKey,
perptypes.StoreKey,
Expand Down Expand Up @@ -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)
Expand All @@ -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),
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 --------------------------
Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -623,6 +637,7 @@ func NewNibiruApp(
vestingtypes.ModuleName,
stakingtypes.ModuleName,
// native x/
dextypes.ModuleName,
pricefeedtypes.ModuleName,
epochstypes.ModuleName,
vpooltypes.ModuleName,
Expand Down Expand Up @@ -652,6 +667,7 @@ func NewNibiruApp(
vestingtypes.ModuleName,
// native x/
epochstypes.ModuleName,
dextypes.ModuleName,
pricefeedtypes.ModuleName,
vpooltypes.ModuleName,
perptypes.ModuleName,
Expand Down Expand Up @@ -687,6 +703,7 @@ func NewNibiruApp(
// native x/
pricefeedtypes.ModuleName,
epochstypes.ModuleName,
dextypes.ModuleName,
vpooltypes.ModuleName,
perptypes.ModuleName,
utiltypes.ModuleName,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions x/dex/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
)
Expand Down
6 changes: 3 additions & 3 deletions x/dex/client/testutil/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions x/dex/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/dex/keeper/balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit 1168754

Please sign in to comment.