-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
245 lines (218 loc) · 6.47 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
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"
"github.com/cosmos/cosmos-sdk/version"
"github.com/furya-official/mage/x/cdp/types"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
cdpTxCmd := &cobra.Command{
Use: "cdp",
Short: "cdp transactions subcommands",
}
cmds := []*cobra.Command{
GetCmdCreateCdp(),
GetCmdDeposit(),
GetCmdWithdraw(),
GetCmdDraw(),
GetCmdRepay(),
GetCmdLiquidate(),
}
for _, cmd := range cmds {
flags.AddTxFlagsToCmd(cmd)
}
cdpTxCmd.AddCommand(cmds...)
return cdpTxCmd
}
// GetCmdCreateCdp returns the command handler for creating a cdp
func GetCmdCreateCdp() *cobra.Command {
return &cobra.Command{
Use: "create [collateral] [debt] [collateral-type]",
Short: "create a new cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Create a new cdp, depositing some collateral and drawing some debt.
Example:
$ %s tx %s create 10000000uatom 1000usdx atom-a --from myKeyName
`, version.AppName, types.ModuleName)),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
collateral, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
}
debt, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
msg := types.NewMsgCreateCDP(clientCtx.GetFromAddress(), collateral, debt, args[2])
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
}
// GetCmdDeposit cli command for depositing to a cdp.
func GetCmdDeposit() *cobra.Command {
return &cobra.Command{
Use: "deposit [owner-addr] [collateral] [collateral-type]",
Short: "deposit collateral to an existing cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Add collateral to an existing cdp.
Example:
$ %s tx %s deposit mage15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom atom-a --from myKeyName
`, version.AppName, types.ModuleName)),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
collateral, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
owner, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
msg := types.NewMsgDeposit(owner, clientCtx.GetFromAddress(), collateral, args[2])
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
}
// GetCmdWithdraw cli command for withdrawing from a cdp.
func GetCmdWithdraw() *cobra.Command {
return &cobra.Command{
Use: "withdraw [owner-addr] [collateral] [collateral-type]",
Short: "withdraw collateral from an existing cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Remove collateral from an existing cdp.
Example:
$ %s tx %s withdraw mage15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw 10000000uatom atom-a --from myKeyName
`, version.AppName, types.ModuleName)),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
collateral, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
owner, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
msg := types.NewMsgWithdraw(owner, clientCtx.GetFromAddress(), collateral, args[2])
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
}
// GetCmdDraw cli command for depositing to a cdp.
func GetCmdDraw() *cobra.Command {
return &cobra.Command{
Use: "draw [collateral-type] [debt]",
Short: "draw debt off an existing cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Create debt in an existing cdp and send the newly minted asset to your account.
Example:
$ %s tx %s draw atom-a 1000usdx --from myKeyName
`, version.AppName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
debt, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
msg := types.NewMsgDrawDebt(clientCtx.GetFromAddress(), args[0], debt)
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
}
// GetCmdRepay cli command for depositing to a cdp.
func GetCmdRepay() *cobra.Command {
return &cobra.Command{
Use: "repay [collateral-name] [debt]",
Short: "repay debt to an existing cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Cancel out debt in an existing cdp.
Example:
$ %s tx %s repay atom-a 1000usdx --from myKeyName
`, version.AppName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
payment, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
msg := types.NewMsgRepayDebt(clientCtx.GetFromAddress(), args[0], payment)
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
}
// GetCmdLiquidate cli command for liquidating a cdp.
func GetCmdLiquidate() *cobra.Command {
return &cobra.Command{
Use: "liquidate [cdp-owner-address] [collateral-type]",
Short: "liquidate a cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Liquidate a cdp if it is below the required liquidation ratio
Example:
$ %s tx %s liquidate mage1y70y90wzmnf00e63efk2lycgqwepthdmyzsfzm btcb-a --from myKeyName
`, version.AppName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
msg := types.NewMsgLiquidate(clientCtx.GetFromAddress(), addr, args[1])
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
}