forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.go
281 lines (242 loc) · 9.24 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
//nolint:gosec // nolint because this file contains strings for testing
package simulation
import (
"math/rand"
"time"
osmo_simulation "github.com/MonOsmosis/osmosis/v10/x/simulation"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/MonOsmosis/osmosis/v10/x/gamm/pool-models/balancer"
"github.com/MonOsmosis/osmosis/v10/x/gamm/keeper"
"github.com/MonOsmosis/osmosis/v10/x/gamm/types"
"github.com/cosmos/cosmos-sdk/codec"
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"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
// Simulation operation weights constants.
const (
OpWeightMsgCreatePool = "op_weight_create_pool"
OpWeightMsgSwapExactAmountIn = "op_weight_swap_exact_amount_in"
OpWeightMsgSwapExactAmountOut = "op_weight_swap_exact_amount_out"
OpWeightMsgJoinPool = "op_weight_join_pool"
OpWeightMsgExitPool = "op_weight_exit_pool"
OpWeightMsgJoinSwapExternAmountIn = "op_weight_join_swap_extern_amount_in"
OpWeightMsgJoinSwapShareAmountOut = "op_weight_join_swap_share_amount_out"
OpWeightMsgExitSwapExternAmountOut = "op_weight_exit_swap_extern_amount_out"
OpWeightMsgExitSwapShareAmountIn = "op_weight_exit_swap_share_amount_in"
DefaultWeightMsgCreatePool int = 10
DefaultWeightMsgSwapExactAmountIn int = 25
DefaultWeightMsgSwapExactAmountOut int = 10
DefaultWeightMsgJoinPool int = 10
DefaultWeightMsgExitPool int = 10
DefaultWeightMsgJoinSwapExternAmountIn int = 10
DefaultWeightMsgJoinSwapShareAmountOut int = 10
DefaultWeightMsgExitSwapExternAmountOut int = 10
DefaultWeightMsgExitSwapShareAmountIn int = 10
)
// WeightedOperations returns all the operations from the module with their respective weights.
func WeightedOperations(
appParams simtypes.AppParams, cdc codec.JSONCodec, ak stakingTypes.AccountKeeper,
bk stakingTypes.BankKeeper, k keeper.Keeper,
) simulation.WeightedOperations {
// var (
// weightMsgCreatePool int
// weightMsgSwapExactAmountIn int
// )
// appParams.GetOrGenerate(cdc, OpWeightMsgCreatePool, &weightMsgCreatePool, nil,
// func(_ *rand.Rand) {
// weightMsgCreatePool = simappparams.DefaultWeightMsgCreateValidator
// weightMsgSwapExactAmountIn = simappparams.DefaultWeightMsgCreateValidator
// },
// )
return simulation.WeightedOperations{
// simulation.NewWeightedOperation(
// weightMsgCreatePool,
// SimulateMsgCreateBalancerPool(ak, bk, k),
// ),
// simulation.NewWeightedOperation(
// weightMsgSwapExactAmountIn,
// SimulateMsgSwapExactAmountIn(ak, bk, k),
// ),
}
}
func genFuturePoolGovernor(r *rand.Rand, addr sdk.Address, tokenList []string) string {
choice := r.Int31n(4)
if choice == 0 { // No governor
return ""
} else if choice == 1 { // Single address governor
return addr.String()
} else if choice == 2 { // LP token governor
return "1d"
} else { // Other token governor
token := tokenList[r.Intn(len(tokenList))]
return token + ",1d"
}
}
func genPoolAssets(r *rand.Rand, acct simtypes.Account, coins sdk.Coins) []balancer.PoolAsset {
// selecting random number between [2, Min(coins.Len, 6)]
numCoins := 2 + r.Intn(Min(coins.Len(), 6)-1)
denomIndices := r.Perm(coins.Len())
assets := []balancer.PoolAsset{}
for _, denomIndex := range denomIndices[:numCoins] {
denom := coins[denomIndex].Denom
amt, _ := simtypes.RandPositiveInt(r, coins[denomIndex].Amount.QuoRaw(100))
reserveAmt := sdk.NewCoin(denom, amt)
weight := sdk.NewInt(r.Int63n(9) + 1)
assets = append(assets, balancer.PoolAsset{Token: reserveAmt, Weight: weight})
}
return assets
}
func genBalancerPoolParams(r *rand.Rand, blockTime time.Time, assets []balancer.PoolAsset) balancer.PoolParams {
// swapFeeInt := int64(r.Intn(1e5))
// swapFee := sdk.NewDecWithPrec(swapFeeInt, 6)
exitFeeInt := int64(r.Intn(1e5))
exitFee := sdk.NewDecWithPrec(exitFeeInt, 6)
// TODO: Randomly generate LBP params
return balancer.PoolParams{
// SwapFee: swapFee,
SwapFee: sdk.ZeroDec(),
ExitFee: exitFee,
}
}
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func Max(x, y int) int {
if x > y {
return x
}
return y
}
// SimulateMsgCreateBalancerPool generates a MsgCreatePool with random values.
func SimulateMsgCreateBalancerPool(ak stakingTypes.AccountKeeper, bk stakingTypes.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)
simCoins := bk.SpendableCoins(ctx, simAccount.Address)
if simCoins.Len() <= 1 {
return simtypes.NoOpMsg(
types.ModuleName, balancer.TypeMsgCreateBalancerPool, "Account doesn't have 2 different coin types"), nil, nil
}
poolAssets := genPoolAssets(r, simAccount, simCoins)
poolParams := genBalancerPoolParams(r, ctx.BlockTime(), poolAssets)
// Commented out as genFuturePoolGovernor() panics on empty denom slice.
// TODO: fix and provide proper denom types.
// TODO: Replace []string{} with all token types on chain.
// futurePoolGovernor := genFuturePoolGovernor(r, simAccount.Address, []string{})
balances := bk.GetAllBalances(ctx, simAccount.Address)
denoms := make([]string, len(balances))
for i := range balances {
denoms[i] = balances[i].Denom
}
// set the pool params to set the pool creation fee to dust amount of denom
k.SetParams(ctx, types.Params{
PoolCreationFee: sdk.Coins{sdk.NewInt64Coin(denoms[0], 1)},
})
msg := &balancer.MsgCreateBalancerPool{
Sender: simAccount.Address.String(),
PoolParams: &poolParams,
PoolAssets: poolAssets,
FuturePoolGovernor: "",
}
spentCoins := balancer.PoolAssetsCoins(poolAssets)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
return osmo_simulation.GenAndDeliverTxWithRandFees(
r, app, txGen, msg, spentCoins, ctx, simAccount, ak, bk, types.ModuleName)
}
}
// SimulateMsgSwapExactAmountIn generates a MsgSwapExactAmountIn with random values
// TODO: Change to use expected keepers
// func SimulateMsgSwapExactAmountIn(ak stakingTypes.AccountKeeper, bk stakingTypes.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)
// simCoins := bk.SpendableCoins(ctx, simAccount.Address)
// if simCoins.Len() <= 0 {
// return simtypes.NoOpMsg(
// types.ModuleName, types.TypeMsgSwapExactAmountIn, "Account have no coin"), nil, nil
// }
// coin := simCoins[r.Intn(len(simCoins))]
// // Use under 0.5% of the account balance
// // TODO: Make like a 33% probability of using a ton of balance
// amt, _ := simtypes.RandPositiveInt(r, coin.Amount.QuoRaw(200))
// tokenIn := sdk.Coin{
// Denom: coin.Denom,
// Amount: amt,
// }
// routes, _ := RandomExactAmountInRoute(ctx, r, k, tokenIn)
// if len(routes) == 0 {
// return simtypes.NoOpMsg(
// types.ModuleName, types.TypeMsgSwapExactAmountIn, "No pool exist"), nil, nil
// }
// msg := types.MsgSwapExactAmountIn{
// Sender: simAccount.Address.String(),
// Routes: routes,
// TokenIn: tokenIn,
// TokenOutMinAmount: sdk.OneInt(),
// // TokenOutMinAmount: tokenOutMin.QuoRaw(2),
// }
// txGen := simappparams.MakeTestEncodingConfig().TxConfig
// return osmo_simulation.GenAndDeliverTxWithRandFees(
// r, app, txGen, &msg, sdk.Coins{tokenIn}, ctx, simAccount, ak, bk, types.ModuleName)
// }
// }
// func RandomExactAmountInRoute(ctx sdk.Context, r *rand.Rand, k keeper.Keeper, tokenIn sdk.Coin) (res []types.SwapAmountInRoute, tokenOut sdk.Coin) {
// routeLen := r.Intn(1) + 1
// allpools, err := k.GetPools(ctx)
// if err != nil {
// panic(err)
// }
// pools := []types.PoolI{}
// for _, pool := range allpools {
// if pool.IsActive(ctx.BlockTime()) {
// pools = append(pools, pool)
// }
// }
// if len(pools) == 0 {
// return
// }
// res = []types.SwapAmountInRoute{}
// for i := 0; i < routeLen; i++ {
// // randomly selected pool might not include the source token, retry
// for retry := 0; retry < 10; retry++ {
// pool := pools[r.Intn(len(pools))]
// inAsset, err := pool.GetPoolAsset(tokenIn.Denom)
// if err != nil {
// continue
// }
// if inAsset.Token.Amount.LT(tokenIn.Amount) {
// continue
// }
// for _, asset := range pool.GetAllPoolAssets() {
// if asset.Token.Denom == tokenIn.Denom {
// continue
// }
// res = append(res, types.SwapAmountInRoute{
// PoolId: pool.GetId(),
// TokenOutDenom: asset.Token.Denom,
// })
// sp, err := k.CalculateSpotPriceWithSwapFee(ctx, pool.GetId(), tokenIn.Denom, asset.Token.Denom)
// if err != nil {
// panic(err)
// }
// amt := tokenIn.Amount.ToDec().Quo(sp).RoundInt()
// tokenIn = sdk.Coin{
// Denom: asset.Token.Denom,
// Amount: amt,
// }
// break
// }
// break
// }
// }
// tokenOut = tokenIn
// return
// }