github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/contract/contracttestbytecode.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 "math/big" 10 11 "github.com/iotexproject/iotex-address/address" 12 "github.com/pkg/errors" 13 "github.com/spf13/cobra" 14 15 "github.com/iotexproject/iotex-core/ioctl" 16 "github.com/iotexproject/iotex-core/ioctl/config" 17 "github.com/iotexproject/iotex-core/ioctl/newcmd/action" 18 "github.com/iotexproject/iotex-core/ioctl/util" 19 ) 20 21 // Multi-language support 22 var ( 23 _testBytecodeCmdUses = map[config.Language]string{ 24 config.English: "bytecode (CONTRACT_ADDRESS|ALIAS) PACKED_ARGUMENTS [AMOUNT_IOTX]", 25 config.Chinese: "bytecode (合约地址|别名) 已打包参数 [IOTX数量]", 26 } 27 _testBytecodeCmdShorts = map[config.Language]string{ 28 config.English: "test smart contract on IoTeX blockchain with packed arguments", 29 config.Chinese: "传入bytecode测试IoTeX区块链上的智能合约", 30 } 31 ) 32 33 // NewContractTestBytecodeCmd represents the contract test bytecode command 34 func NewContractTestBytecodeCmd(client ioctl.Client) *cobra.Command { 35 use, _ := client.SelectTranslation(_testBytecodeCmdUses) 36 short, _ := client.SelectTranslation(_testBytecodeCmdShorts) 37 38 cmd := &cobra.Command{ 39 Use: use, 40 Short: short, 41 Args: cobra.RangeArgs(2, 3), 42 RunE: func(cmd *cobra.Command, args []string) error { 43 cmd.SilenceUsage = true 44 addr, err := client.Address(args[0]) 45 if err != nil { 46 return errors.Wrap(err, "failed to get contract address") 47 } 48 49 contract, err := address.FromString(addr) 50 if err != nil { 51 return errors.Wrap(err, "failed to convert string into address") 52 } 53 54 bytecode, err := decodeBytecode(args[1]) 55 if err != nil { 56 return errors.Wrap(err, "invalid bytecode") 57 } 58 59 amount := big.NewInt(0) 60 if len(args) == 3 { 61 amount, err = util.StringToRau(args[2], util.IotxDecimalNum) 62 if err != nil { 63 return errors.Wrap(err, "invalid amount") 64 } 65 } 66 67 _, signer, _, _, gasLimit, _, err := action.GetWriteCommandFlag(cmd) 68 if err != nil { 69 return err 70 } 71 result, err := action.Read(client, contract, amount.String(), bytecode, signer, gasLimit) 72 if err != nil { 73 return errors.Wrap(err, "failed to read contract") 74 } 75 76 cmd.Printf("return: %s\n", result) 77 return nil 78 }, 79 } 80 action.RegisterWriteCommand(client, cmd) 81 return cmd 82 }