github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/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 "encoding/hex" 10 11 "github.com/pkg/errors" 12 "github.com/spf13/cobra" 13 14 "github.com/iotexproject/iotex-core/ioctl" 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/newcmd/alias" 17 "github.com/iotexproject/iotex-core/ioctl/util" 18 ) 19 20 // Multi-language support 21 var ( 22 _readCmdShorts = map[config.Language]string{ 23 config.English: "read smart contract on IoTeX blockchain", 24 config.Chinese: "读取IoTeX区块链上的智能合约", 25 } 26 _readCmdUses = map[config.Language]string{ 27 config.English: "read (ALIAS|CONTRACT_ADDRESS) -b BYTE_CODE [-s SIGNER]", 28 config.Chinese: "read (别名|联系人地址) -b 类型码 [-s 签署人]", 29 } 30 ) 31 32 // NewActionReadCmd represents the action Read command 33 func NewActionReadCmd(client ioctl.Client) *cobra.Command { 34 use, _ := client.SelectTranslation(_readCmdUses) 35 short, _ := client.SelectTranslation(_readCmdShorts) 36 37 cmd := &cobra.Command{ 38 Use: use, 39 Short: short, 40 Args: cobra.ExactArgs(1), 41 RunE: func(cmd *cobra.Command, args []string) error { 42 cmd.SilenceUsage = true 43 contract, err := alias.IOAddress(client, args[0]) 44 if err != nil { 45 return errors.Wrap(err, "failed to get contract address") 46 } 47 bytecodeFlag, err := cmd.Flags().GetString(bytecodeFlagLabel) 48 if err != nil { 49 return errors.Wrap(err, "failed to get flag bytecode") 50 } 51 bytecode, err := hex.DecodeString(util.TrimHexPrefix(bytecodeFlag)) 52 if err != nil { 53 return errors.Wrap(err, "invalid bytecode") 54 } 55 signer, err := cmd.Flags().GetString(signerFlagLabel) 56 if err != nil { 57 return err 58 } 59 result, err := Read(client, contract, "0", bytecode, signer, GasLimitFlagDefault) 60 if err != nil { 61 return errors.Wrap(err, "failed to Read contract") 62 } 63 cmd.Println(result) 64 return err 65 }, 66 } 67 registerBytecodeFlag(client, cmd) 68 registerSignerFlag(client, cmd) 69 return cmd 70 }