/
tx.go
149 lines (119 loc) · 4.37 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
package cli
import (
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/jpthor/cosmos-swap/x/swapservice/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)
func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
swapserviceTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "swapservice transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: utils.ValidateCmd,
}
swapserviceTxCmd.AddCommand(client.PostCommands(
GetCmdSetPoolData(cdc),
GetCmdSetAccData(cdc),
GetCmdSetStakeData(cdc),
GetCmdSwap(cdc),
)...)
return swapserviceTxCmd
}
// GetCmdSetPoolData is the CLI command for sending a SetPoolData transaction
func GetCmdSetPoolData(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "set-pool [token name] [ticker]",
Short: "TODO: remove me",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}
msg := types.NewMsgSetPoolData(args[0], args[1], cliCtx.GetFromAddress())
err := msg.ValidateBasic()
if err != nil {
return err
}
cliCtx.PrintResponse = true
// return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, msgs)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
// GetCmdSetAccData is the CLI command for sending a SetAccData transaction
func GetCmdSetAccData(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "set-account [name] [ticker] [amount]",
Short: "Create a new account.",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}
msg := types.NewMsgSetAccData(args[0], args[1], args[2], cliCtx.GetFromAddress())
err := msg.ValidateBasic()
if err != nil {
return err
}
cliCtx.PrintResponse = true
// return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, msgs)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
// GetCmdSetStakeData is the CLI command for sending a SetStakeData transaction
func GetCmdSetStakeData(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "set-stake [name] [ticker] [atoms] [tokens]",
Short: "Stake coins into a pool",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}
msg := types.NewMsgSetStakeData(args[0], args[1], args[2], args[3], cliCtx.GetFromAddress())
err := msg.ValidateBasic()
if err != nil {
return err
}
cliCtx.PrintResponse = true
// return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, msgs)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
// GetCmdSwap is the CLI command for swapping tokens
func GetCmdSwap(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "set-swap [source] [target] [amount] [requester] [destination]",
Short: "Stake coins",
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}
msg := types.NewMsgSwap(args[0], args[1], args[2], args[3], args[4], cliCtx.GetFromAddress())
err := msg.ValidateBasic()
if err != nil {
return err
}
cliCtx.PrintResponse = true
// return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, msgs)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}