-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.go
163 lines (143 loc) · 4.62 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
package simulation
// DONTCOVER
import (
"math"
"math/big"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/furyanprotocol/v4-chain/protocol/lib"
"github.com/furyanprotocol/v4-chain/protocol/testutil/sim_helpers"
assettypes "github.com/furyanprotocol/v4-chain/protocol/x/assets/types"
"github.com/furyanprotocol/v4-chain/protocol/x/sending/keeper"
"github.com/furyanprotocol/v4-chain/protocol/x/sending/types"
satypes "github.com/furyanprotocol/v4-chain/protocol/x/subaccounts/types"
)
const (
opWeightMsgCreateTransfer = "op_weight_msg_create_transfer"
defaultWeightMsgCreateTransfer int = 100
)
var (
typeMsgCreateTransfer = sdk.MsgTypeURL(&types.MsgCreateTransfer{})
)
// WeightedOperations returns all the operations from the module with their respective weights.
func WeightedOperations(
appParams simtypes.AppParams,
jsonCdc codec.JSONCodec,
k keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
sk types.SubaccountsKeeper,
) simulation.WeightedOperations {
protoCdc := codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
operations := make([]simtypes.WeightedOperation, 0)
var weightMsgCreateTransfer int
appParams.GetOrGenerate(jsonCdc, opWeightMsgCreateTransfer, &weightMsgCreateTransfer, nil,
func(_ *rand.Rand) {
weightMsgCreateTransfer = defaultWeightMsgCreateTransfer
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgCreateTransfer,
SimulateMsgCreateTransfer(k, ak, bk, sk, protoCdc),
))
return operations
}
// SimulateMsgCreateTransfer generates a random MsgCreateTransfer.
func SimulateMsgCreateTransfer(
k keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
sk types.SubaccountsKeeper,
cdc *codec.ProtoCodec,
) simtypes.Operation {
return func(
r *rand.Rand,
app *baseapp.BaseApp,
ctx sdk.Context,
accs []simtypes.Account,
chainId string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
senderAccount, err := sk.GetRandomSubaccount(ctx, r)
// Return a no-op message if we don't have any accounts
if err != nil {
return simtypes.NoOpMsg(
types.ModuleName,
typeMsgCreateTransfer,
"Not enough subaccounts for transfer",
), nil, nil
}
bigNetCollateral, bigInitialMargin, _, err := sk.GetNetCollateralAndMarginRequirements(
ctx,
satypes.Update{
SubaccountId: *senderAccount.GetId(),
},
)
if err != nil {
panic(err)
}
// Select a different subaccount as the recipient.
recipientAccount, err := sk.GetRandomSubaccount(ctx, r)
if err != nil || *recipientAccount.GetId() == *senderAccount.GetId() {
// Return a no-op message if we don't have any accounts or choose the same account as the sender
// which should be quite rare.
return simtypes.NoOpMsg(
types.ModuleName,
typeMsgCreateTransfer,
"Not enough subaccounts for transfer",
), nil, nil
}
// Calculate the maximum amount that the receiver can receive without any integer overflow.
bigAmountReceivable := new(big.Int).Sub(
new(big.Int).SetUint64(math.MaxUint64),
recipientAccount.GetUsdcPosition(),
)
// Calculate the maximum amount that can be sent without making the subaccount under-collateralized.
bigAmountPayable := new(big.Int).Sub(bigNetCollateral, bigInitialMargin)
bigMaxAmountToSend := lib.BigMin(bigAmountPayable, bigAmountReceivable)
if bigMaxAmountToSend.Sign() <= 0 {
return simtypes.NoOpMsg(
types.ModuleName,
typeMsgCreateTransfer,
"Sender does not have enough balance or receiver might overflow",
), nil, nil
}
// Generate a random amount between [1, bigMaxAmountToSend].
// Rand generates a number in [0, bigMaxAmountToSend), add 1 to make it in [1, bigMaxAmountToSend].
amountToSend := new(big.Int).Add(
new(big.Int).Rand(r, bigMaxAmountToSend),
big.NewInt(1),
)
msg := &types.MsgCreateTransfer{
Transfer: &types.Transfer{
Sender: *senderAccount.GetId(),
Recipient: *recipientAccount.GetId(),
AssetId: assettypes.AssetUsdc.Id,
Amount: amountToSend.Uint64(),
},
}
proposer, _ := simtypes.FindAccount(accs, senderAccount.GetId().MustGetAccAddress())
opMsg, err := sim_helpers.GenerateAndDeliverTx(
r,
app,
ctx,
chainId,
cdc,
ak,
bk,
proposer,
types.ModuleName,
msg,
typeMsgCreateTransfer,
true, // fee does not apply when creating a transfer.
)
if err != nil {
panic(err) // panic to halt/fail simulation.
}
return opMsg, nil, nil
}
}