github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/contract/contractdeploybytecode.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package contract
     7  
     8  import (
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/iotexproject/iotex-core/ioctl/cmd/action"
    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] [--init-amount AMOUNT_IOTX]",
    21  		config.Chinese: "bytecode BYTECODE [ABI文件路径 初始化输入] [--init-amount IOTX数量]",
    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 init() {
    42  	_initialAmountFlag.RegisterCommand(_contractDeployBytecodeCmd)
    43  }
    44  
    45  func contractDeployBytecode(args []string) error {
    46  	bytecode, err := decodeBytecode(args[0])
    47  	if err != nil {
    48  		return output.NewError(output.ConvertError, "failed to decode bytecode", err)
    49  	}
    50  
    51  	if len(args) == 3 {
    52  		abi, err := readAbiFile(args[1])
    53  		if err != nil {
    54  			return err
    55  		}
    56  		// Constructor's method name is "" (empty string)
    57  		packedArg, err := packArguments(abi, "", args[2])
    58  		if err != nil {
    59  			return output.NewError(output.ConvertError, "failed to pack given arguments", err)
    60  		}
    61  
    62  		bytecode = append(bytecode, packedArg...)
    63  	}
    64  
    65  	amount, err := util.StringToRau(_initialAmountFlag.Value().(string), util.IotxDecimalNum)
    66  	if err != nil {
    67  		return output.NewError(output.FlagError, "invalid amount", err)
    68  	}
    69  
    70  	return action.Execute("", amount, bytecode)
    71  }