/
committx.go
74 lines (64 loc) · 1.74 KB
/
committx.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package cmd
import (
"context"
"errors"
"os"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/jsonrpc"
"github.com/spf13/cobra"
)
var committxCmd = &cobra.Command{
Use: "committx",
Short: "commit transaction to aergo server",
Args: cobra.MinimumNArgs(0),
RunE: execCommitTX,
}
var recipient string
var jsonTx string
var jsonPath string
func init() {
rootCmd.AddCommand(committxCmd)
committxCmd.Flags().StringVar(&jsonTx, "jsontx", "", "Transaction list json\n"+"Tx(Transaction) json example is\n"+`{
"Hash": "Base58EncodedBytes",
"Body": {
"Nonce": 1,
"Account": "AmLfhA2F82Nayuek17tvzechaQPe5cRQKBBJ8xfei7GejvufVRBp",
"Recipient": "Amgf9vfcHKkC1ijGTMjxLoeTTutXgbaHHBznpHu5ugutU96iKSLW",
"Amount": "0",
"Payload": "Base58EncodedBytes",
"Limit": 0,
"Price": "0",
"Type": 0,
"ChainIdHash": "Base58EncodedBytes",
"Sign": "Base58EncodedBytes"
}
}`)
committxCmd.Flags().StringVar(&jsonPath, "jsontxpath", "", "Transaction list json file path")
}
func execCommitTX(cmd *cobra.Command, args []string) error {
if jsonPath != "" {
b, readerr := os.ReadFile(jsonPath)
if readerr != nil {
return errors.New("Failed to read --jsontxpath\n" + readerr.Error())
}
jsonTx = string(b)
}
if jsonTx != "" {
var msg *types.CommitResultList
txlist, err := jsonrpc.ParseBase58Tx([]byte(jsonTx))
if err != nil {
return errors.New("Failed to parse --jsontx\n" + err.Error())
}
msg, err = client.CommitTX(context.Background(), &types.TxList{Txs: txlist})
if err != nil {
return errors.New("Failed request to aergo server\n" + err.Error())
}
res := jsonrpc.ConvCommitResultList(msg)
cmd.Println(jsonrpc.MarshalJSON(res))
}
return nil
}