forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendtx.go
55 lines (49 loc) · 1.49 KB
/
sendtx.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package cmd
import (
"context"
"errors"
"github.com/aergoio/aergo/cmd/aergocli/util"
"github.com/aergoio/aergo/types"
"github.com/mr-tron/base58/base58"
"github.com/spf13/cobra"
)
var sendtxCmd = &cobra.Command{
Use: "sendtx",
Short: "Send transaction",
Args: cobra.MinimumNArgs(0),
RunE: execSendTX,
}
func init() {
rootCmd.AddCommand(sendtxCmd)
sendtxCmd.Flags().StringVar(&from, "from", "", "Sender account address")
sendtxCmd.MarkFlagRequired("from")
sendtxCmd.Flags().StringVar(&to, "to", "", "Recipient account address")
sendtxCmd.MarkFlagRequired("to")
sendtxCmd.Flags().StringVar(&amount, "amount", "0", "How much in AER")
sendtxCmd.MarkFlagRequired("amount")
}
func execSendTX(cmd *cobra.Command, args []string) error {
account, err := types.DecodeAddress(from)
if err != nil {
return errors.New("Wrong address in --from flag\n" + err.Error())
}
recipient, err := types.DecodeAddress(to)
if err != nil {
return errors.New("Wrong address in --to flag\n" + err.Error())
}
amountBigInt, err := util.ParseUnit(amount)
if err != nil {
return errors.New("Wrong value in --amount flag\n" + err.Error())
}
tx := &types.Tx{Body: &types.TxBody{Account: account, Recipient: recipient, Amount: amountBigInt.Bytes()}}
msg, err := client.SendTX(context.Background(), tx)
if err != nil {
return errors.New("Failed request to aergo sever\n" + err.Error())
}
cmd.Println(base58.Encode(msg.Hash), msg.Error)
return nil
}