Skip to content

Commit

Permalink
Merge pull request #342 from BitCannaGlobal/v047-burn-module-add-params
Browse files Browse the repository at this point in the history
V047 burn module tests
  • Loading branch information
RaulBernal authored Mar 28, 2024
2 parents 5f3076d + 88d4c63 commit 45a70dd
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
6 changes: 3 additions & 3 deletions x/burn/module_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgBurnCoinsAction,
burnsimulation.SimulateMsgBurnCoinsAction(am.accountKeeper, am.bankKeeper, am.keeper),
burnsimulation.SimulateMsgBurnCoinsAction(am.accountKeeper, am.bankKeeper, am.keeper, types.ModuleCdc),
))

// this line is used by starport scaffolding # simapp/module/operation

return operations
}

// ProposalMsgs returns msgs used for governance proposals for simulations.
// ProposalMsgs returns msgs used for governance proposals for simulations. // TODO
func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
return []simtypes.WeightedProposalMsg{
simulation.NewWeightedProposalMsg(
opWeightMsgBurnCoinsAction,
defaultWeightMsgBurnCoinsAction,
func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg {
burnsimulation.SimulateMsgBurnCoinsAction(am.accountKeeper, am.bankKeeper, am.keeper)
burnsimulation.SimulateMsgBurnCoinsAction(am.accountKeeper, am.bankKeeper, am.keeper, types.ModuleCdc)
return nil
},
),
Expand Down
41 changes: 37 additions & 4 deletions x/burn/simulation/burn_coins_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,57 @@ import (
"github.com/BitCannaGlobal/bcna/x/burn/keeper"
"github.com/BitCannaGlobal/bcna/x/burn/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
)

// func SimulateMsgBurnCoinsAction(
// ak types.AccountKeeper,
// bk types.BankKeeper,
// k keeper.Keeper,
// ) simtypes.Operation {
// return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
// ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
// simAccount, _ := simtypes.RandomAcc(r, accs)
// msg := &types.MsgBurnCoinsAction{
// Creator: simAccount.Address.String(),
// }

// // TODO: Handling the BurnCoinsAction simulation

// return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "BurnCoinsAction simulation not implemented"), nil, nil
// }
// }
func SimulateMsgBurnCoinsAction(
ak types.AccountKeeper,
bk types.BankKeeper,
k keeper.Keeper,
cdc *codec.ProtoCodec,
) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
// Select a random account
simAccount, _ := simtypes.RandomAcc(r, accs)

// Get the balance of the account
balance := bk.GetBalance(ctx, simAccount.Address, "ubcna")

// Has the account coins?
if balance.IsZero() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgBurnCoinsAction, "Balance insuficiente"), nil, nil
}

// Assign a ramdom amount (within the max balance)
amount := simtypes.RandIntBetween(r, 1, int(balance.Amount.Int64()))

// Build and send the message
msg := &types.MsgBurnCoinsAction{
Creator: simAccount.Address.String(),
Amount: sdk.NewCoin(balance.Denom, sdk.NewInt(int64(amount))),
}

// TODO: Handling the BurnCoinsAction simulation
opMsg := simtypes.NewOperationMsg(msg, true, "", cdc)

return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "BurnCoinsAction simulation not implemented"), nil, nil
return opMsg, nil, nil
}
}
6 changes: 4 additions & 2 deletions x/burn/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ func TestGenesisState_Validate(t *testing.T) {
valid: true,
},
{
desc: "valid genesis state",
desc: "valid genesis state",
genState: &types.GenesisState{

Params: types.Params{
BurnDenom: "ubcna",
},
// this line is used by starport scaffolding # types/genesis/validField
},
valid: true,
Expand Down
5 changes: 5 additions & 0 deletions x/burn/types/message_burn_coins_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ func (msg *MsgBurnCoinsAction) ValidateBasic() error {
if msg.Amount.IsNegative() || msg.Amount.IsZero() || !msg.Amount.IsValid() {
return fmt.Errorf("amount must be positive or valid")
}
// Let's filter the length in the amount (max digits or bigger possible number )
maxDigits := 18
if len(msg.Amount.Amount.String()) > maxDigits {
return fmt.Errorf("token amount exceeds maximum length of %d digits", maxDigits)
}
return nil
}
42 changes: 41 additions & 1 deletion x/burn/types/message_burn_coins_action_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import (
"fmt"
"math"
"strings"
"testing"

"github.com/BitCannaGlobal/bcna/testutil/sample"
Expand All @@ -23,11 +26,48 @@ func TestMsgBurnCoinsAction_ValidateBasic(t *testing.T) {
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid address",
name: "valid address and valid amount",
msg: MsgBurnCoinsAction{
Creator: sample.AccAddress(),
Amount: sdk.NewInt64Coin("testcoin", 1000),
},
}, {
name: "negative amount",
msg: MsgBurnCoinsAction{
Creator: sample.AccAddress(),
Amount: sdk.Coin{Denom: "testcoin", Amount: sdk.NewInt(-1000)},
},
err: fmt.Errorf("amount must be positive or valid"),
}, {
name: "zero amount",
msg: MsgBurnCoinsAction{
Creator: sample.AccAddress(),
Amount: sdk.NewInt64Coin("testcoin", 0),
},
err: fmt.Errorf("amount must be positive or valid"),
}, {
name: "invalid denom",
msg: MsgBurnCoinsAction{
Creator: sample.AccAddress(),
Amount: sdk.Coin{Denom: "", Amount: sdk.NewInt(1000)},
},
err: fmt.Errorf("amount must be positive or valid"),
},
{
name: "excessive token amount length",
msg: MsgBurnCoinsAction{
Creator: sample.AccAddress(),
Amount: sdk.Coin{Denom: "ubcna", Amount: sdk.NewInt(math.MaxInt64)}, //9223372036854775807
},
err: fmt.Errorf("token amount exceeds maximum length of 18 digits"),
},
{
name: "excessive denom length",
msg: MsgBurnCoinsAction{
Creator: sample.AccAddress(),
Amount: sdk.Coin{Denom: strings.Repeat("a", 129), Amount: sdk.NewInt(1000)},
},
err: fmt.Errorf("amount must be positive or valid"),
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 45a70dd

Please sign in to comment.