github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/account/accountinfo.go (about)

     1  // Copyright (c) 2019 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  	"fmt"
    11  	"math/big"
    12  
    13  	"github.com/iotexproject/iotex-address/address"
    14  	"github.com/spf13/cobra"
    15  
    16  	"github.com/iotexproject/iotex-core/ioctl/config"
    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  	_infoCmdUses = map[config.Language]string{
    24  		config.English: "info [ALIAS|ADDRESS]",
    25  		config.Chinese: "info [别名|地址]",
    26  	}
    27  	_infoCmdShorts = map[config.Language]string{
    28  		config.English: "Display an account's information",
    29  		config.Chinese: "显示账号信息",
    30  	}
    31  )
    32  
    33  // _accountInfoCmd represents the account info command
    34  var _accountInfoCmd = &cobra.Command{
    35  	Use:   config.TranslateInLang(_infoCmdUses, config.UILanguage),
    36  	Short: config.TranslateInLang(_infoCmdShorts, config.UILanguage),
    37  	Args:  cobra.ExactArgs(1),
    38  	RunE: func(cmd *cobra.Command, args []string) error {
    39  		cmd.SilenceUsage = true
    40  		err := info(args[0])
    41  		return output.PrintError(err)
    42  	},
    43  }
    44  
    45  type infoMessage struct {
    46  	Address          string `json:"address"`
    47  	EthAddress       string `json:"ethAddress"`
    48  	Balance          string `json:"balance"`
    49  	PendingNonce     int    `json:"pendingNonce"`
    50  	NumActions       int    `json:"numActions"`
    51  	IsContract       bool   `json:"isContract"`
    52  	ContractByteCode string `json:"contractByteCode"`
    53  }
    54  
    55  // info gets information of an IoTeX blockchain address
    56  func info(arg string) error {
    57  	addr := arg
    58  	if arg != address.StakingBucketPoolAddr && arg != address.RewardingPoolAddr {
    59  		var err error
    60  		addr, err = util.GetAddress(arg)
    61  		if err != nil {
    62  			return output.NewError(output.AddressError, "", err)
    63  		}
    64  	}
    65  	accountMeta, err := GetAccountMeta(addr)
    66  	if err != nil {
    67  		return output.NewError(output.APIError, "", err)
    68  	}
    69  	balance, ok := new(big.Int).SetString(accountMeta.Balance, 10)
    70  	if !ok {
    71  		return output.NewError(output.ConvertError, "", err)
    72  	}
    73  	ethAddr, err := address.FromString(addr)
    74  	if err != nil {
    75  		return output.NewError(output.ConvertError, "", err)
    76  	}
    77  	message := infoMessage{
    78  		Address:          addr,
    79  		EthAddress:       ethAddr.Hex(),
    80  		Balance:          util.RauToString(balance, util.IotxDecimalNum),
    81  		PendingNonce:     int(accountMeta.PendingNonce),
    82  		NumActions:       int(accountMeta.NumActions),
    83  		IsContract:       accountMeta.IsContract,
    84  		ContractByteCode: hex.EncodeToString(accountMeta.ContractByteCode),
    85  	}
    86  
    87  	fmt.Println((message.String()))
    88  	return nil
    89  }
    90  
    91  func (m *infoMessage) String() string {
    92  	if output.Format == "" {
    93  		return fmt.Sprintf("%s:\n%s", m.Address, output.JSONString(m))
    94  	}
    95  	return output.FormatString(output.Result, m)
    96  }