github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/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/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/output"
    17  	"github.com/iotexproject/iotex-core/ioctl/util"
    18  )
    19  
    20  // Multi-language support
    21  var (
    22  	_testBytecodeCmdUses = map[config.Language]string{
    23  		config.English: "bytecode (CONTRACT_ADDRESS|ALIAS) PACKED_ARGUMENTS [AMOUNT_IOTX]",
    24  		config.Chinese: "bytecode (合约地址|别名) 已打包参数 [IOTX数量]",
    25  	}
    26  	_testBytecodeCmdShorts = map[config.Language]string{
    27  		config.English: "test smart contract on IoTeX blockchain with packed arguments",
    28  		config.Chinese: "传入bytecode测试IoTeX区块链上的智能合约",
    29  	}
    30  )
    31  
    32  // _contractTestBytecodeCmd represents the contract test bytecode command
    33  var _contractTestBytecodeCmd = &cobra.Command{
    34  	Use:   config.TranslateInLang(_testBytecodeCmdUses, config.UILanguage),
    35  	Short: config.TranslateInLang(_testBytecodeCmdShorts, config.UILanguage),
    36  	Args:  cobra.RangeArgs(2, 3),
    37  	RunE: func(cmd *cobra.Command, args []string) error {
    38  		cmd.SilenceUsage = true
    39  		err := contractTestBytecode(args)
    40  		return output.PrintError(err)
    41  	},
    42  }
    43  
    44  func contractTestBytecode(args []string) error {
    45  	addr, err := util.Address(args[0])
    46  	if err != nil {
    47  		return output.NewError(output.AddressError, "failed to get contract address", err)
    48  	}
    49  
    50  	contract, err := address.FromString(addr)
    51  	if err != nil {
    52  		return output.NewError(output.ConvertError, "failed to convert string into address", err)
    53  	}
    54  
    55  	bytecode, err := decodeBytecode(args[1])
    56  	if err != nil {
    57  		return output.NewError(output.ConvertError, "invalid bytecode", err)
    58  	}
    59  
    60  	amount := big.NewInt(0)
    61  	if len(args) == 3 {
    62  		amount, err = util.StringToRau(args[2], util.IotxDecimalNum)
    63  		if err != nil {
    64  			return output.NewError(output.ConvertError, "invalid amount", err)
    65  		}
    66  	}
    67  
    68  	result, err := action.Read(contract, amount.String(), bytecode)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	output.PrintResult("return: " + result)
    74  	return nil
    75  }