/
tx.go
110 lines (87 loc) · 2.6 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
package cli
import (
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/commercionetwork/commercionetwork/x/commerciomint/internal/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"
)
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "CommercioMINT transactions subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
openCDPCmd(cdc),
closeCDPCmd(cdc),
)
return txCmd
}
func openCDPCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "open-cdp [amount]",
Short: "Opens a CDP for the given amount of ucommercio coins",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return openCDPCmdFunc(cmd, args, cdc)
},
}
cmd = client.PostCommands(cmd)[0]
return cmd
}
func openCDPCmdFunc(cmd *cobra.Command, args []string, cdc *codec.Codec) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
sender := cliCtx.GetFromAddress()
amount, err := sdk.ParseCoins(args[0])
if err != nil {
return err
}
cdpMsg := types.MsgOpenCdp{
Depositor: sender,
DepositedAmount: amount,
}
err = cdpMsg.ValidateBasic()
if err != nil {
return err
}
return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{cdpMsg})
}
func closeCDPCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "close-cdp [block-height]",
Short: "Closes a CDP for a user, opened at a given block height",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return closeCDPCmdFunc(cmd, args, cdc)
},
}
cmd = client.PostCommands(cmd)[0]
return cmd
}
func closeCDPCmdFunc(cmd *cobra.Command, args []string, cdc *codec.Codec) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
sender := cliCtx.GetFromAddress()
timestamp, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return fmt.Errorf("timestamp must be a number")
}
cdpMsg := types.MsgCloseCdp{
Signer: sender,
Timestamp: timestamp,
}
err = cdpMsg.ValidateBasic()
if err != nil {
return err
}
return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{cdpMsg})
}