/
operations.go
200 lines (170 loc) · 6.1 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
package simulation
// DONTCOVER
import (
"math/rand"
"strings"
core "github.com/classic-terra/core/types"
"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"
"github.com/classic-terra/core/x/market/types"
)
// Simulation operation weights constants
const (
OpWeightMsgSwap = "op_weight_msg_swap" //#nosec
)
// 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,
ok types.OracleKeeper,
) simulation.WeightedOperations {
var weightMsgSwap int
appParams.GetOrGenerate(cdc, OpWeightMsgSwap, &weightMsgSwap, nil,
func(_ *rand.Rand) {
weightMsgSwap = simappparams.DefaultWeightMsgSend
},
)
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgSwap,
SimulateMsgSwap(ak, bk, ok),
),
}
}
// SimulateMsgSwap generates a MsgSwap with random values.
// nolint: funlen
func SimulateMsgSwap(
ak types.AccountKeeper,
bk types.BankKeeper,
ok types.OracleKeeper,
) 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)
account := ak.GetAccount(ctx, simAccount.Address)
spendable := bk.SpendableCoins(ctx, simAccount.Address)
fees, err := simtypes.RandomFees(r, ctx, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwap, "unable to generate fees"), nil, err
}
var whitelist []string
ok.IterateLunaExchangeRates(ctx, func(denom string, ex sdk.Dec) bool {
whitelist = append(whitelist, denom)
return false
})
var offerDenom string
var askDenom string
whitelistLen := len(whitelist)
if whitelistLen == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwap, "no available exchange rates"), nil, nil
}
if randVal := simtypes.RandIntBetween(r, 0, whitelistLen*2); randVal < whitelistLen {
offerDenom = core.MicroLunaDenom
askDenom = whitelist[randVal]
} else {
offerDenom = whitelist[randVal-whitelistLen]
askDenom = core.MicroLunaDenom
}
amount := simtypes.RandomAmount(r, spendable.AmountOf(offerDenom).Sub(fees.AmountOf(offerDenom)))
if amount.Equal(sdk.ZeroInt()) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwap, "not enough offer denom amount"), nil, nil
}
msg := types.NewMsgSwap(simAccount.Address, sdk.NewCoin(offerDenom, amount), askDenom)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenTx(
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.Deliver(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
}
}
// SimulateMsgSwapSend generates a MsgSwapSend with random values.
// nolint: funlen
func SimulateMsgSwapSend(
ak types.AccountKeeper,
bk types.BankKeeper,
ok types.OracleKeeper,
) 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)
receiverAccount, _ := simtypes.RandomAcc(r, accs)
account := ak.GetAccount(ctx, simAccount.Address)
spendable := bk.SpendableCoins(ctx, simAccount.Address)
fees, err := simtypes.RandomFees(r, ctx, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapSend, "unable to generate fees"), nil, err
}
var whitelist []string
ok.IterateLunaExchangeRates(ctx, func(denom string, ex sdk.Dec) bool {
whitelist = append(whitelist, denom)
return false
})
var offerDenom string
var askDenom string
whitelistLen := len(whitelist)
if whitelistLen == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapSend, "no available exchange rates"), nil, nil
}
if randVal := simtypes.RandIntBetween(r, 0, whitelistLen*2); randVal < whitelistLen {
offerDenom = core.MicroLunaDenom
askDenom = whitelist[randVal]
} else {
offerDenom = whitelist[randVal-whitelistLen]
askDenom = core.MicroLunaDenom
}
// Check send_enabled status of offer denom
if !bk.IsSendEnabledCoin(ctx, sdk.Coin{Denom: offerDenom, Amount: sdk.NewInt(1)}) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapSend, err.Error()), nil, nil
}
amount := simtypes.RandomAmount(r, spendable.AmountOf(offerDenom).Sub(fees.AmountOf(offerDenom)))
if amount.Equal(sdk.ZeroInt()) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapSend, "not enough offer denom amount"), nil, nil
}
msg := types.NewMsgSwapSend(simAccount.Address, receiverAccount.Address, sdk.NewCoin(offerDenom, amount), askDenom)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenTx(
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.Deliver(txGen.TxEncoder(), tx)
if err != nil {
if strings.Contains(err.Error(), "insufficient fee") {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "ignore tax error"), nil, nil
}
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
}
}