Skip to content

Commit

Permalink
test: add proposal msg
Browse files Browse the repository at this point in the history
  • Loading branch information
dadamu committed Jun 14, 2023
1 parent 225e527 commit 4b8e926
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
5 changes: 5 additions & 0 deletions x/tokenfactory/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions x/tokenfactory/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func WeightedOperations(
return sim.WeightedOperations{
sim.NewWeightedOperation(
weightMsgCreateDenom,
SimulateMsgCreateDenom(sk, ak, bk),
SimulateMsgCreateDenom(sk, tfk, ak, bk),
),
sim.NewWeightedOperation(
weightMsgMint,
Expand All @@ -96,15 +96,15 @@ 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,
accs []simtypes.Account, _ string,
) (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
}
Expand All @@ -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
Expand All @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions x/tokenfactory/simulation/proposal.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 4b8e926

Please sign in to comment.