github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/account/accountbalance.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  	"fmt"
    10  	"math/big"
    11  
    12  	"github.com/spf13/cobra"
    13  
    14  	ioAddress "github.com/iotexproject/iotex-address/address"
    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  	_balanceCmdUses = map[config.Language]string{
    23  		config.English: "balance [ALIAS|ADDRESS]",
    24  		config.Chinese: "balance [别名|地址]",
    25  	}
    26  	_balanceCmdShorts = map[config.Language]string{
    27  		config.English: "Get balance of an account",
    28  		config.Chinese: "查询账号余额",
    29  	}
    30  )
    31  
    32  // accountBalanceCmd represents the account balance command
    33  var accountBalanceCmd = &cobra.Command{
    34  	Use:   config.TranslateInLang(_balanceCmdUses, config.UILanguage),
    35  	Short: config.TranslateInLang(_balanceCmdShorts, config.UILanguage),
    36  	Args:  cobra.RangeArgs(0, 1),
    37  	RunE: func(cmd *cobra.Command, args []string) error {
    38  		cmd.SilenceUsage = true
    39  		arg := ""
    40  		if len(args) == 1 {
    41  			arg = args[0]
    42  		}
    43  		err := balance(arg)
    44  		return output.PrintError(err)
    45  	},
    46  }
    47  
    48  type balanceMessage struct {
    49  	Address string `json:"address"`
    50  	Balance string `json:"balance"`
    51  }
    52  
    53  // balance gets balance of an IoTeX blockchain address
    54  func balance(arg string) error {
    55  	addr := arg
    56  	if arg != ioAddress.StakingBucketPoolAddr && arg != ioAddress.RewardingPoolAddr {
    57  		var err error
    58  		addr, err = util.GetAddress(arg)
    59  		if err != nil {
    60  			return output.NewError(output.AddressError, "", err)
    61  		}
    62  	}
    63  	accountMeta, err := GetAccountMeta(addr)
    64  	if err != nil {
    65  		return output.NewError(0, "", err) // TODO: undefined error
    66  	}
    67  	balance, ok := new(big.Int).SetString(accountMeta.Balance, 10)
    68  	if !ok {
    69  		return output.NewError(output.ConvertError, "", err)
    70  	}
    71  	message := balanceMessage{
    72  		Address: addr,
    73  		Balance: util.RauToString(balance, util.IotxDecimalNum),
    74  	}
    75  	fmt.Println((message.String()))
    76  	return nil
    77  }
    78  
    79  func (m *balanceMessage) String() string {
    80  	if output.Format == "" {
    81  		return fmt.Sprintf("%s: %s IOTX", m.Address, m.Balance)
    82  	}
    83  	return output.FormatString(output.Result, m)
    84  }