/
operations.go
313 lines (286 loc) · 9.71 KB
/
operations.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package simulation
import (
"math/rand"
"strings"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
appparams "github.com/cosmosquad-labs/squad/v3/app/params"
utils "github.com/cosmosquad-labs/squad/v3/types"
"github.com/cosmosquad-labs/squad/v3/x/lpfarm/keeper"
"github.com/cosmosquad-labs/squad/v3/x/lpfarm/types"
minttypes "github.com/cosmosquad-labs/squad/v3/x/mint/types"
)
// Simulation operation weights constants.
const (
OpWeightMsgCreatePrivatePlan = "op_weight_msg_create_private_plan"
OpWeightMsgFarm = "op_weight_msg_farm"
OpWeightMsgUnfarm = "op_weight_msg_unfarm"
OpWeightMsgHarvest = "op_weight_msg_harvest"
DefaultWeightCreatePrivatePlan = 10
DefaultWeightFarm = 40
DefaultWeightUnfarm = 50
DefaultWeightHarvest = 20
)
var (
gas = uint64(20000000)
fees = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}
)
// WeightedOperations returns all the operations from the module with their respective weights.
func WeightedOperations(
appParams simtypes.AppParams, cdc codec.JSONCodec,
ak types.AccountKeeper, bk types.BankKeeper, lk types.LiquidityKeeper, k keeper.Keeper,
) simulation.WeightedOperations {
var (
weightMsgCreatePrivatePlan int
weightMsgFarm int
weightMsgUnfarm int
weightMsgHarvest int
)
appParams.GetOrGenerate(cdc, OpWeightMsgCreatePrivatePlan, &weightMsgCreatePrivatePlan, nil, func(_ *rand.Rand) {
weightMsgCreatePrivatePlan = DefaultWeightCreatePrivatePlan
})
appParams.GetOrGenerate(cdc, OpWeightMsgFarm, &weightMsgFarm, nil, func(_ *rand.Rand) {
weightMsgFarm = DefaultWeightFarm
})
appParams.GetOrGenerate(cdc, OpWeightMsgUnfarm, &weightMsgUnfarm, nil, func(_ *rand.Rand) {
weightMsgUnfarm = DefaultWeightUnfarm
})
appParams.GetOrGenerate(cdc, OpWeightMsgHarvest, &weightMsgHarvest, nil, func(_ *rand.Rand) {
weightMsgHarvest = DefaultWeightHarvest
})
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgCreatePrivatePlan,
SimulateMsgCreatePrivatePlan(ak, bk, lk, k),
),
simulation.NewWeightedOperation(
weightMsgFarm,
SimulateMsgFarm(ak, bk),
),
simulation.NewWeightedOperation(
weightMsgUnfarm,
SimulateMsgUnfarm(ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgHarvest,
SimulateMsgHarvest(ak, bk, k),
),
}
}
func SimulateMsgCreatePrivatePlan(
ak types.AccountKeeper, bk types.BankKeeper, lk types.LiquidityKeeper, 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) {
if !k.CanCreatePrivatePlan(ctx) {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgCreatePrivatePlan,
"cannot create more private plans"), nil, nil
}
rewardAllocs, ok := genRewardAllocs(r, ctx, lk)
if !ok {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgCreatePrivatePlan, "no pairs"), nil, nil
}
simAccount, _ := simtypes.RandomAcc(r, accs)
msg := types.NewMsgCreatePrivatePlan(
simAccount.Address, "Farming Plan", rewardAllocs,
ctx.BlockTime().AddDate(0, 0, 1),
ctx.BlockTime().AddDate(0, 0, 2+r.Intn(5)))
fundAddr(ctx, bk, simAccount.Address, k.GetPrivatePlanCreationFee(ctx))
acc := ak.GetAccount(ctx, simAccount.Address)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
gas,
chainID,
[]uint64{acc.GetAccountNumber()},
[]uint64{acc.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(
types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}
// Fund the newly created private farming plan's farming pool
planId, _ := k.GetLastPlanId(ctx)
plan, _ := k.GetPlan(ctx, planId)
fundAddr(ctx, bk, plan.GetFarmingPoolAddress(), utils.ParseCoins("1000000_000000stake"))
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
}
}
func SimulateMsgFarm(ak types.AccountKeeper, bk types.BankKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
accs = utils.ShuffleSimAccounts(r, accs)
var simAccount simtypes.Account
var spendable sdk.Coins
var coinToFarm sdk.Coin
skip := true
for _, simAccount = range accs {
spendable = bk.SpendableCoins(ctx, simAccount.Address)
poolCoins := sdk.Coins{}
for _, coin := range spendable {
if strings.HasPrefix(coin.Denom, "pool") {
poolCoins = poolCoins.Add(coin)
}
}
poolCoins = simtypes.RandSubsetCoins(r, poolCoins)
if len(poolCoins) > 0 {
coinToFarm = poolCoins[r.Intn(len(poolCoins))]
skip = false
break
}
}
if skip {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgFarm, "no account to farm"), nil, nil
}
msg := types.NewMsgFarm(simAccount.Address, coinToFarm)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: appparams.MakeTestEncodingConfig().TxConfig,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: types.ModuleName,
CoinsSpentInMsg: spendable,
}
return utils.GenAndDeliverTxWithFees(txCtx, gas, fees)
}
}
func SimulateMsgUnfarm(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) {
accs = utils.ShuffleSimAccounts(r, accs)
var simAccount simtypes.Account
var spendable sdk.Coins
var coinToUnfarm sdk.Coin
skip := true
for _, simAccount = range accs {
var positions []types.Position
k.IteratePositionsByFarmer(ctx, simAccount.Address, func(position types.Position) (stop bool) {
positions = append(positions, position)
return false
})
if len(positions) > 0 {
position := positions[r.Intn(len(positions))]
coinToUnfarm = sdk.NewCoin(
position.Denom,
utils.RandomInt(r, sdk.OneInt(), position.FarmingAmount))
skip = false
break
}
}
if skip {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgUnfarm, "no account to unfarm"), nil, nil
}
msg := types.NewMsgUnfarm(simAccount.Address, coinToUnfarm)
spendable = bk.SpendableCoins(ctx, simAccount.Address)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: appparams.MakeTestEncodingConfig().TxConfig,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: types.ModuleName,
CoinsSpentInMsg: spendable,
}
return utils.GenAndDeliverTxWithFees(txCtx, gas, fees)
}
}
func SimulateMsgHarvest(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) {
accs = utils.ShuffleSimAccounts(r, accs)
var simAccount simtypes.Account
var spendable sdk.Coins
var denomToHarvest string
skip := true
for _, simAccount = range accs {
var positions []types.Position
k.IteratePositionsByFarmer(ctx, simAccount.Address, func(position types.Position) (stop bool) {
positions = append(positions, position)
return false
})
if len(positions) > 0 {
position := positions[r.Intn(len(positions))]
denomToHarvest = position.Denom
skip = false
break
}
}
if skip {
return simtypes.NoOpMsg(
types.ModuleName, types.TypeMsgHarvest, "no account to harvest"), nil, nil
}
msg := types.NewMsgHarvest(simAccount.Address, denomToHarvest)
spendable = bk.SpendableCoins(ctx, simAccount.Address)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: appparams.MakeTestEncodingConfig().TxConfig,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: types.ModuleName,
CoinsSpentInMsg: spendable,
}
return utils.GenAndDeliverTxWithFees(txCtx, gas, fees)
}
}
func fundAddr(ctx sdk.Context, bk types.BankKeeper, addr sdk.AccAddress, amt sdk.Coins) {
if err := bk.MintCoins(ctx, minttypes.ModuleName, amt); err != nil {
panic(err)
}
if err := bk.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amt); err != nil {
panic(err)
}
}
func genRewardAllocs(r *rand.Rand, ctx sdk.Context, lk types.LiquidityKeeper) (rewardAllocs []types.RewardAllocation, ok bool) {
pairs := lk.GetAllPairs(ctx)
if len(pairs) == 0 {
return nil, false
}
r.Shuffle(len(pairs), func(i, j int) {
pairs[i], pairs[j] = pairs[j], pairs[i]
})
n := r.Intn(len(pairs)) + 1 // Number of reward allocs
for i := 0; i < n; i++ {
pair := pairs[i]
rewardsPerDay := simtypes.RandSubsetCoins(r, utils.ParseCoins("1000_000000stake"))
rewardAllocs = append(rewardAllocs, types.NewPairRewardAllocation(pair.Id, rewardsPerDay))
}
return rewardAllocs, true
}