-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathaccountinfo.go
112 lines (101 loc) · 3.53 KB
/
accountinfo.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2022 IoTeX Foundation
// 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 (
"encoding/hex"
"encoding/json"
"fmt"
"log"
"math/big"
"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/iotexproject/iotex-core/v2/ioctl"
"github.com/iotexproject/iotex-core/v2/ioctl/config"
"github.com/iotexproject/iotex-core/v2/ioctl/util"
)
// Multi-language support
var (
_infoCmdUses = map[config.Language]string{
config.English: "info [ALIAS|ADDRESS]",
config.Chinese: "info [别名|地址]",
}
_infoCmdShorts = map[config.Language]string{
config.English: "Display an account's information",
config.Chinese: "显示账号信息",
}
_invalidAccountBalance = map[config.Language]string{
config.English: "invalid account balance",
config.Chinese: "无效的账户余额",
}
_failToGetAccountMeta = map[config.Language]string{
config.English: "failed to get account meta",
config.Chinese: "获取账户信息失败",
}
)
// NewAccountInfo represents the account info command
func NewAccountInfo(client ioctl.Client) *cobra.Command {
use, _ := client.SelectTranslation(_infoCmdUses)
short, _ := client.SelectTranslation(_infoCmdShorts)
failToGetAddress, _ := client.SelectTranslation(_failToGetAddress)
failToConvertStringIntoAddress, _ := client.SelectTranslation(_failToConvertStringIntoAddress)
_invalidAccountBalance, _ := client.SelectTranslation(_invalidAccountBalance)
_failToGetAccountMeta, _ := client.SelectTranslation(_failToGetAccountMeta)
return &cobra.Command{
Use: use,
Short: short,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
addr := args[0]
if addr != address.StakingBucketPoolAddr && addr != address.RewardingPoolAddr {
var err error
addr, err = client.AddressWithDefaultIfNotExist(addr)
if err != nil {
return errors.Wrap(err, failToGetAddress)
}
}
accountMeta, err := Meta(client, addr)
if err != nil {
return errors.Wrap(err, _failToGetAccountMeta)
}
balance, ok := new(big.Int).SetString(accountMeta.Balance, 10)
if !ok {
return errors.New(_invalidAccountBalance)
}
ethAddr, err := address.FromString(addr)
if err != nil {
return errors.Wrap(err, failToConvertStringIntoAddress)
}
message := infoMessage{
Address: addr,
EthAddress: ethAddr.Hex(),
Balance: util.RauToString(balance, util.IotxDecimalNum),
PendingNonce: int(accountMeta.PendingNonce),
NumActions: int(accountMeta.NumActions),
IsContract: accountMeta.IsContract,
ContractByteCode: hex.EncodeToString(accountMeta.ContractByteCode),
}
cmd.Println(message.String())
return nil
},
}
}
type infoMessage struct {
Address string `json:"address"`
EthAddress string `json:"ethAddress"`
Balance string `json:"balance"`
PendingNonce int `json:"pendingNonce"`
NumActions int `json:"numActions"`
IsContract bool `json:"isContract"`
ContractByteCode string `json:"contractByteCode"`
}
func (m *infoMessage) String() string {
byteAsJSON, err := json.MarshalIndent(m, "", " ")
if err != nil {
log.Panic(err)
}
return fmt.Sprint(string(byteAsJSON))
}