github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/contract/contracttestfunction.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/spf13/cobra" 13 14 "github.com/iotexproject/iotex-core/ioctl/cmd/action" 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/flag" 17 "github.com/iotexproject/iotex-core/ioctl/output" 18 "github.com/iotexproject/iotex-core/ioctl/util" 19 ) 20 21 // Multi-language support 22 var ( 23 _testFunctionCmdUses = map[config.Language]string{ 24 config.English: "function (CONTRACT_ADDRESS|ALIAS) ABI_PATH FUNCTION_NAME [AMOUNT_IOTX] " + 25 "[--with-arguments INVOKE_INPUT]", 26 config.Chinese: "function (合约地址|别名) ABI文件路径 函数名 [IOTX数量] [--with-arguments 调用输入]", 27 } 28 _testFunctionCmdShorts = map[config.Language]string{ 29 config.English: "test smart contract on IoTeX blockchain with function name", 30 config.Chinese: "调用函数测试IoTeX区块链上的智能合约", 31 } 32 ) 33 34 // _contractTestFunctionCmd represents the contract test bytecode command 35 var _contractTestFunctionCmd = &cobra.Command{ 36 Use: config.TranslateInLang(_testFunctionCmdUses, config.UILanguage), 37 Short: config.TranslateInLang(_testFunctionCmdShorts, config.UILanguage), 38 Args: cobra.RangeArgs(3, 4), 39 RunE: func(cmd *cobra.Command, args []string) error { 40 cmd.SilenceUsage = true 41 err := contractTestFunction(args) 42 return output.PrintError(err) 43 }, 44 } 45 46 func contractTestFunction(args []string) error { 47 addr, err := util.Address(args[0]) 48 if err != nil { 49 return output.NewError(output.AddressError, "failed to get contract address", err) 50 } 51 52 contract, err := address.FromString(addr) 53 if err != nil { 54 return output.NewError(output.ConvertError, "failed to convert string into address", err) 55 } 56 57 abi, err := readAbiFile(args[1]) 58 if err != nil { 59 return output.NewError(output.ReadFileError, "failed to read abi file "+args[1], err) 60 } 61 62 methodName := args[2] 63 64 amount := big.NewInt(0) 65 if len(args) == 4 { 66 amount, err = util.StringToRau(args[3], util.IotxDecimalNum) 67 if err != nil { 68 return output.NewError(output.ConvertError, "invalid amount", err) 69 } 70 } 71 72 bytecode, err := packArguments(abi, methodName, flag.WithArgumentsFlag.Value().(string)) 73 if err != nil { 74 return output.NewError(output.ConvertError, "failed to pack given arguments", err) 75 } 76 77 rowResult, err := action.Read(contract, amount.String(), bytecode) 78 if err != nil { 79 return err 80 } 81 82 result, err := ParseOutput(abi, methodName, rowResult) 83 if err != nil { 84 result = rowResult 85 } 86 87 output.PrintResult("return: " + result) 88 return nil 89 }