-
Notifications
You must be signed in to change notification settings - Fork 10
/
simulation.go
92 lines (79 loc) · 2.67 KB
/
simulation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package simulation
import (
"fmt"
"math/rand"
simappparams "cosmossdk.io/simapp/params"
"github.com/cosmos/cosmos-sdk/baseapp"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
sdksimulation "github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/ignite/modules/testutil/simulation"
"github.com/ignite/modules/x/claim/keeper"
"github.com/ignite/modules/x/claim/types"
)
func SimulateMsgClaim(
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) {
msg := &types.MsgClaim{}
// find an account
simAccount, _ := simtypes.RandomAcc(r, accs)
// check the account has a claim record and initial claim has not been completed
cr, found := k.GetClaimRecord(ctx, simAccount.Address.String())
if !found {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "account has no claim record"), nil, nil
}
var (
mission types.Mission
missions = k.GetAllMission(ctx)
hasMission = false
)
for _, m := range missions {
if cr.IsMissionCompleted(m.MissionID) && !cr.IsMissionClaimed(m.MissionID) {
hasMission = true
mission = m
}
}
if !hasMission {
return simtypes.NoOpMsg(
types.ModuleName,
msg.Type(),
fmt.Sprintf("%s don't have mission to claim", simAccount.Address.String()),
), nil, nil
}
// verify that there is claimable amount
airdropSupply, _ := k.GetAirdropSupply(ctx)
claimableAmount := cr.ClaimableFromMission(mission)
claimable := sdk.NewCoins(sdk.NewCoin(airdropSupply.Denom, claimableAmount))
// calculate claimable after decay factor
decayInfo := k.DecayInformation(ctx)
claimable = decayInfo.ApplyDecayFactor(claimable, ctx.BlockTime())
// check final claimable non-zero
if claimable.Empty() {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), types.ErrNoClaimable.Error()), nil, nil
}
// initialize basic message
msg = &types.MsgClaim{
Claimer: simAccount.Address.String(),
}
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx, simtestutil.DefaultGenTxGas)
}
}