github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountnonce.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  
    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  )
    17  
    18  // Multi-language support
    19  var (
    20  	_nonceCmdShorts = map[config.Language]string{
    21  		config.English: "Get nonce of an account",
    22  		config.Chinese: "获取账户的nonce值",
    23  	}
    24  	_nonceCmdUses = map[config.Language]string{
    25  		config.English: "nonce [ALIAS|ADDRESS]",
    26  		config.Chinese: "nonce [别名|地址]",
    27  	}
    28  )
    29  
    30  // NewAccountNonce represents the account nonce command
    31  func NewAccountNonce(client ioctl.Client) *cobra.Command {
    32  	use, _ := client.SelectTranslation(_nonceCmdUses)
    33  	short, _ := client.SelectTranslation(_nonceCmdShorts)
    34  	failToGetAddress, _ := client.SelectTranslation(_failToGetAddress)
    35  	failToGetAccountMeta, _ := client.SelectTranslation(_failToGetAccountMeta)
    36  
    37  	cmd := &cobra.Command{
    38  		Use:   use,
    39  		Short: short,
    40  		Args:  cobra.RangeArgs(0, 1),
    41  		RunE: func(cmd *cobra.Command, args []string) error {
    42  			cmd.SilenceUsage = true
    43  			arg := ""
    44  			if len(args) == 1 {
    45  				arg = args[0]
    46  			}
    47  
    48  			addr, err := client.AddressWithDefaultIfNotExist(arg)
    49  			if err != nil {
    50  				return errors.Wrap(err, failToGetAddress)
    51  			}
    52  			accountMeta, err := Meta(client, addr)
    53  			if err != nil {
    54  				return errors.Wrap(err, failToGetAccountMeta)
    55  			}
    56  
    57  			cmd.Println(fmt.Sprintf("%s:\nPending Nonce: %d",
    58  				addr, accountMeta.PendingNonce))
    59  			return nil
    60  		},
    61  	}
    62  	return cmd
    63  }