8 "github.com/spf13/cobra"
9 jww "github.com/spf13/jwalterweatherman"
11 "github.com/bytom/api"
12 "github.com/bytom/blockchain/txbuilder"
13 "github.com/bytom/protocol/bc/types"
14 "github.com/bytom/util"
18 buildTransactionCmd.PersistentFlags().StringVarP(&buildType, "type", "t", "", "transaction type, valid types: 'issue', 'spend'")
19 buildTransactionCmd.PersistentFlags().StringVarP(&receiverProgram, "receiver", "r", "", "program of receiver")
20 buildTransactionCmd.PersistentFlags().StringVarP(&address, "address", "a", "", "address of receiver")
21 buildTransactionCmd.PersistentFlags().StringVarP(&btmGas, "gas", "g", "20000000", "program of receiver")
22 buildTransactionCmd.PersistentFlags().BoolVar(&pretty, "pretty", false, "pretty print json result")
23 buildTransactionCmd.PersistentFlags().BoolVar(&alias, "alias", false, "use alias build transaction")
25 signTransactionCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "password of the account which sign these transaction(s)")
26 signTransactionCmd.PersistentFlags().BoolVar(&pretty, "pretty", false, "pretty print json result")
28 signSubTransactionCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "password of the account which sign these transaction(s)")
30 listTransactionsCmd.PersistentFlags().StringVar(&txID, "id", "", "transaction id")
31 listTransactionsCmd.PersistentFlags().StringVar(&account, "account_id", "", "account id")
32 listTransactionsCmd.PersistentFlags().BoolVar(&detail, "detail", false, "list transactions details")
48 var buildIssueReqFmt = `
50 {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":%s, "account_id": "%s"},
51 {"type": "issue", "asset_id": "%s", "amount": %s},
52 {"type": "control_address", "asset_id": "%s", "amount": %s, "address": "%s"}
55 var buildIssueReqFmtByAlias = `
57 {"type": "spend_account", "asset_alias": "BTM", "amount":%s, "account_alias": "%s"},
58 {"type": "issue", "asset_alias": "%s", "amount": %s},
59 {"type": "control_address", "asset_alias": "%s", "amount": %s, "address": "%s"}
62 var buildSpendReqFmt = `
64 {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":%s, "account_id": "%s"},
65 {"type": "spend_account", "asset_id": "%s","amount": %s,"account_id": "%s"},
66 {"type": "control_receiver", "asset_id": "%s", "amount": %s, "receiver":{"control_program": "%s","expires_at":"2017-12-28T12:52:06.78309768+08:00"}}
69 var buildSpendReqFmtByAlias = `
71 {"type": "spend_account", "asset_alias": "BTM", "amount":%s, "account_alias": "%s"},
72 {"type": "spend_account", "asset_alias": "%s","amount": %s,"account_alias": "%s"},
73 {"type": "control_receiver", "asset_alias": "%s", "amount": %s, "receiver":{"control_program": "%s","expires_at":"2017-12-28T12:52:06.78309768+08:00"}}
76 var buildRetireReqFmt = `
78 {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":%s, "account_id": "%s"},
79 {"type": "spend_account", "asset_id": "%s","amount": %s,"account_id": "%s"},
80 {"type": "retire", "asset_id": "%s","amount": %s,"account_id": "%s"}
83 var buildRetireReqFmtByAlias = `
85 {"type": "spend_account", "asset_alias": "BTM", "amount":%s, "account_alias": "%s"},
86 {"type": "spend_account", "asset_alias": "%s","amount": %s,"account_alias": "%s"},
87 {"type": "retire", "asset_alias": "%s","amount": %s,"account_alias": "%s"}
90 var buildControlAddressReqFmt = `
92 {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":%s, "account_id": "%s"},
93 {"type": "spend_account", "asset_id": "%s","amount": %s,"account_id": "%s"},
94 {"type": "control_address", "asset_id": "%s", "amount": %s,"address": "%s"}
97 var buildControlAddressReqFmtByAlias = `
99 {"type": "spend_account", "asset_alias": "BTM", "amount":%s, "account_alias": "%s"},
100 {"type": "spend_account", "asset_alias": "%s","amount": %s, "account_alias": "%s"},
101 {"type": "control_address", "asset_alias": "%s", "amount": %s,"address": "%s"}
104 var buildTransactionCmd = &cobra.Command{
105 Use: "build-transaction <accountID|alias> <assetID|alias> <amount>",
106 Short: "Build one transaction template,default use account id and asset id",
107 Args: cobra.RangeArgs(3, 4),
108 PreRun: func(cmd *cobra.Command, args []string) {
109 cmd.MarkFlagRequired("type")
110 if buildType == "spend" {
111 cmd.MarkFlagRequired("receiver")
114 Run: func(cmd *cobra.Command, args []string) {
115 var buildReqStr string
116 accountInfo := args[0]
122 buildReqStr = fmt.Sprintf(buildIssueReqFmtByAlias, btmGas, accountInfo, assetInfo, amount, assetInfo, amount, address)
125 buildReqStr = fmt.Sprintf(buildIssueReqFmt, btmGas, accountInfo, assetInfo, amount, assetInfo, amount, address)
128 buildReqStr = fmt.Sprintf(buildSpendReqFmtByAlias, btmGas, accountInfo, assetInfo, amount, accountInfo, assetInfo, amount, receiverProgram)
131 buildReqStr = fmt.Sprintf(buildSpendReqFmt, btmGas, accountInfo, assetInfo, amount, accountInfo, assetInfo, amount, receiverProgram)
134 buildReqStr = fmt.Sprintf(buildRetireReqFmtByAlias, btmGas, accountInfo, assetInfo, amount, accountInfo, assetInfo, amount, accountInfo)
137 buildReqStr = fmt.Sprintf(buildRetireReqFmt, btmGas, accountInfo, assetInfo, amount, accountInfo, assetInfo, amount, accountInfo)
140 buildReqStr = fmt.Sprintf(buildControlAddressReqFmtByAlias, btmGas, accountInfo, assetInfo, amount, accountInfo, assetInfo, amount, address)
143 buildReqStr = fmt.Sprintf(buildControlAddressReqFmt, btmGas, accountInfo, assetInfo, amount, accountInfo, assetInfo, amount, address)
145 jww.ERROR.Println("Invalid transaction template type")
146 os.Exit(util.ErrLocalExe)
149 var buildReq api.BuildRequest
150 if err := json.Unmarshal([]byte(buildReqStr), &buildReq); err != nil {
151 jww.ERROR.Println(err)
152 os.Exit(util.ErrLocalExe)
155 data, exitCode := util.ClientCall("/build-transaction", &buildReq)
156 if exitCode != util.Success {
165 dataMap, ok := data.(map[string]interface{})
167 jww.ERROR.Println("invalid type assertion")
168 os.Exit(util.ErrLocalParse)
171 rawTemplate, err := json.Marshal(dataMap)
173 jww.ERROR.Println(err)
174 os.Exit(util.ErrLocalParse)
177 jww.FEEDBACK.Printf("Template Type: %s\n%s\n", buildType, string(rawTemplate))
181 var signTransactionCmd = &cobra.Command{
182 Use: "sign-transaction <json templates>",
183 Short: "Sign transaction templates with account password",
184 Args: cobra.ExactArgs(1),
185 PreRun: func(cmd *cobra.Command, args []string) {
186 cmd.MarkFlagRequired("password")
188 Run: func(cmd *cobra.Command, args []string) {
189 template := txbuilder.Template{}
191 err := json.Unmarshal([]byte(args[0]), &template)
193 jww.ERROR.Println(err)
194 os.Exit(util.ErrLocalExe)
198 Password string `json:"password"`
199 Txs txbuilder.Template `json:"transaction"`
200 }{Password: password, Txs: template}
202 jww.FEEDBACK.Printf("\n\n")
203 data, exitCode := util.ClientCall("/sign-transaction", &req)
204 if exitCode != util.Success {
213 dataMap, ok := data.(map[string]interface{})
215 jww.ERROR.Println("invalid type assertion")
216 os.Exit(util.ErrLocalParse)
219 rawSign, err := json.Marshal(dataMap)
221 jww.ERROR.Println(err)
222 os.Exit(util.ErrLocalParse)
224 jww.FEEDBACK.Printf("\nSign Template:\n%s\n", string(rawSign))
228 var submitTransactionCmd = &cobra.Command{
229 Use: "submit-transaction <signed json raw_transaction>",
230 Short: "Submit signed transaction",
231 Args: cobra.ExactArgs(1),
232 Run: func(cmd *cobra.Command, args []string) {
234 Tx types.Tx `json:"raw_transaction"`
237 err := json.Unmarshal([]byte(args[0]), &ins)
239 jww.ERROR.Println(err)
240 os.Exit(util.ErrLocalExe)
243 data, exitCode := util.ClientCall("/submit-transaction", &ins)
244 if exitCode != util.Success {
252 var calculateTransactionGasCmd = &cobra.Command{
253 Use: "calculate-transaction-gas <signed json raw_transaction>",
254 Short: "calculate gas for signed transaction",
255 Args: cobra.ExactArgs(1),
256 Run: func(cmd *cobra.Command, args []string) {
258 Tx types.Tx `json:"raw_transaction"`
261 err := json.Unmarshal([]byte(args[0]), &ins)
263 jww.ERROR.Println(err)
264 os.Exit(util.ErrLocalExe)
267 data, exitCode := util.ClientCall("/calculate-transaction-gas", &ins)
268 if exitCode != util.Success {
276 var signSubTransactionCmd = &cobra.Command{
277 Use: "sign-submit-transaction <json templates>",
278 Short: "Sign and Submit transaction templates with account password",
279 Args: cobra.ExactArgs(1),
280 PreRun: func(cmd *cobra.Command, args []string) {
281 cmd.MarkFlagRequired("password")
283 Run: func(cmd *cobra.Command, args []string) {
284 template := txbuilder.Template{}
286 err := json.Unmarshal([]byte(args[0]), &template)
288 jww.ERROR.Println(err)
289 os.Exit(util.ErrLocalExe)
293 Password string `json:"password"`
294 Txs txbuilder.Template `json:"transaction"`
295 }{Password: password, Txs: template}
297 jww.FEEDBACK.Printf("\n\n")
298 data, exitCode := util.ClientCall("/sign-submit-transaction", &req)
299 if exitCode != util.Success {
307 var getTransactionCmd = &cobra.Command{
308 Use: "get-transaction <hash>",
309 Short: "get the transaction by matching the given transaction hash",
310 Args: cobra.ExactArgs(1),
311 Run: func(cmd *cobra.Command, args []string) {
313 TxID string `json:"tx_id"`
316 data, exitCode := util.ClientCall("/get-transaction", txInfo)
317 if exitCode != util.Success {
325 var listTransactionsCmd = &cobra.Command{
326 Use: "list-transactions",
327 Short: "List the transactions",
329 Run: func(cmd *cobra.Command, args []string) {
331 ID string `json:"id"`
332 AccountID string `json:"account_id"`
333 Detail bool `json:"detail"`
334 }{ID: txID, AccountID: account, Detail: detail}
336 data, exitCode := util.ClientCall("/list-transactions", &filter)
337 if exitCode != util.Success {
345 var gasRateCmd = &cobra.Command{
347 Short: "Print the current gas rate",
349 Run: func(cmd *cobra.Command, args []string) {
350 data, exitCode := util.ClientCall("/gas-rate")
351 if exitCode != util.Success {