github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/contract/contractdeploybin.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 "os" 10 11 "github.com/spf13/cobra" 12 13 "github.com/iotexproject/iotex-core/ioctl/cmd/action" 14 "github.com/iotexproject/iotex-core/ioctl/config" 15 "github.com/iotexproject/iotex-core/ioctl/output" 16 "github.com/iotexproject/iotex-core/ioctl/util" 17 ) 18 19 // Multi-language support 20 var ( 21 _deployBinCmdUses = map[config.Language]string{ 22 config.English: "bin BIN_PATH [ABI_PATH INIT_INPUT] [--init-amount AMOUNT_IOTX]", 23 config.Chinese: "bin BIN文件路径 [ABI文件路径 初始化输入] [--init-amount IOTX数量]", 24 } 25 _deployBinCmdShorts = map[config.Language]string{ 26 config.English: "deploy smart contract with bin on IoTeX blockchain", 27 config.Chinese: "deploy 使用 bin 文件方式在 IoTex区块链上部署智能合约", 28 } 29 ) 30 31 // _contractDeployBinCmd represents the contract deploy bin command 32 var _contractDeployBinCmd = &cobra.Command{ 33 Use: config.TranslateInLang(_deployBinCmdUses, config.UILanguage), 34 Short: config.TranslateInLang(_deployBinCmdShorts, config.UILanguage), 35 Args: util.CheckArgs(1, 3), 36 RunE: func(cmd *cobra.Command, args []string) error { 37 cmd.SilenceUsage = true 38 err := contractDeployBin(args) 39 return output.PrintError(err) 40 }, 41 } 42 43 func init() { 44 _initialAmountFlag.RegisterCommand(_contractDeployBinCmd) 45 } 46 47 func contractDeployBin(args []string) error { 48 bin, err := os.ReadFile(args[0]) 49 if err != nil { 50 return output.NewError(output.ReadFileError, "failed to read bin file", err) 51 } 52 bytecode, err := decodeBytecode(string(bin)) 53 if err != nil { 54 return output.NewError(output.ConvertError, "failed to decode bytecode", err) 55 } 56 if len(args) == 3 { 57 abi, err := readAbiFile(args[1]) 58 if err != nil { 59 return err 60 } 61 62 // Constructor's method name is "" (empty string) 63 packedArg, err := packArguments(abi, "", args[2]) 64 if err != nil { 65 return output.NewError(output.ConvertError, "failed to pack given arguments", err) 66 } 67 68 bytecode = append(bytecode, packedArg...) 69 } 70 71 amount, err := util.StringToRau(_initialAmountFlag.Value().(string), util.IotxDecimalNum) 72 if err != nil { 73 return output.NewError(output.FlagError, "invalid amount", err) 74 } 75 76 return action.Execute("", amount, bytecode) 77 }