github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/actionread.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 action
     7  
     8  import (
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/iotexproject/iotex-core/ioctl/cmd/alias"
    12  	"github.com/iotexproject/iotex-core/ioctl/config"
    13  	"github.com/iotexproject/iotex-core/ioctl/output"
    14  )
    15  
    16  // Multi-language support
    17  var (
    18  	_readCmdShorts = map[config.Language]string{
    19  		config.English: "read smart contract on IoTeX blockchain",
    20  		config.Chinese: "读取IoTeX区块链上的智能合约",
    21  	}
    22  	_readCmdUses = map[config.Language]string{
    23  		config.English: "read (ALIAS|CONTRACT_ADDRESS) -b BYTE_CODE [-s SIGNER]",
    24  		config.Chinese: "reads (别名|联系人地址) -b 类型码 [-s 签署人]",
    25  	}
    26  )
    27  
    28  // _actionReadCmd represents the action Read command
    29  var _actionReadCmd = &cobra.Command{
    30  	Use:   config.TranslateInLang(_readCmdUses, config.UILanguage),
    31  	Short: config.TranslateInLang(_readCmdShorts, config.UILanguage),
    32  	Args:  cobra.ExactArgs(1),
    33  	RunE: func(cmd *cobra.Command, args []string) error {
    34  		cmd.SilenceUsage = true
    35  		err := read(args[0])
    36  		return output.PrintError(err)
    37  	},
    38  }
    39  
    40  func init() {
    41  	_signerFlag.RegisterCommand(_actionReadCmd)
    42  	_bytecodeFlag.RegisterCommand(_actionReadCmd)
    43  	_bytecodeFlag.MarkFlagRequired(_actionReadCmd)
    44  }
    45  
    46  func read(arg string) error {
    47  	contract, err := alias.IOAddress(arg)
    48  	if err != nil {
    49  		return output.NewError(output.AddressError, "failed to get contract address", err)
    50  	}
    51  	bytecode, err := decodeBytecode()
    52  	if err != nil {
    53  		return output.NewError(output.ConvertError, "invalid bytecode", err)
    54  	}
    55  	result, err := Read(contract, "0", bytecode)
    56  	if err != nil {
    57  		return output.NewError(0, "failed to Read contract", err)
    58  	}
    59  	output.PrintResult(result)
    60  	return err
    61  }