-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
453 lines (378 loc) · 14 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package cli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
bondsclient "github.com/furyfoundation/fury-blockchain/x/bonds/client"
"github.com/furyfoundation/fury-blockchain/x/bonds/types"
iidtypes "github.com/furyfoundation/fury-blockchain/x/iid/types"
)
func NewTxCmd() *cobra.Command {
bondsTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Bonds transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
bondsTxCmd.AddCommand(
NewCmdCreateBond(),
NewCmdEditBond(),
NewCmdSetNextAlpha(),
NewCmdUpdateBondState(),
NewCmdBuy(),
NewCmdSell(),
NewCmdSwap(),
NewCmdMakeOutcomePayment(),
NewCmdWithdrawShare(),
NewCmdWithdrawReserve(),
)
return bondsTxCmd
}
func NewCmdCreateBond() *cobra.Command {
cmd := &cobra.Command{
Use: "create-bond",
Short: "Create bond",
RunE: func(cmd *cobra.Command, args []string) (err error) {
_token, _ := cmd.Flags().GetString(FlagToken)
_name, _ := cmd.Flags().GetString(FlagName)
_description, _ := cmd.Flags().GetString(FlagDescription)
_functionType, _ := cmd.Flags().GetString(FlagFunctionType)
_functionParameters, _ := cmd.Flags().GetString(FlagFunctionParameters)
_reserveTokens, _ := cmd.Flags().GetString(FlagReserveTokens)
_txFeePercentage, _ := cmd.Flags().GetString(FlagTxFeePercentage)
_exitFeePercentage, _ := cmd.Flags().GetString(FlagExitFeePercentage)
_feeAddress, _ := cmd.Flags().GetString(FlagFeeAddress)
_reserveWithdrawalAddress, _ := cmd.Flags().GetString(FlagReserveWithdrawalAddress)
_maxSupply, _ := cmd.Flags().GetString(FlagMaxSupply)
_orderQuantityLimits, _ := cmd.Flags().GetString(FlagOrderQuantityLimits)
_sanityRate, _ := cmd.Flags().GetString(FlagSanityRate)
_sanityMarginPercentage, _ := cmd.Flags().GetString(FlagSanityMarginPercentage)
_allowSells, _ := cmd.Flags().GetBool(FlagAllowSells)
_allowReserveWithdrawals, _ := cmd.Flags().GetBool(FlagAllowReserveWithdrawals)
_alphaBond, _ := cmd.Flags().GetBool(FlagAlphaBond)
_batchBlocks, _ := cmd.Flags().GetString(FlagBatchBlocks)
_outcomePayment, _ := cmd.Flags().GetString(FlagOutcomePayment)
_bondDid, _ := cmd.Flags().GetString(FlagBondDid)
_creatorDid, _ := cmd.Flags().GetString(FlagCreatorDid)
_controllerDid, _ := cmd.Flags().GetString(FlagControllerDid)
_oracleDid, _ := cmd.Flags().GetString(FlagOracleDid)
// Parse function parameters
functionParams, err := bondsclient.ParseFunctionParams(_functionParameters)
if err != nil {
return fmt.Errorf(err.Error())
}
// Parse reserve tokens
reserveTokens := strings.Split(_reserveTokens, ",")
// Parse tx fee percentage
txFeePercentage, err := sdk.NewDecFromStr(_txFeePercentage)
if err != nil {
return sdkerrors.Wrap(types.ErrArgumentMissingOrNonFloat, "tx fee percentage")
}
// Parse exit fee percentage
exitFeePercentage, err := sdk.NewDecFromStr(_exitFeePercentage)
if err != nil {
return sdkerrors.Wrap(types.ErrArgumentMissingOrNonFloat, "exit fee percentage")
}
// Parse fee address
feeAddress, err := sdk.AccAddressFromBech32(_feeAddress)
if err != nil {
return err
}
// Parse reserve withdrawal address
reserveWithdrawalAddress, err := sdk.AccAddressFromBech32(_reserveWithdrawalAddress)
if err != nil {
return err
}
// Parse max supply
maxSupply, err := sdk.ParseCoinNormalized(_maxSupply) //sdk.ParseCoin(_maxSupply)
if err != nil {
return err
}
// Parse order quantity limits
orderQuantityLimits, err := sdk.ParseCoinsNormalized(_orderQuantityLimits) //sdk.ParseCoins(_orderQuantityLimits)
if err != nil {
return err
}
// Parse sanity rate
sanityRate, err := sdk.NewDecFromStr(_sanityRate)
if err != nil {
return fmt.Errorf(err.Error())
}
// Parse sanity margin percentage
sanityMarginPercentage, err := sdk.NewDecFromStr(_sanityMarginPercentage)
if err != nil {
return fmt.Errorf(err.Error())
}
// Parse batch blocks
batchBlocks, err := sdk.ParseUint(_batchBlocks)
if err != nil {
return sdkerrors.Wrap(types.ErrArgumentMissingOrNonUInteger, "max batch blocks")
}
// Parse outcome payment
var outcomePayment sdk.Int
if len(_outcomePayment) == 0 {
outcomePayment = sdk.ZeroInt()
} else {
var ok bool
outcomePayment, ok = sdk.NewIntFromString(_outcomePayment)
if !ok {
return sdkerrors.Wrap(types.ErrArgumentMustBeInteger, "outcome payment")
}
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgCreateBond(_token, _name, _description,
iidtypes.DIDFragment(_creatorDid), iidtypes.DIDFragment(_controllerDid), iidtypes.DIDFragment(_oracleDid), _functionType, functionParams,
reserveTokens, txFeePercentage, exitFeePercentage, feeAddress,
reserveWithdrawalAddress, maxSupply, orderQuantityLimits,
sanityRate, sanityMarginPercentage, _allowSells,
_allowReserveWithdrawals, _alphaBond, batchBlocks,
outcomePayment, _bondDid, clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().AddFlagSet(fsBondCreate)
_ = cmd.MarkFlagRequired(FlagToken)
_ = cmd.MarkFlagRequired(FlagName)
_ = cmd.MarkFlagRequired(FlagDescription)
_ = cmd.MarkFlagRequired(FlagFunctionType)
_ = cmd.MarkFlagRequired(FlagFunctionParameters)
_ = cmd.MarkFlagRequired(FlagReserveTokens)
_ = cmd.MarkFlagRequired(FlagTxFeePercentage)
_ = cmd.MarkFlagRequired(FlagExitFeePercentage)
_ = cmd.MarkFlagRequired(FlagFeeAddress)
_ = cmd.MarkFlagRequired(FlagReserveWithdrawalAddress)
_ = cmd.MarkFlagRequired(FlagMaxSupply)
_ = cmd.MarkFlagRequired(FlagOrderQuantityLimits)
_ = cmd.MarkFlagRequired(FlagSanityRate)
_ = cmd.MarkFlagRequired(FlagSanityMarginPercentage)
//_ = cmd.MarkFlagRequired(FlagAllowSells) // If not specified, assume False
//_ = cmd.MarkFlagRequired(FlagAllowReserveWithdrawals) // If not specified, assume False
_ = cmd.MarkFlagRequired(FlagBatchBlocks)
// _ = cmd.MarkFlagRequired(FlagOutcomePayment) // Optional
_ = cmd.MarkFlagRequired(FlagBondDid)
_ = cmd.MarkFlagRequired(FlagCreatorDid)
_ = cmd.MarkFlagRequired(FlagControllerDid)
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewCmdEditBond() *cobra.Command {
cmd := &cobra.Command{
Use: "edit-bond",
Short: "Edit bond",
RunE: func(cmd *cobra.Command, args []string) error {
_name, _ := cmd.Flags().GetString(FlagName)
_description, _ := cmd.Flags().GetString(FlagDescription)
_orderQuantityLimits, _ := cmd.Flags().GetString(FlagOrderQuantityLimits)
_sanityRate, _ := cmd.Flags().GetString(FlagSanityRate)
_sanityMarginPercentage, _ := cmd.Flags().GetString(FlagSanityMarginPercentage)
_bondDid, _ := cmd.Flags().GetString(FlagBondDid)
_editorDid, _ := cmd.Flags().GetString(FlagEditorDid)
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgEditBond(_name, _description, _orderQuantityLimits,
_sanityRate, _sanityMarginPercentage, iidtypes.DIDFragment(_editorDid), _bondDid, clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().AddFlagSet(fsBondEdit)
_ = cmd.MarkFlagRequired(FlagBondDid)
_ = cmd.MarkFlagRequired(FlagEditorDid)
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewCmdSetNextAlpha() *cobra.Command {
cmd := &cobra.Command{
Use: "set-next-alpha [new-alpha] [bond-did] [editor-did]",
Example: "set-next-alpha 0.5 U7GK8p8rVhJMKhBVRCJJ8c <editor-fury-did>",
Short: "Edit a bond's alpha parameter",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
_alpha := args[0]
_bondDid := args[1]
_editorDid := args[2]
// Parse alpha
alpha, err := sdk.NewDecFromStr(_alpha)
if err != nil {
return sdkerrors.Wrap(types.ErrArgumentMissingOrNonFloat, "alpha")
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgSetNextAlpha(alpha, iidtypes.DIDFragment(_editorDid), _bondDid, clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewCmdUpdateBondState() *cobra.Command {
cmd := &cobra.Command{
Use: "update-bond-state [new-state] [bond-did] [editor-did]",
Example: "update-bond-state SETTLE U7GK8p8rVhJMKhBVRCJJ8c <editor-fury-did>",
Short: "Edit a bond's current state",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
_state := args[0]
_bondDid := args[1]
_editorDid := args[2]
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgUpdateBondState(types.BondState(_state), iidtypes.DIDFragment(_editorDid), _bondDid, clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewCmdBuy() *cobra.Command {
cmd := &cobra.Command{
Use: "buy [bond-token-with-amount] [max-prices] [bond-did] [buyer-did]",
Example: "" +
"buy 10abc 1000res1 U7GK8p8rVhJMKhBVRCJJ8c <buyer-fury-did>\n" +
"buy 10abc 1000res1,1000res2 U7GK8p8rVhJMKhBVRCJJ8c <buyer-fury-did>",
Short: "Buy from a bond",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
bondCoinWithAmount, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
}
maxPrices, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgBuy(
iidtypes.DIDFragment(args[3]), bondCoinWithAmount, maxPrices, args[2], clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewCmdSell() *cobra.Command {
cmd := &cobra.Command{
Use: "sell [bond-token-with-amount] [bond-did] [seller-did]",
Example: "sell 10abc U7GK8p8rVhJMKhBVRCJJ8c <seller-fury-did>",
Short: "Sell from a bond",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
bondCoinWithAmount, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgSell(iidtypes.DIDFragment(args[2]), bondCoinWithAmount, args[1], clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewCmdSwap() *cobra.Command {
cmd := &cobra.Command{
Use: "swap [from-amount] [from-token] [to-token] [bond-did] [swapper-did]",
Example: "" +
"swap 100 res1 res2 U7GK8p8rVhJMKhBVRCJJ8c <swapper-fury-did>\n" +
"swap 100 res2 res1 U7GK8p8rVhJMKhBVRCJJ8c <swapper-fury-did>",
Short: "Perform a swap between two tokens",
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
// Check that from amount and token can be parsed to a coin
from, err := bondsclient.ParseTwoPartCoin(args[0], args[1])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgSwap(iidtypes.DIDFragment(args[4]), from, args[2], args[3], clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewCmdMakeOutcomePayment() *cobra.Command {
cmd := &cobra.Command{
Use: "make-outcome-payment [bond-did] [amount] [sender-did]",
Example: "make-outcome-payment U7GK8p8rVhJMKhBVRCJJ8c 100 <sender-fury-did>",
Short: "Make an outcome payment to a bond",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
amount, ok := sdk.NewIntFromString(args[1])
if !ok {
return sdkerrors.Wrap(types.ErrArgumentMustBeInteger, "outcome payment")
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgMakeOutcomePayment(iidtypes.DIDFragment(args[2]), amount, args[0], clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewCmdWithdrawShare() *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-share [bond-did] [recipient-did]",
Example: "withdraw-share U7GK8p8rVhJMKhBVRCJJ8c <recipient-fury-did>",
Short: "Withdraw share from a bond that is in settlement state",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgWithdrawShare(iidtypes.DIDFragment(args[1]), args[0], clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewCmdWithdrawReserve() *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-reserve [bond-did] [amount] [withdrawer-did]",
Example: "withdraw-reserve U7GK8p8rVhJMKhBVRCJJ8c 1000res <withdrawer-fury-did>",
Short: "Withdraw reserve from a bond",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
amount, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgWithdrawReserve(iidtypes.DIDFragment(args[2]), amount, args[0], clientCtx.GetFromAddress().String())
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}