From 4b8e9265e32a088f1e9fe69c6115103b4f089331 Mon Sep 17 00:00:00 2001 From: Paul Chen Date: Wed, 14 Jun 2023 20:27:10 +0800 Subject: [PATCH] test: add proposal msg --- x/tokenfactory/module.go | 5 +++ x/tokenfactory/simulation/operations.go | 17 ++++++--- x/tokenfactory/simulation/proposal.go | 47 +++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 x/tokenfactory/simulation/proposal.go diff --git a/x/tokenfactory/module.go b/x/tokenfactory/module.go index 6665a78895..9de4c069be 100644 --- a/x/tokenfactory/module.go +++ b/x/tokenfactory/module.go @@ -193,6 +193,11 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) { // RegisterStoreDecoder performs a no-op. func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {} +// ProposalMsgs returns msgs used for governance proposals for simulations. +func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg { + return simulation.ProposalMsgs() +} + // WeightedOperations returns the all the tokenfactory module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { return simulation.WeightedOperations(simState.AppParams, simState.Cdc, am.sk, am.tfk, am.ak, am.bk) diff --git a/x/tokenfactory/simulation/operations.go b/x/tokenfactory/simulation/operations.go index ee39aa8c21..a0b3760727 100644 --- a/x/tokenfactory/simulation/operations.go +++ b/x/tokenfactory/simulation/operations.go @@ -75,7 +75,7 @@ func WeightedOperations( return sim.WeightedOperations{ sim.NewWeightedOperation( weightMsgCreateDenom, - SimulateMsgCreateDenom(sk, ak, bk), + SimulateMsgCreateDenom(sk, tfk, ak, bk), ), sim.NewWeightedOperation( weightMsgMint, @@ -96,7 +96,7 @@ func WeightedOperations( // SimulateMsgCreateDenom tests and runs a single MsgCreateDenom func SimulateMsgCreateDenom( - sk types.SubspacesKeeper, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, + sk types.SubspacesKeeper, tfk types.TokenFactoryKeeper, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, ) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, @@ -104,7 +104,7 @@ func SimulateMsgCreateDenom( ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { // Get the data - subspaceID, subdenom, signer, skip := randomCreateDenomFields(r, ctx, accs, sk, bk) + subspaceID, subdenom, signer, skip := randomCreateDenomFields(r, ctx, accs, sk, tfk, bk) if skip { return simtypes.NoOpMsg(types.RouterKey, "MsgCreateDenom", "skip"), nil, nil } @@ -118,7 +118,7 @@ func SimulateMsgCreateDenom( // randomCreateDenomFields returns the data used to build a random MsgCreateDenom func randomCreateDenomFields( - r *rand.Rand, ctx sdk.Context, accs []simtypes.Account, sk types.SubspacesKeeper, bk types.BankKeeper, + r *rand.Rand, ctx sdk.Context, accs []simtypes.Account, sk types.SubspacesKeeper, tfk types.TokenFactoryKeeper, bk bankkeeper.Keeper, ) (subspaceID uint64, subdenom string, signer simtypes.Account, skip bool) { // Get a subspace id @@ -131,6 +131,15 @@ func randomCreateDenomFields( subspace := subspacessim.RandomSubspace(r, subspaces) subspaceID = subspace.ID + // Check treasury balances + balances := bk.SpendableCoins(ctx, sdk.MustAccAddressFromBech32(subspace.Treasury)) + creationFees := tfk.GetParams(ctx).DenomCreationFee + if balances.IsAllLT(creationFees) { + // Skip because treasury does not have enough coins + skip = true + return + } + // Get a denom subdenom = simtypes.RandStringOfLength(r, 6) denom, _ := tokenfactorytypes.GetTokenDenom(subspace.Treasury, subdenom) diff --git a/x/tokenfactory/simulation/proposal.go b/x/tokenfactory/simulation/proposal.go new file mode 100644 index 0000000000..2473afe33d --- /dev/null +++ b/x/tokenfactory/simulation/proposal.go @@ -0,0 +1,47 @@ +package simulation + +import ( + "math/rand" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/address" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" + + "github.com/desmos-labs/desmos/v5/x/tokenfactory/types" +) + +// DONTCOVER + +// Simulation operation weights constants +const ( + DefaultWeightMsgUpdateParams int = 50 + + OpWeightMsgUpdateParams = "op_weight_msg_update_params" //nolint:gosec +) + +// ProposalMsgs defines the module weighted proposals' contents +func ProposalMsgs() []simtypes.WeightedProposalMsg { + return []simtypes.WeightedProposalMsg{ + simulation.NewWeightedProposalMsg( + OpWeightMsgUpdateParams, + DefaultWeightMsgUpdateParams, + SimulateMsgUpdateParams, + ), + } +} + +// SimulateMsgUpdateParams returns a random MsgUpdateParams +func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg { + // use the default gov module account address as authority + var authority sdk.AccAddress = address.Module("gov") + + params := types.NewParams(sdk.NewCoins( + sdk.NewCoin(sdk.DefaultBondDenom, simtypes.RandomAmount(r, sdk.NewInt(100)))), + ) + + return &types.MsgUpdateParams{ + Authority: authority.String(), + Params: params, + } +}