-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
205 lines (181 loc) · 5.59 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
package cli
import (
"fmt"
"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"
"github.com/cosmos/cosmos-sdk/version"
"github.com/furya-official/mage/x/issuance/types"
)
// GetTxCmd returns the transaction cli commands for the issuance module
func GetTxCmd() *cobra.Command {
issuanceTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "transaction commands for the issuance module",
}
cmds := []*cobra.Command{
GetCmdIssueTokens(),
GetCmdRedeemTokens(),
GetCmdBlockAddress(),
GetCmdUnblockAddress(),
GetCmdPauseAsset(),
}
for _, cmd := range cmds {
flags.AddTxFlagsToCmd(cmd)
}
issuanceTxCmd.AddCommand(cmds...)
return issuanceTxCmd
}
func GetCmdIssueTokens() *cobra.Command {
return &cobra.Command{
Use: "issue [tokens] [receiver]",
Short: "issue new tokens to the receiver address",
Long: "The asset owner issues new tokens that will be credited to the receiver address",
Example: fmt.Sprintf(`$ %s tx %s issue 20000000usdtoken mage15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw
`, version.AppName, types.ModuleName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
tokens, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
}
// We use the string later but here validate the acc address
receiver, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}
msg := types.NewMsgIssueTokens(cliCtx.GetFromAddress().String(), tokens, receiver.String())
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
}
func GetCmdRedeemTokens() *cobra.Command {
return &cobra.Command{
Use: "redeem [tokens]",
Short: "redeem tokens",
Long: "The asset owner redeems (burns) tokens, removing them from the circulating supply",
Example: fmt.Sprintf(`$ %s tx %s redeem 20000000usdtoken
`, version.AppName, types.ModuleName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
tokens, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
}
msg := types.NewMsgRedeemTokens(cliCtx.GetFromAddress().String(), tokens)
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
}
func GetCmdBlockAddress() *cobra.Command {
return &cobra.Command{
Use: "block [address] [denom]",
Short: "block an address for the input denom",
Long: "The asset owner blocks an address from holding coins of that denomination. Any tokens of the input denomination held by the address will be sent to the owner address",
Example: fmt.Sprintf(`$ %s tx %s block mage15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw usdtoken
`, version.AppName, types.ModuleName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
address, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
err = sdk.ValidateDenom(args[1])
if err != nil {
return err
}
msg := types.NewMsgBlockAddress(cliCtx.GetFromAddress().String(), args[1], address.String())
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
}
func GetCmdUnblockAddress() *cobra.Command {
return &cobra.Command{
Use: "unblock [address] [denom]",
Short: "unblock an address for the input denom",
Long: "The asset owner unblocks an address from holding coins of that denomination.",
Example: fmt.Sprintf(`$ %s tx %s unblock mage15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw usdtoken
`, version.AppName, types.ModuleName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
address, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
err = sdk.ValidateDenom(args[1])
if err != nil {
return err
}
msg := types.NewMsgUnblockAddress(cliCtx.GetFromAddress().String(), args[1], address.String())
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
}
func GetCmdPauseAsset() *cobra.Command {
return &cobra.Command{
Use: "set-pause-status [denom] [status]",
Short: "pause or unpause an asset",
Long: "The asset owner pauses or un-pauses the input asset, halting new issuance and redemption",
Example: fmt.Sprintf(`$ %s tx %s pause usdtoken true
`, version.AppName, types.ModuleName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
err = sdk.ValidateDenom(args[0])
if err != nil {
return err
}
var status bool
if args[1] == "true" {
status = true
} else if args[1] == "false" {
status = false
} else {
return fmt.Errorf(fmt.Sprintf("status must be true or false, got %s", args[1]))
}
msg := types.NewMsgSetPauseStatus(cliCtx.GetFromAddress().String(), args[0], status)
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
}