-
Notifications
You must be signed in to change notification settings - Fork 16
/
list.go
119 lines (104 loc) · 3.52 KB
/
list.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
package simulation
import (
"fmt"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
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/jackalLabs/canine-chain/x/rns/keeper"
"github.com/jackalLabs/canine-chain/x/rns/types"
)
func SimulateMsgList(
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) {
// choosing a random account WITH registered domains
var simAccount simtypes.Account
var names []types.Names
simAccount, _ = simtypes.RandomAcc(r, accs)
// checking if any names are registered
exists := k.CheckExistence(ctx)
if !exists {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgList, "No domains registered yet"), nil, nil
}
for {
// finding all registered domain names
wctx := sdk.WrapSDKContext(ctx)
nReq := &types.QueryListOwnedNamesRequest{
Address: simAccount.Address.String(),
}
// requesting the domain names
regNames, err := k.ListOwnedNames(wctx, nReq)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgList, "Couldn't request names"), nil, nil
}
names = regNames.GetNames()
if names != nil {
break
}
simAccount, _ = simtypes.RandomAcc(r, accs)
}
// initializing the message
msg := &types.MsgList{
Creator: simAccount.Address.String(),
}
// choosing a random name that isn't already listed
unListed := make([]types.Names, 0)
for _, name := range names {
if _, found := k.GetForsale(ctx, name.Name+"."+name.Tld); !found && name.Name != "" {
unListed = append(unListed, name)
}
}
if len(unListed) < 1 {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "All owned domains are listed"), nil, nil
}
nameI := simtypes.RandIntBetween(r, 0, len(unListed))
tName := unListed[nameI]
// checking if the name is listable
if ctx.BlockHeight() > tName.Expires {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "Expired domain"), nil, nil
}
if tName.Locked > ctx.BlockHeight() {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "Can't list a free name"), nil, nil
}
// generating the fees
price := sdk.NewInt(0) // listing is free?
spendable := bk.SpendableCoins(ctx, simAccount.Address)
coins, hasNeg := spendable.SafeSub(sdk.NewCoins(sdk.NewCoin("ujkl", price)))
var fees sdk.Coins
if !hasNeg {
var err error
fees, err = simtypes.RandomFees(r, ctx, coins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, err
}
}
msg.Name = tName.Name + "." + tName.Tld
msg.Price = fmt.Sprint(simtypes.RandIntBetween(r, 0, 10000000)) + "ujkl"
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
ModuleName: types.ModuleName,
}
OpMsg, _, err := simulation.GenAndDeliverTx(txCtx, fees)
// creating a future op
fOp := simtypes.FutureOperation{
BlockHeight: int(ctx.BlockHeight()) + 15,
Op: SimulateMsgBid(ak, bk, k),
}
fOps := []simtypes.FutureOperation{fOp}
return OpMsg, fOps, err
}
}