-
Notifications
You must be signed in to change notification settings - Fork 13
/
operations.go
393 lines (332 loc) · 14.8 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package simulation
import (
"math/rand"
"time"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
liquidityparams "github.com/gravity-devs/liquidity/v2/app/params"
"github.com/gravity-devs/liquidity/v2/x/liquidity/keeper"
"github.com/gravity-devs/liquidity/v2/x/liquidity/types"
)
// Simulation operation weights constants.
//
//nolint:gosec
const (
OpWeightMsgCreatePool = "op_weight_msg_create_pool"
OpWeightMsgDepositWithinBatch = "op_weight_msg_deposit_to_pool"
OpWeightMsgWithdrawWithinBatch = "op_weight_msg_withdraw_from_pool"
OpWeightMsgSwapWithinBatch = "op_weight_msg_swap"
)
// 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, k keeper.Keeper,
) simulation.WeightedOperations {
var weightMsgCreatePool int
appParams.GetOrGenerate(cdc, OpWeightMsgCreatePool, &weightMsgCreatePool, nil,
func(_ *rand.Rand) {
weightMsgCreatePool = liquidityparams.DefaultWeightMsgCreatePool
},
)
var weightMsgDepositWithinBatch int
appParams.GetOrGenerate(cdc, OpWeightMsgDepositWithinBatch, &weightMsgDepositWithinBatch, nil,
func(_ *rand.Rand) {
weightMsgDepositWithinBatch = liquidityparams.DefaultWeightMsgDepositWithinBatch
},
)
var weightMsgMsgWithdrawWithinBatch int
appParams.GetOrGenerate(cdc, OpWeightMsgWithdrawWithinBatch, &weightMsgMsgWithdrawWithinBatch, nil,
func(_ *rand.Rand) {
weightMsgMsgWithdrawWithinBatch = liquidityparams.DefaultWeightMsgWithdrawWithinBatch
},
)
var weightMsgSwapWithinBatch int
appParams.GetOrGenerate(cdc, OpWeightMsgSwapWithinBatch, &weightMsgSwapWithinBatch, nil,
func(_ *rand.Rand) {
weightMsgSwapWithinBatch = liquidityparams.DefaultWeightMsgSwapWithinBatch
},
)
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgCreatePool,
SimulateMsgCreatePool(ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgDepositWithinBatch,
SimulateMsgDepositWithinBatch(ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgMsgWithdrawWithinBatch,
SimulateMsgWithdrawWithinBatch(ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgSwapWithinBatch,
SimulateMsgSwapWithinBatch(ak, bk, k),
),
}
}
// SimulateMsgCreatePool generates a MsgCreatePool with random values
func SimulateMsgCreatePool(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) {
var simAccount simtypes.Account
randomAccounts = simtypes.RandomAccounts(r, 3)
simAccount = randomAccounts[r.Intn(3)]
params := k.GetParams(ctx)
params.MaxReserveCoinAmount = GenMaxReserveCoinAmount(r)
k.SetParams(ctx, params)
// get randomized two denoms to create liquidity pool
var mintingDenoms []string
denomA, denomB := randomDenoms(r)
reserveCoinDenoms := []string{denomA, denomB}
mintingDenoms = append(mintingDenoms, reserveCoinDenoms...)
// simAccount should have some fees to pay for transaction and pool creation fee
var feeDenoms []string
for _, fee := range params.PoolCreationFee {
feeDenoms = append(feeDenoms, fee.GetDenom())
}
mintingDenoms = append(mintingDenoms, feeDenoms...)
// mint coins of randomized and fee denoms
err := mintCoins(ctx, r, bk, simAccount, mintingDenoms)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgCreatePool, "unable to mint and send coins"), nil, err
}
account := ak.GetAccount(ctx, simAccount.Address)
spendable := bk.SpendableCoins(ctx, account.GetAddress())
poolName := types.PoolName(reserveCoinDenoms, types.DefaultPoolTypeID)
reserveAcc := types.GetPoolReserveAcc(poolName, false)
// ensure the liquidity pool doesn't exist
_, found := k.GetPoolByReserveAccIndex(ctx, reserveAcc)
if found {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgCreatePool, "liquidity pool already exists"), nil, nil
}
balanceA := bk.GetBalance(ctx, simAccount.Address, denomA).Amount
if balanceA.IsNegative() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgCreatePool, "balanceA is negative"), nil, nil
}
balanceB := bk.GetBalance(ctx, simAccount.Address, denomB).Amount
if balanceB.IsNegative() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgCreatePool, "balanceB is negative"), nil, nil
}
poolCreator := account.GetAddress()
depositCoinA := randomDepositCoin(r, params.MinInitDepositAmount, denomA)
depositCoinB := randomDepositCoin(r, params.MinInitDepositAmount, denomB)
depositCoins := sdk.NewCoins(depositCoinA, depositCoinB)
// it will fail if the total reserve coin amount after the deposit is larger than the parameter
err = types.ValidateReserveCoinLimit(params.MaxReserveCoinAmount, depositCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgCreatePool, "can not exceed reserve coin limit amount"), nil, nil
}
msg := types.NewMsgCreatePool(poolCreator, types.DefaultPoolTypeID, depositCoins)
fees, err := randomFees(r, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgCreatePool, "unable to generate fees"), nil, err
}
txGen := liquidityparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
}
}
// SimulateMsgDepositWithinBatch generates a MsgDepositWithinBatch with random values
func SimulateMsgDepositWithinBatch(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) {
if len(k.GetAllPools(ctx)) == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDepositWithinBatch, "number of liquidity pools equals zero"), nil, nil
}
pool, ok := randomLiquidity(r, k, ctx)
if !ok {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDepositWithinBatch, "unable to pick liquidity pool"), nil, nil
}
reserveCoinDenomA := pool.ReserveCoinDenoms[0]
reserveCoinDenomB := pool.ReserveCoinDenoms[1]
// select random simulated account and mint reserve coins
// note that select the simulated account that has some balances of reserve coin denoms result in
// many failed transactions due to random accounts change after a creating pool.
simAccount := randomAccounts[r.Intn(len(randomAccounts))]
err := mintCoins(ctx, r, bk, simAccount, []string{reserveCoinDenomA, reserveCoinDenomB})
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDepositWithinBatch, "unable to mint and send coins"), nil, err
}
params := k.GetParams(ctx)
params.MaxReserveCoinAmount = GenMaxReserveCoinAmount(r)
k.SetParams(ctx, params)
account := ak.GetAccount(ctx, simAccount.Address)
spendable := bk.SpendableCoins(ctx, account.GetAddress())
depositor := account.GetAddress()
depositCoinA := randomDepositCoin(r, params.MinInitDepositAmount, reserveCoinDenomA)
depositCoinB := randomDepositCoin(r, params.MinInitDepositAmount, reserveCoinDenomB)
depositCoins := sdk.NewCoins(depositCoinA, depositCoinB)
reserveCoins := k.GetReserveCoins(ctx, pool)
// it will fail if the total reserve coin amount after the deposit is larger than the parameter
err = types.ValidateReserveCoinLimit(params.MaxReserveCoinAmount, reserveCoins.Add(depositCoinA, depositCoinB))
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDepositWithinBatch, "can not exceed reserve coin limit amount"), nil, nil
}
fees, err := randomFees(r, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDepositWithinBatch, "unable to generate fees"), nil, err
}
msg := types.NewMsgDepositWithinBatch(depositor, pool.Id, depositCoins)
txGen := liquidityparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
}
}
// SimulateMsgWithdrawWithinBatch generates a MsgWithdrawWithinBatch with random values.
func SimulateMsgWithdrawWithinBatch(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) {
if len(k.GetAllPools(ctx)) == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawWithinBatch, "number of liquidity pools equals zero"), nil, nil
}
pool, ok := randomLiquidity(r, k, ctx)
if !ok {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawWithinBatch, "unable to pick liquidity pool"), nil, nil
}
poolCoinDenom := pool.GetPoolCoinDenom()
// select random simulated account and mint reserve coins
// note that select the simulated account that has some balance of pool coin denom result in
// many failed transactions due to random accounts change after a creating pool.
simAccount := randomAccounts[r.Intn(len(randomAccounts))]
err := mintCoins(ctx, r, bk, simAccount, []string{poolCoinDenom})
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawWithinBatch, "unable to mint and send coins"), nil, err
}
// if simAccount.PrivKey == nil, then no account has pool coin denom balanace
if simAccount.PrivKey == nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawWithinBatch, "account private key is nil"), nil, nil
}
account := ak.GetAccount(ctx, simAccount.Address)
spendable := bk.SpendableCoins(ctx, account.GetAddress())
balance := bk.GetBalance(ctx, simAccount.Address, poolCoinDenom)
withdrawer := account.GetAddress()
withdrawCoin := randomWithdrawCoin(r, poolCoinDenom, balance.Amount)
msg := types.NewMsgWithdrawWithinBatch(withdrawer, pool.Id, withdrawCoin)
fees, err := randomFees(r, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawWithinBatch, "unable to generate fees"), nil, err
}
txGen := liquidityparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
}
}
// SimulateMsgSwapWithinBatch generates a MsgSwapWithinBatch with random values
func SimulateMsgSwapWithinBatch(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) {
if len(k.GetAllPools(ctx)) == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapWithinBatch, "number of liquidity pools equals zero"), nil, nil
}
pool, ok := randomLiquidity(r, k, ctx)
if !ok {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapWithinBatch, "unable to pick liquidity pool"), nil, nil
}
reserveCoinDenomA := pool.ReserveCoinDenoms[0]
reserveCoinDenomB := pool.ReserveCoinDenoms[1]
// select random simulated account and mint reserve coins
// note that select the simulated account that has some balances of reserve coin denoms result in
// many failed transactions due to random accounts change after a creating pool.
simAccount := randomAccounts[r.Intn(len(randomAccounts))]
err := mintCoins(ctx, r, bk, simAccount, []string{reserveCoinDenomA, reserveCoinDenomB})
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapWithinBatch, "unable to mint and send coins"), nil, err
}
account := ak.GetAccount(ctx, simAccount.Address)
spendable := bk.SpendableCoins(ctx, account.GetAddress())
swapRequester := account.GetAddress()
offerCoin := randomOfferCoin(r, k, ctx, pool, pool.ReserveCoinDenoms[0])
demandCoinDenom := pool.ReserveCoinDenoms[1]
orderPrice := randomOrderPrice(r)
swapFeeRate := GenSwapFeeRate(r)
// set randomly generated swap fee rate in params to prevent from miscalculation
params := k.GetParams(ctx)
params.SwapFeeRate = swapFeeRate
k.SetParams(ctx, params)
msg := types.NewMsgSwapWithinBatch(swapRequester, pool.Id, types.DefaultSwapTypeID, offerCoin, demandCoinDenom, orderPrice, swapFeeRate)
fees, err := randomFees(r, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapWithinBatch, "unable to generate fees"), nil, err
}
txGen := liquidityparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
}
}