-
Notifications
You must be signed in to change notification settings - Fork 180
/
tx.go
268 lines (234 loc) · 7.9 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
package cli
import (
"bufio"
"fmt"
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/okex/okexchain/x/gov"
"github.com/spf13/cobra"
client "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
farmutils "github.com/okex/okexchain/x/farm/client/utils"
"github.com/okex/okexchain/x/farm/types"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
farmTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
SuggestionsMinimumDistance: 2,
}
farmTxCmd.AddCommand(client.PostCommands(
GetCmdCreatePool(cdc),
GetCmdDestroyPool(cdc),
GetCmdProvide(cdc),
GetCmdLock(cdc),
GetCmdUnlock(cdc),
GetCmdClaim(cdc),
)...)
return farmTxCmd
}
func GetCmdCreatePool(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-pool [pool-name] [min-lock-amount] [yield-token]",
Short: "create a farm pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Create a farm pool.
Example:
$ %s tx farm create-pool pool-eth-xxb 10eth xxb --from mykey
$ %s tx farm create-pool pool-ammswap_eth_usdk-xxb 10ammswap_eth_usdk xxb --from mykey
`, version.ClientName, version.ClientName),
),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)
poolName := args[0]
minLockAmount, err := sdk.ParseDecCoin(args[1])
if err != nil {
return err
}
yieldToken := args[2]
msg := types.NewMsgCreatePool(cliCtx.GetFromAddress(), poolName, minLockAmount, yieldToken)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}
func GetCmdDestroyPool(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "destroy-pool [pool-name]",
Short: "destroy a farm pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Destroy a farm pool.
Example:
$ %s tx farm destroy-pool pool-eth-xxb --from mykey
`, version.ClientName),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)
poolName := args[0]
msg := types.NewMsgDestroyPool(cliCtx.GetFromAddress(), poolName)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}
func GetCmdProvide(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "provide [pool-name] [amount] [yield-per-block] [start-height-to-yield]",
Short: "provide a number of yield tokens into a pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Provide a number of yield tokens into a pool.
Example:
$ %s tx farm provide pool-eth-xxb 1000xxb 5 10000 --from mykey
`, version.ClientName),
),
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)
amount, err := sdk.ParseDecCoin(args[1])
if err != nil {
return err
}
yieldPerBlock, err := sdk.NewDecFromStr(args[2])
if err != nil {
return err
}
startHeightToYield, err := strconv.ParseInt(args[3], 10, 64)
if err != nil {
return err
}
poolName := args[0]
msg := types.NewMsgProvide(poolName, cliCtx.GetFromAddress(), amount, yieldPerBlock, startHeightToYield)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}
func GetCmdLock(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "lock [pool-name] [amount]",
Short: "lock a number of tokens for yield farming",
Long: strings.TrimSpace(
fmt.Sprintf(`Lock a number of tokens for yield farming.
Example:
$ %s tx farm lock pool-eth-xxb 5eth --from mykey
`, version.ClientName),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)
amount, err := sdk.ParseDecCoin(args[1])
if err != nil {
return err
}
poolName := args[0]
msg := types.NewMsgLock(poolName, cliCtx.GetFromAddress(), amount)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}
func GetCmdUnlock(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "unlock [pool-name] [amount]",
Short: "unlock a number of tokens",
Long: strings.TrimSpace(
fmt.Sprintf(`Unlock a number of tokens.
Example:
$ %s tx farm unlock pool-eth-xxb 1eth --from mykey
`, version.ClientName),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)
amount, err := sdk.ParseDecCoin(args[1])
if err != nil {
return err
}
poolName := args[0]
msg := types.NewMsgUnlock(poolName, cliCtx.GetFromAddress(), amount)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}
func GetCmdClaim(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "claim [pool-name]",
Short: "claim yield farming rewards",
Long: strings.TrimSpace(
fmt.Sprintf(`Claim yield farming rewards.
Example:
$ %s tx farm claim --from mykey
`, version.ClientName),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)
poolName := args[0]
msg := types.NewMsgClaim(poolName, cliCtx.GetFromAddress())
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}
// GetCmdManageWhiteListProposal implements a command handler for submitting a farm manage white list proposal transaction
func GetCmdManageWhiteListProposal(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "manage-white-list [proposal-file]",
Args: cobra.ExactArgs(1),
Short: "Submit a manage white list proposal",
Long: strings.TrimSpace(
fmt.Sprintf(`Submit a manage white list proposal along with an initial deposit.
The proposal details must be supplied via a JSON file.
Example:
$ %s tx gov submit-proposal manage-white-list <path/to/proposal.json> --from=<key_or_address>
Where proposal.json contains:
{
"title": "manage white list with a pool name",
"description": "add a pool name into the white list",
"pool_name": "pool-eth-xxb",
"is_added": true,
"deposit": [
{
"denom": "%s",
"amount": "100"
}
]
}
`, version.ClientName, sdk.DefaultBondDenom,
)),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposal, err := farmutils.ParseManageWhiteListProposalJSON(cdc, args[0])
if err != nil {
return err
}
from := cliCtx.GetFromAddress()
content := types.NewManageWhiteListProposal(proposal.Title, proposal.Description, proposal.PoolName, proposal.IsAdded)
msg := gov.NewMsgSubmitProposal(content, proposal.Deposit, from)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}