/
accountnonce.go
78 lines (69 loc) · 2.24 KB
/
accountnonce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package account
import (
"fmt"
"github.com/spf13/cobra"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
)
// Multi-language support
var (
nonceCmdShorts = map[config.Language]string{
config.English: "Get nonce of an account",
config.Chinese: "获取账户的nonce值",
}
nonceCmdUses = map[config.Language]string{
config.English: "nonce [ALIAS|ADDRESS]",
config.Chinese: "nonce [别名|地址]",
}
)
// accountNonceCmd represents the account nonce command
var accountNonceCmd = &cobra.Command{
Use: config.TranslateInLang(nonceCmdUses, config.UILanguage),
Short: config.TranslateInLang(nonceCmdShorts, config.UILanguage),
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
arg := ""
if len(args) == 1 {
arg = args[0]
}
err := nonce(arg)
return output.PrintError(err)
},
}
type nonceMessage struct {
Address string `json:"address"`
Nonce int `json:"nonce"`
PendingNonce int `json:"pendingNonce"`
}
// nonce gets nonce and pending nonce of an IoTeX blockchain address
func nonce(arg string) error {
addr, err := util.GetAddress(arg)
if err != nil {
return output.NewError(output.AddressError, "failed to get address", err)
}
accountMeta, err := GetAccountMeta(addr)
if err != nil {
return output.NewError(0, "", err)
}
message := nonceMessage{
Address: addr,
Nonce: int(accountMeta.Nonce),
PendingNonce: int(accountMeta.PendingNonce),
}
fmt.Println(message.String())
return nil
}
func (m *nonceMessage) String() string {
if output.Format == "" {
return fmt.Sprintf("%s:\nNonce: %d, Pending Nonce: %d",
m.Address, m.Nonce, m.PendingNonce)
}
return output.FormatString(output.Result, m)
}