Skip to content

Commit 3cbec52

Browse files
authored
ioctl supports for smart contract cmds (iotexproject#2138)
1 parent d83c17f commit 3cbec52

9 files changed

+301
-0
lines changed

ioctl/cmd/contract/contract.go

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ var ContractCmd = &cobra.Command{
6161

6262
func init() {
6363
ContractCmd.AddCommand(ContractCompileCmd)
64+
ContractCmd.AddCommand(contractDeployCmd)
65+
ContractCmd.AddCommand(contractInvokeCmd)
6466
ContractCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
6567
config.ReadConfig.Endpoint, config.TranslateInLang(flagEndpointUsages, config.UILanguage))
6668
ContractCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure,

ioctl/cmd/contract/contractdeploy.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2020 IoTeX Foundation
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package contract
8+
9+
import (
10+
"github.com/spf13/cobra"
11+
12+
"github.com/iotexproject/iotex-core/ioctl/config"
13+
)
14+
15+
// Multi-language support
16+
var (
17+
deployCmdUses = map[config.Language]string{
18+
config.English: "deploy",
19+
config.Chinese: "deploy",
20+
}
21+
deployCmdShorts = map[config.Language]string{
22+
config.English: "deploy smart contract of IoTeX blockchain",
23+
config.Chinese: "在IoTeX区块链部署智能合约",
24+
}
25+
)
26+
27+
// contractDeployCmd represents the contract deploy command
28+
var contractDeployCmd = &cobra.Command{
29+
Use: config.TranslateInLang(deployCmdUses, config.UILanguage),
30+
Short: config.TranslateInLang(deployCmdShorts, config.UILanguage),
31+
}
32+
33+
func init() {
34+
contractDeployCmd.AddCommand(contractDeployBytecodeCmd)
35+
contractDeployCmd.AddCommand(contractDeployBinCmd)
36+
contractDeployCmd.AddCommand(contractDeploySolCmd)
37+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2020 IoTeX Foundation
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package contract
8+
9+
import (
10+
"github.com/spf13/cobra"
11+
12+
"github.com/iotexproject/iotex-core/ioctl/config"
13+
"github.com/iotexproject/iotex-core/ioctl/output"
14+
"github.com/iotexproject/iotex-core/ioctl/util"
15+
)
16+
17+
// Multi-language support
18+
var (
19+
deployBinCmdUses = map[config.Language]string{
20+
config.English: "bin BIN_PATH [ABI_PATH INIT_INPUT]",
21+
config.Chinese: "bin BIN BIN文件路径 [ABI文件路径 初始化参数]",
22+
}
23+
deployBinCmdShorts = map[config.Language]string{
24+
config.English: "deploy smart contract with bin on IoTeX blockchain",
25+
config.Chinese: "deploy 使用 bin 文件方式在 IoTex区块链上部署智能合约",
26+
}
27+
)
28+
29+
// contractDeployBinCmd represents the contract deploy bin command
30+
var contractDeployBinCmd = &cobra.Command{
31+
Use: config.TranslateInLang(deployBinCmdUses, config.UILanguage),
32+
Short: config.TranslateInLang(deployBytecodeCmdShorts, config.UILanguage),
33+
Args: util.CheckArgs(1, 3),
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
cmd.SilenceUsage = true
36+
err := contractDeployBin(args)
37+
return output.PrintError(err)
38+
},
39+
}
40+
41+
func contractDeployBin(args []string) error {
42+
return nil
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2020 IoTeX Foundation
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package contract
8+
9+
import (
10+
"github.com/spf13/cobra"
11+
12+
"github.com/iotexproject/iotex-core/ioctl/config"
13+
"github.com/iotexproject/iotex-core/ioctl/output"
14+
"github.com/iotexproject/iotex-core/ioctl/util"
15+
)
16+
17+
// Multi-language support
18+
var (
19+
deployBytecodeCmdUses = map[config.Language]string{
20+
config.English: "bytecode BYTECODE [ABI_PATH INIT_INPUT]",
21+
config.Chinese: "bytecode BYTECODE [ABI文件路径 初始化参数]",
22+
}
23+
deployBytecodeCmdShorts = map[config.Language]string{
24+
config.English: "deploy smart contract with bytecode on IoTeX blockchain",
25+
config.Chinese: "deploy 使用 bytecode 文件方式在 IoTex区块链上部署智能合约",
26+
}
27+
)
28+
29+
// contractDeployBytecodeCmd represents the contract deploy bytecode command
30+
var contractDeployBytecodeCmd = &cobra.Command{
31+
Use: config.TranslateInLang(deployBytecodeCmdUses, config.UILanguage),
32+
Short: config.TranslateInLang(deployBytecodeCmdShorts, config.UILanguage),
33+
Args: util.CheckArgs(1, 3),
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
cmd.SilenceUsage = true
36+
err := contractDeployBytecode(args)
37+
return output.PrintError(err)
38+
},
39+
}
40+
41+
func contractDeployBytecode(args []string) error {
42+
return nil
43+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2020 IoTeX Foundation
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package contract
8+
9+
import (
10+
"github.com/spf13/cobra"
11+
12+
"github.com/iotexproject/iotex-core/ioctl/config"
13+
"github.com/iotexproject/iotex-core/ioctl/output"
14+
)
15+
16+
// Multi-language support
17+
var (
18+
deploySolCmdUses = map[config.Language]string{
19+
config.English: "sol CODE_PATH CONTRACT_NAME [INIT_INPUT]",
20+
config.Chinese: "sol 源代码文件路径 合约名 [初始化参数]",
21+
}
22+
deploySolCmdShorts = map[config.Language]string{
23+
config.English: "deploy smart contract with sol on IoTeX blockchain",
24+
config.Chinese: "deploy 使用 sol 方式在 IoTex区块链上部署智能合约",
25+
}
26+
)
27+
28+
// contractDeploySolCmd represents the contract deploy sol command
29+
var contractDeploySolCmd = &cobra.Command{
30+
Use: config.TranslateInLang(deploySolCmdUses, config.UILanguage),
31+
Short: config.TranslateInLang(deployBytecodeCmdShorts, config.UILanguage),
32+
Args: cobra.RangeArgs(1, 3),
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
cmd.SilenceUsage = true
35+
err := contractDeploySol(args)
36+
return output.PrintError(err)
37+
},
38+
}
39+
40+
func contractDeploySol(args []string) error {
41+
return nil
42+
}

ioctl/cmd/contract/contractinvoke.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2020 IoTeX Foundation
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package contract
8+
9+
import (
10+
"github.com/iotexproject/iotex-core/ioctl/config"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// Multi-language support
15+
var (
16+
invokeCmdUses = map[config.Language]string{
17+
config.English: "invoke",
18+
config.Chinese: "invoke",
19+
}
20+
invokeCmdShorts = map[config.Language]string{
21+
config.English: "invoke smart contract on IoTex blockchain",
22+
config.Chinese: "调用IoTex区块链上的智能合约",
23+
}
24+
)
25+
26+
// contractInvokeCmd represents the contract invoke command
27+
var contractInvokeCmd = &cobra.Command{
28+
Use: config.TranslateInLang(invokeCmdUses, config.UILanguage),
29+
Short: config.TranslateInLang(invokeCmdShorts, config.UILanguage),
30+
}
31+
32+
func init() {
33+
contractInvokeCmd.AddCommand(contractInvokeFunctionCmd)
34+
contractInvokeCmd.AddCommand(contractInvokeBytecodeCmd)
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2020 IoTeX Foundation
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package contract
8+
9+
import (
10+
"github.com/spf13/cobra"
11+
12+
"github.com/iotexproject/iotex-core/ioctl/config"
13+
"github.com/iotexproject/iotex-core/ioctl/output"
14+
)
15+
16+
// Multi-language support
17+
var (
18+
invokeBytecodeCmdUses = map[config.Language]string{
19+
config.English: "bytecode (CONTRACT_ADDRESS|ALIAS) PACKED_ARGUMENTS",
20+
config.Chinese: "bytecode (合约地址|别名) 已打包参数",
21+
}
22+
invokeBytecodeCmdShorts = map[config.Language]string{
23+
config.English: "invoke smart contract on IoTex blockchain with packed arguments",
24+
config.Chinese: "invoke 通过 已打包参数方式 调用IoTex区块链上的智能合约",
25+
}
26+
)
27+
28+
// contractInvokeBytecodeCmd represents the contract invoke bytecode command
29+
var contractInvokeBytecodeCmd = &cobra.Command{
30+
Use: config.TranslateInLang(invokeBytecodeCmdUses, config.UILanguage),
31+
Short: config.TranslateInLang(invokeBytecodeCmdShorts, config.UILanguage),
32+
Args: cobra.ExactArgs(2),
33+
RunE: func(cmd *cobra.Command, args []string) error {
34+
cmd.SilenceUsage = true
35+
err := contractInvokeBytecode(args)
36+
return output.PrintError(err)
37+
},
38+
}
39+
40+
func contractInvokeBytecode(args []string) error {
41+
return nil
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2020 IoTeX Foundation
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package contract
8+
9+
import (
10+
"github.com/spf13/cobra"
11+
12+
"github.com/iotexproject/iotex-core/ioctl/config"
13+
"github.com/iotexproject/iotex-core/ioctl/output"
14+
"github.com/iotexproject/iotex-core/ioctl/util"
15+
)
16+
17+
// Multi-language support
18+
var (
19+
invokeFunctionCmdUses = map[config.Language]string{
20+
config.English: "function (CONTRACT_ADDRESS|ALIAS) FUNCTION_NAME [ABI_PATH INVOKE_INPUT]",
21+
config.Chinese: "function (合约地址|别名) 函数名 [ABI文件路径 调用初始化输入]",
22+
}
23+
invokeFunctionCmdShorts = map[config.Language]string{
24+
config.English: "invoke smart contract on IoTex blockchain with function name",
25+
config.Chinese: "invoke 通过 函数名方式 调用IoTex区块链上的智能合约",
26+
}
27+
)
28+
29+
// contractInvokeFunctionCmd represents the contract invoke function command
30+
var contractInvokeFunctionCmd = &cobra.Command{
31+
Use: config.TranslateInLang(invokeFunctionCmdUses, config.UILanguage),
32+
Short: config.TranslateInLang(invokeFunctionCmdShorts, config.UILanguage),
33+
Args: util.CheckArgs(1, 3),
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
cmd.SilenceUsage = true
36+
err := contractInvokeFunction(args)
37+
return output.PrintError(err)
38+
},
39+
}
40+
41+
func contractInvokeFunction(args []string) error {
42+
return nil
43+
}

ioctl/util/util.go

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package util
99
import (
1010
"bytes"
1111
"crypto/tls"
12+
"fmt"
1213
"io/ioutil"
1314
"math/big"
1415
"os"
@@ -192,6 +193,19 @@ func JwtAuth() (jwt metadata.MD, err error) {
192193
return metadata.Pairs("authorization", "bearer "+string(jwtString)), nil
193194
}
194195

196+
// CheckArgs used for check ioctl cmd arg(s)'s num
197+
func CheckArgs(validNum ...int) cobra.PositionalArgs {
198+
return func(cmd *cobra.Command, args []string) error {
199+
for _, n := range validNum {
200+
if len(args) == n {
201+
return nil
202+
}
203+
}
204+
nums := strings.Replace(strings.Trim(fmt.Sprint(validNum), "[]"), " ", " or ", -1)
205+
return fmt.Errorf("accepts "+nums+" arg(s), received %d", len(args))
206+
}
207+
}
208+
195209
// TrimHexPrefix removes 0x prefix from a string if it has
196210
func TrimHexPrefix(s string) string {
197211
return strings.TrimPrefix(s, "0x")

0 commit comments

Comments
 (0)