Skip to content

Commit

Permalink
feat: proposal simulation for erc20 module
Browse files Browse the repository at this point in the history
  • Loading branch information
poorphd committed Jul 8, 2024
1 parent 2d65e23 commit f1af21c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
31 changes: 27 additions & 4 deletions x/erc20/simulation/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func SimulateMsgConvertCoin(k keeper.Keeper, ak types.AccountKeeper, bk types.Ba
Bankkeeper: bk,
ModuleName: types.ModuleName,
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
op, fOps, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
return op, fOps, err
}
}

Expand All @@ -127,10 +128,11 @@ func SimulateMsgConvertErc20(k keeper.Keeper, ak types.AccountKeeper, bk types.B
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {

pairs := k.GetTokenPairs(ctx)

if len(pairs) == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgConvertERC20, "no pairs available"), nil, nil

_, err := SimulateRegisterERC20(r, ctx, accs, k, ak, bk, ek, fk)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgConvertERC20, "no pairs available"), nil, nil
Expand All @@ -141,10 +143,29 @@ func SimulateMsgConvertErc20(k keeper.Keeper, ak types.AccountKeeper, bk types.B
// randomly pick one pair
pair := pairs[r.Intn(len(pairs))]

erc20ABI := contracts.ERC20MinterBurnerDecimalsContract.ABI
deployer := types.ModuleAddress
contractAddr := pair.GetERC20Contract()
randomIteration := r.Intn(10)
for i := 0; i < randomIteration; i++ {
simAccount, _ := simtypes.RandomAcc(r, accs)

mintAmt := sdkmath.NewInt(1000000000)
receiver := common.BytesToAddress(simAccount.Address.Bytes())
before := k.BalanceOf(ctx, erc20ABI, contractAddr, receiver)
_, err := k.CallEVM(ctx, erc20ABI, deployer, contractAddr, true, "mint", receiver, mintAmt.BigInt())
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgConvertERC20, "no account has native ERC20"), nil, nil
}
after := k.BalanceOf(ctx, erc20ABI, contractAddr, receiver)
if after.Cmp(before.Add(before, mintAmt.BigInt())) != 0 {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgConvertERC20, "no account has native ERC20"), nil, nil
}
}

// select random account that has coins baseDenom
var simAccount simtypes.Account
var erc20Balance *big.Int
erc20ABI := contracts.ERC20MinterBurnerDecimalsContract.ABI
skip := true

for i := 0; i < len(accs); i++ {
Expand Down Expand Up @@ -175,6 +196,8 @@ func SimulateMsgConvertErc20(k keeper.Keeper, ak types.AccountKeeper, bk types.B
Bankkeeper: bk,
ModuleName: types.ModuleName,
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)

op, fOps, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
return op, fOps, err
}
}
13 changes: 11 additions & 2 deletions x/erc20/simulation/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

sdkmath "cosmossdk.io/math"
"github.com/Canto-Network/Canto/v7/app/params"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -47,11 +48,12 @@ func TestWeightedOperations(t *testing.T) {
opMsgName string
}{
{params.DefaultWeightMsgConvertCoin, types.ModuleName, sdk.MsgTypeURL(&types.MsgConvertCoin{})},
{params.DefaultWeightMsgConvertErc20, types.ModuleName, types.TypeMsgConvertERC20},
{params.DefaultWeightMsgConvertErc20, types.ModuleName, sdk.MsgTypeURL(&types.MsgConvertERC20{})},
}

for i, w := range weightedOps {
opMsg, _, _ := w.Op()(r, canto.BaseApp, ctx, accs, ctx.ChainID())
opMsg, _, err := w.Op()(r, canto.BaseApp, ctx, accs, ctx.ChainID())
require.NoError(t, err)
require.Equal(t, expected[i].weight, w.Weight())
require.Equal(t, expected[i].opMsgRoute, opMsg.Route)
require.Equal(t, expected[i].opMsgName, opMsg.Name)
Expand All @@ -74,6 +76,13 @@ func createTestApp(t *testing.T, isCheckTx bool) (*app.Canto, sdk.Context) {
ProposerAddress: consAddr,
})
ctx = ctx.WithChainID("canto_9001-1")
app.FinalizeBlock(
&abci.RequestFinalizeBlock{
Height: 1,
ProposerAddress: consAddr,
},
)

