-
Notifications
You must be signed in to change notification settings - Fork 4
/
root.go
143 lines (123 loc) · 4.91 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
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
package cmd
import (
"os"
"github.com/cosmos/cosmos-sdk/client"
sdkconfig "github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/server"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank"
bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/cosmos/cosmos-sdk/x/staking"
cross "github.com/datachainlab/cross/x/core"
"github.com/hyperledger-labs/yui-relayer/chains/tendermint"
"github.com/spf13/cobra"
"github.com/datachainlab/fabric-tendermint-cross-demo/cmds/alpha/config"
"github.com/datachainlab/fabric-tendermint-cross-demo/contracts/erc20/modules/erc20contract"
erc20contracttypes "github.com/datachainlab/fabric-tendermint-cross-demo/contracts/erc20/modules/erc20contract/types"
"github.com/datachainlab/fabric-tendermint-cross-demo/contracts/erc20/modules/erc20mgr"
erc20types "github.com/datachainlab/fabric-tendermint-cross-demo/contracts/erc20/modules/erc20mgr/types"
)
var defaultHome = os.ExpandEnv("$HOME/.alpha")
// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
// context must be configured by svrcmd.Execute()
if err := svrcmd.Execute(NewRootCmd(), defaultHome); err != nil {
switch e := err.(type) {
case server.ErrorCode:
os.Exit(e.Code)
default:
os.Exit(1)
}
}
}
// NewRootCmd creates a new root command
func NewRootCmd() *cobra.Command {
// refer to demo/chains/tendermint/simapp/app.go L143
moduleBasics := module.NewBasicManager(
auth.AppModuleBasic{},
staking.AppModuleBasic{},
bank.AppModuleBasic{},
cross.AppModuleBasic{},
erc20mgr.AppModuleBasic{},
erc20contract.AppModuleBasic{},
)
encodingConfig := makeEncodingConfig(moduleBasics)
initClientCtx := client.Context{}.
WithJSONCodec(encodingConfig.Marshaler).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
WithInput(os.Stdin).
WithAccountRetriever(authtypes.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastBlock).
WithSkipConfirmation(true).
WithHomeDir(defaultHome).
WithViper("") // In simapp, we don't use any prefix for env variables.
rootCmd := &cobra.Command{
Use: "alphacli",
Short: "This provides commands for cross on a Tendermint chain",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// set the default command outputs
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())
initClientCtx = client.ReadHomeFlag(initClientCtx, cmd)
initClientCtx, err := sdkconfig.ReadFromClientConfig(initClientCtx)
if err != nil {
return err
}
if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
return err
}
return server.InterceptConfigsPreRunHandler(cmd)
},
SilenceUsage: true,
}
initRootCmd(rootCmd, moduleBasics, encodingConfig)
return rootCmd
}
func makeEncodingConfig(moduleBasics module.BasicManager) simappparams.EncodingConfig {
encodingConfig := simappparams.MakeTestEncodingConfig()
std.RegisterLegacyAminoCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
moduleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino)
moduleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig
}
func initRootCmd(rootCmd *cobra.Command, moduleBasics module.BasicManager, encodingConfig simappparams.EncodingConfig) {
cobra.EnableCommandSorting = false
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
// Register interfaces
tendermint.RegisterInterfaces(interfaceRegistry)
erc20types.RegisterInterfaces(interfaceRegistry)
erc20contracttypes.RegisterInterfaces(interfaceRegistry)
cryptotypes.RegisterInterfaces(interfaceRegistry)
cross.AppModuleBasic{}.RegisterInterfaces(interfaceRegistry)
ctx := &config.Context{Codec: cdc}
// Register subcommands
// refer to `demo/chains/tendermint/simapp/simd/cmd/root.go`
rootCmd.AddCommand(
genutilcli.InitCmd(moduleBasics, defaultHome),
keys.Commands(defaultHome),
addGenesisAccountCmd(defaultHome),
genutilcli.GenTxCmd(moduleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, defaultHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, defaultHome),
bankcli.NewTxCmd(),
queryCommand(moduleBasics),
tendermintCmd(),
crossCmd(ctx),
erc20Cmd(),
)
}