github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountinfo.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 account 7 8 import ( 9 "encoding/hex" 10 "encoding/json" 11 "fmt" 12 "log" 13 "math/big" 14 15 "github.com/iotexproject/iotex-address/address" 16 "github.com/pkg/errors" 17 "github.com/spf13/cobra" 18 19 "github.com/iotexproject/iotex-core/ioctl" 20 "github.com/iotexproject/iotex-core/ioctl/config" 21 "github.com/iotexproject/iotex-core/ioctl/util" 22 ) 23 24 // Multi-language support 25 var ( 26 _infoCmdUses = map[config.Language]string{ 27 config.English: "info [ALIAS|ADDRESS]", 28 config.Chinese: "info [别名|地址]", 29 } 30 _infoCmdShorts = map[config.Language]string{ 31 config.English: "Display an account's information", 32 config.Chinese: "显示账号信息", 33 } 34 _invalidAccountBalance = map[config.Language]string{ 35 config.English: "invalid account balance", 36 config.Chinese: "无效的账户余额", 37 } 38 _failToGetAccountMeta = map[config.Language]string{ 39 config.English: "failed to get account meta", 40 config.Chinese: "获取账户信息失败", 41 } 42 ) 43 44 // NewAccountInfo represents the account info command 45 func NewAccountInfo(client ioctl.Client) *cobra.Command { 46 use, _ := client.SelectTranslation(_infoCmdUses) 47 short, _ := client.SelectTranslation(_infoCmdShorts) 48 failToGetAddress, _ := client.SelectTranslation(_failToGetAddress) 49 failToConvertStringIntoAddress, _ := client.SelectTranslation(_failToConvertStringIntoAddress) 50 _invalidAccountBalance, _ := client.SelectTranslation(_invalidAccountBalance) 51 _failToGetAccountMeta, _ := client.SelectTranslation(_failToGetAccountMeta) 52 53 return &cobra.Command{ 54 Use: use, 55 Short: short, 56 Args: cobra.ExactArgs(1), 57 RunE: func(cmd *cobra.Command, args []string) error { 58 cmd.SilenceUsage = true 59 addr := args[0] 60 if addr != address.StakingBucketPoolAddr && addr != address.RewardingPoolAddr { 61 var err error 62 addr, err = client.AddressWithDefaultIfNotExist(addr) 63 if err != nil { 64 return errors.Wrap(err, failToGetAddress) 65 } 66 } 67 68 accountMeta, err := Meta(client, addr) 69 if err != nil { 70 return errors.Wrap(err, _failToGetAccountMeta) 71 } 72 balance, ok := new(big.Int).SetString(accountMeta.Balance, 10) 73 if !ok { 74 return errors.New(_invalidAccountBalance) 75 } 76 ethAddr, err := address.FromString(addr) 77 if err != nil { 78 return errors.Wrap(err, failToConvertStringIntoAddress) 79 } 80 81 message := infoMessage{ 82 Address: addr, 83 EthAddress: ethAddr.Hex(), 84 Balance: util.RauToString(balance, util.IotxDecimalNum), 85 PendingNonce: int(accountMeta.PendingNonce), 86 NumActions: int(accountMeta.NumActions), 87 IsContract: accountMeta.IsContract, 88 ContractByteCode: hex.EncodeToString(accountMeta.ContractByteCode), 89 } 90 cmd.Println(message.String()) 91 return nil 92 }, 93 } 94 } 95 96 type infoMessage struct { 97 Address string `json:"address"` 98 EthAddress string `json:"ethAddress"` 99 Balance string `json:"balance"` 100 PendingNonce int `json:"pendingNonce"` 101 NumActions int `json:"numActions"` 102 IsContract bool `json:"isContract"` 103 ContractByteCode string `json:"contractByteCode"` 104 } 105 106 func (m *infoMessage) String() string { 107 byteAsJSON, err := json.MarshalIndent(m, "", " ") 108 if err != nil { 109 log.Panic(err) 110 } 111 return fmt.Sprint(string(byteAsJSON)) 112 }