-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
77 lines (68 loc) · 1.98 KB
/
root.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
package txs
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/blockservice/echoin/sdk"
)
// nolint
const (
FlagName = "name"
FlagNoSign = "no-sign"
FlagIn = "in"
FlagPrepare = "prepare"
FlagAddress = "address"
FlagType = "type"
FlagNonce = "nonce"
FlagVMChainId = "vm-chain-id"
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "tx",
Short: "Post tx from json input",
RunE: doRawTx,
}
func init() {
RootCmd.PersistentFlags().String(FlagName, "", "name to sign the tx")
RootCmd.PersistentFlags().String(FlagAddress, "", "account address to sign the tx")
RootCmd.PersistentFlags().Bool(FlagNoSign, false, "don't add a signature")
RootCmd.PersistentFlags().String(FlagPrepare, "", "file to store prepared tx")
RootCmd.Flags().String(FlagIn, "", "file with tx in json format")
RootCmd.PersistentFlags().String(FlagType, "commit", "type(sync|commit) of broadcast tx to tendermint")
RootCmd.PersistentFlags().Int(FlagNonce, -1, "Sequence number for this transaction")
RootCmd.PersistentFlags().Int64(FlagVMChainId, 19, "20: staging, 19: testnet, 18: mainnet")
}
func doRawTx(cmd *cobra.Command, args []string) error {
raw, err := readInput(viper.GetString(FlagIn))
if err != nil {
return err
}
// parse the input
var tx sdk.Tx
err = json.Unmarshal(raw, &tx)
if err != nil {
return errors.WithStack(err)
}
commit := viper.GetString(FlagType)
if commit == "commit" {
// otherwise, post it and display response
bres, err := PrepareOrPostTx(tx)
if err != nil {
return err
}
if bres == nil {
return nil // successful prep, nothing left to do
}
return OutputTx(bres) // print response of the post
} else {
bres, err := PrepareOrPostTxSync(tx)
if err != nil {
return err
}
if bres == nil {
return nil // successful prep, nothing left to do
}
return OutputTxSync(bres) // print response of the post
}
}