/
tx.go
293 lines (255 loc) · 8.92 KB
/
tx.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
package cli
import (
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/percosis-labs/percosis/osmoutils/percocli"
"github.com/percosis-labs/percosis/v16/x/protorev/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewCmdTx returns the cli transaction commands for this module
func NewCmdTx() *cobra.Command {
txCmd := percocli.TxIndexCmd(types.ModuleName)
percocli.AddTxCmd(txCmd, CmdSetDeveloperAccount)
percocli.AddTxCmd(txCmd, CmdSetMaxPoolPointsPerTx)
percocli.AddTxCmd(txCmd, CmdSetMaxPoolPointsPerBlock)
txCmd.AddCommand(
CmdSetDeveloperHotRoutes().BuildCommandCustomFn(),
CmdSetPoolWeights().BuildCommandCustomFn(),
CmdSetBaseDenoms().BuildCommandCustomFn(),
CmdSetProtoRevAdminAccountProposal(),
CmdSetProtoRevEnabledProposal(),
)
return txCmd
}
// CmdSetDeveloperHotRoutes implements the command to set the protorev hot routes
func CmdSetDeveloperHotRoutes() *percocli.TxCliDesc {
desc := percocli.TxCliDesc{
Use: "set-hot-routes [path/to/routes.json]",
Short: "set the protorev hot routes",
Long: `Must provide a json file with all of the hot routes that will be set.
Sample json file:
[
{
"token_in": "ufury",
"token_out": "ibc/123...",
"arb_routes" : [
{
"trades": [
{
"pool": 1,
"token_in": "ufury",
"token_out": "uatom"
},
{
"pool": 2,
"token_in": "uatom",
"token_out": "ibc/123..."
},
{
"pool": 0,
"token_in": "ibc/123...",
"token_out": "ufury"
}
],
"step_size": 1000000
}
]
}
]
`,
Example: fmt.Sprintf(`$ %s tx protorev set-hot-routes routes.json --from mykey`, version.AppName),
NumArgs: 1,
ParseAndBuildMsg: BuildSetHotRoutesMsg,
}
return &desc
}
// CmdSetDeveloperAccount implements the command to set the protorev developer account
func CmdSetDeveloperAccount() (*percocli.TxCliDesc, *types.MsgSetDeveloperAccount) {
return &percocli.TxCliDesc{
Use: "set-developer-account [sdk.AccAddress]",
Short: "set the protorev developer account",
NumArgs: 1,
ParseAndBuildMsg: func(clientCtx client.Context, args []string, flags *pflag.FlagSet) (sdk.Msg, error) {
developer, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return nil, err
}
return &types.MsgSetDeveloperAccount{
DeveloperAccount: developer.String(),
Admin: clientCtx.GetFromAddress().String(),
}, nil
},
}, &types.MsgSetDeveloperAccount{}
}
// CmdSetMaxPoolPointsPerTx implements the command to set the max pool points per tx
func CmdSetMaxPoolPointsPerTx() (*percocli.TxCliDesc, *types.MsgSetMaxPoolPointsPerTx) {
return &percocli.TxCliDesc{
Use: "set-max-pool-points-per-tx [uint64]",
Short: "set the max pool points that can be consumed per tx",
NumArgs: 1,
ParseAndBuildMsg: func(clientCtx client.Context, args []string, flags *pflag.FlagSet) (sdk.Msg, error) {
maxPoolPointsPerTx, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return nil, err
}
return &types.MsgSetMaxPoolPointsPerTx{
MaxPoolPointsPerTx: maxPoolPointsPerTx,
Admin: clientCtx.GetFromAddress().String(),
}, nil
},
}, &types.MsgSetMaxPoolPointsPerTx{}
}
// CmdSetMaxPoolPointsPerBlock implements the command to set the max pool points per block
func CmdSetMaxPoolPointsPerBlock() (*percocli.TxCliDesc, *types.MsgSetMaxPoolPointsPerBlock) {
return &percocli.TxCliDesc{
Use: "set-max-pool-points-per-block [uint64]",
Short: "set the max pool points that can be consumed per block",
NumArgs: 1,
ParseAndBuildMsg: func(clientCtx client.Context, args []string, flags *pflag.FlagSet) (sdk.Msg, error) {
maxPoolPointsPerBlock, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return nil, err
}
return &types.MsgSetMaxPoolPointsPerBlock{
MaxPoolPointsPerBlock: maxPoolPointsPerBlock,
Admin: clientCtx.GetFromAddress().String(),
}, nil
},
}, &types.MsgSetMaxPoolPointsPerBlock{}
}
// CmdSetPoolWeights implements the command to set the pool weights used to estimate execution costs
func CmdSetPoolWeights() *percocli.TxCliDesc {
desc := percocli.TxCliDesc{
Use: "set-pool-weights [path/to/routes.json]",
Short: "set the protorev pool weights",
Long: `Must provide a json file with all the pool weights that will be set.
Sample json file:
{
"stable_weight" : 1,
"balancer_weight" : 1,
"concentrated_weight" : 1
}
`,
Example: fmt.Sprintf(`$ %s tx protorev set-pool-weights weights.json --from mykey`, version.AppName),
NumArgs: 1,
ParseAndBuildMsg: BuildSetPoolWeightsMsg,
}
return &desc
}
// CmdSetBaseDenoms implements the command to set the base denoms used in the highest liquidity method
func CmdSetBaseDenoms() *percocli.TxCliDesc {
desc := percocli.TxCliDesc{
Use: "set-base-denoms [path/to/denoms.json]",
Short: "set the protorev base denoms",
Long: `Must provide a json file with all the base denoms that will be set.
Sample json file:
[
{
"step_size" : 10000,
"denom" : "ufury"
},
{
"step_size" : 10000,
"denom" : "atom"
}
]
`,
Example: fmt.Sprintf(`$ %s tx protorev set-base-denoms denoms.json --from mykey`, version.AppName),
NumArgs: 1,
ParseAndBuildMsg: BuildSetBaseDenomsMsg,
}
return &desc
}
// CmdSetProtoRevAdminAccountProposal implements the command to submit a SetProtoRevAdminAccountProposal
func CmdSetProtoRevAdminAccountProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "set-admin-account-proposal [sdk.AccAddress]",
Args: cobra.ExactArgs(1),
Short: "submit a set protorev admin account proposal to set the admin account for x/protorev",
Example: fmt.Sprintf(`$ %s tx protorev set-protorev-admin-account perco123... --from mykey`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
createContent := func(title string, description string, args ...string) (govtypes.Content, error) {
return types.NewSetProtoRevAdminAccountProposal(title, description, args[0]), nil
}
return ProposalExecute(cmd, args, createContent)
},
}
cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagDescription, "", "description of proposal")
cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal")
flags.AddTxFlagsToCmd(cmd)
_ = cmd.MarkFlagRequired(cli.FlagTitle)
_ = cmd.MarkFlagRequired(cli.FlagDescription)
return cmd
}
// CmdSetProtoRevEnabledProposal implements the command to submit a SetProtoRevEnabledProposal
func CmdSetProtoRevEnabledProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "set-enabled-proposal [boolean]",
Args: cobra.ExactArgs(1),
Short: "submit a set protorev enabled proposal to enable or disable the protocol",
Example: fmt.Sprintf(`$ %s tx protorev set-protorev-enabled true --from mykey`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
createContent := func(title string, description string, args ...string) (govtypes.Content, error) {
res, err := strconv.ParseBool(args[0])
if err != nil {
return nil, err
}
content := types.NewSetProtoRevEnabledProposal(title, description, res)
return content, nil
}
return ProposalExecute(cmd, args, createContent)
},
}
cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagDescription, "", "description of proposal")
cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal")
flags.AddTxFlagsToCmd(cmd)
_ = cmd.MarkFlagRequired(cli.FlagTitle)
_ = cmd.MarkFlagRequired(cli.FlagDescription)
return cmd
}
// ProposalExecute is a helper function to execute a proposal command. It takes in a function to create the proposal content.
func ProposalExecute(cmd *cobra.Command, args []string, createContent func(title string, description string, args ...string) (govtypes.Content, error)) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
title, err := cmd.Flags().GetString(cli.FlagTitle)
if err != nil {
return err
}
description, err := cmd.Flags().GetString(cli.FlagDescription)
if err != nil {
return err
}
depositStr, err := cmd.Flags().GetString(cli.FlagDeposit)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(depositStr)
if err != nil {
return err
}
from := clientCtx.GetFromAddress()
content, err := createContent(title, description, args...)
if err != nil {
return err
}
msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from)
if err != nil {
return err
}
if err = msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
}