return app, ctx
}

Expand Down
30 changes: 8 additions & 22 deletions x/erc20/simulation/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account)

func SimulateRegisterCoin(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account, k keeper.Keeper, bk types.BankKeeper) (sdk.Msg, error) {
coinMetadata := types.GenRandomCoinMetadata(r)
if err := bk.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(coinMetadata.Base, sdkmath.NewInt(1)))); err != nil {
if err := bk.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(coinMetadata.Base, sdkmath.NewInt(1000000000000)))); err != nil {
panic(err)
}
bankparams := bk.GetParams(ctx)
Expand All @@ -92,12 +92,13 @@ func SimulateRegisterCoin(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account
for i := 0; i < randomIteration; i++ {
simAccount, _ := simtypes.RandomAcc(r, accs)

if err := bk.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(coinMetadata.Base, sdkmath.NewInt(100000000)))); err != nil {
if err := bk.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(coinMetadata.Base, sdkmath.NewInt(1000000000)))); err != nil {
return &types.MsgRegisterCoin{}, err
}
if err := bk.SendCoinsFromModuleToAccount(ctx, types.ModuleName, simAccount.Address, sdk.NewCoins(sdk.NewCoin(coinMetadata.Base, sdkmath.NewInt(100000000)))); err != nil {
if err := bk.SendCoinsFromModuleToAccount(ctx, types.ModuleName, simAccount.Address, sdk.NewCoins(sdk.NewCoin(coinMetadata.Base, sdkmath.NewInt(1000000000)))); err != nil {
return &types.MsgRegisterCoin{}, err
}

}

// use the default gov module account address as authority
Expand Down Expand Up @@ -136,8 +137,6 @@ func SimulateRegisterERC20(r *rand.Rand, ctx sdk.Context, accs []simtypes.Accoun
evmParams.EvmDenom = "stake"
ek.SetParams(ctx, evmParams)

isNativeErc20 := r.Intn(2) == 1

// account key
priv, err := ethsecp256k1.GenerateKey()
if err != nil {
Expand All @@ -152,23 +151,10 @@ func SimulateRegisterERC20(r *rand.Rand, ctx sdk.Context, accs []simtypes.Accoun
var contractAddr common.Address
coinMetadata := types.GenRandomCoinMetadata(r)

coins := sdk.NewCoins(sdk.NewCoin(evmParams.EvmDenom, sdkmath.NewInt(10000000000000000)))
if err = bk.MintCoins(ctx, types.ModuleName, coins); err != nil {
return &types.MsgRegisterERC20{}, err
}

if err = bk.SendCoinsFromModuleToModule(ctx, types.ModuleName, authtypes.FeeCollectorName, coins); err != nil {
return &types.MsgRegisterERC20{}, err
}
if isNativeErc20 {
deployer = types.ModuleAddress
contractAddr, err = keeper.DeployERC20Contract(ctx, k, ak, coinMetadata)
} else {
deployer = addr
erc20Name := coinMetadata.Name
erc20Symbol := coinMetadata.Symbol
contractAddr, err = keeper.DeployContract(ctx, ek, fk, deployer, signer, erc20Name, erc20Symbol, erc20Decimals)
}
deployer = addr
erc20Name := coinMetadata.Name
erc20Symbol := coinMetadata.Symbol
contractAddr, err = keeper.DeployContract(ctx, ek, fk, deployer, signer, erc20Name, erc20Symbol, erc20Decimals)

// mint cosmos coin to random accounts
randomIteration := r.Intn(10)
Expand Down

0 comments on commit f1af21c

Please sign in to comment.