-
Notifications
You must be signed in to change notification settings - Fork 320
/
accountactions.go
202 lines (183 loc) · 5.83 KB
/
accountactions.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// 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 (
"context"
"fmt"
"strconv"
"strings"
"github.com/golang/protobuf/ptypes"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/rodaine/table"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)
var countLimit *uint64
// Multi-language support
var (
actionsCmdShorts = map[config.Language]string{
config.English: "Show the list of actions for an account",
config.Chinese: "显示账户的操作列表",
}
actionsCmdUses = map[config.Language]string{
config.English: "actions (ALIAS|ADDRESS) [SKIP]",
config.Chinese: "actions (ALIAS|ADDRESS) [SKIP]",
}
flagCountUsages = map[config.Language]string{
config.English: "choose a count limit",
config.Chinese: "选择一个计数限制",
}
)
// accountActionsCmd represents the account sign command
var accountActionsCmd = &cobra.Command{
Use: config.TranslateInLang(actionsCmdUses, config.UILanguage),
Short: config.TranslateInLang(actionsCmdShorts, config.UILanguage),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := accountActions(args)
return output.PrintError(err)
},
}
func init() {
countLimit = accountActionsCmd.Flags().Uint64("limit", 15, config.TranslateInLang(flagCountUsages, config.UILanguage))
}
func accountActions(args []string) error {
var skip uint64
var err error
if len(args) == 2 {
skip, err = strconv.ParseUint(args[1], 10, 64)
if err != nil {
return output.NewError(output.ConvertError, "failed to convert skip ", nil)
}
}
addr, err := util.Address(args[0])
if err != nil {
return output.NewError(output.AddressError, "failed to get address", err)
}
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
if err != nil {
return output.NewError(output.NetworkError, "failed to connect to endpoint", err)
}
defer conn.Close()
cli := iotexapi.NewAPIServiceClient(conn)
ctx := context.Background()
jwtMD, err := util.JwtAuth()
if err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
}
requestGetAccount := iotexapi.GetAccountRequest{
Address: addr,
}
accountResponse, err := cli.GetAccount(ctx, &requestGetAccount)
if err != nil {
return output.NewError(output.APIError, "failed to get account", err)
}
numActions := accountResponse.AccountMeta.GetNumActions()
fmt.Println("Total:", numActions)
requestGetAction := iotexapi.GetActionsRequest{
Lookup: &iotexapi.GetActionsRequest_ByAddr{
ByAddr: &iotexapi.GetActionsByAddressRequest{
Address: addr,
Start: numActions - *countLimit - skip,
Count: *countLimit,
},
},
}
response, err := cli.GetActions(ctx, &requestGetAction)
if err != nil {
sta, ok := status.FromError(err)
if ok {
return output.NewError(output.APIError, sta.Message(), nil)
}
return output.NewError(output.NetworkError, "failed to invoke GetActions api", err)
}
if len(response.ActionInfo) == 0 {
return output.NewError(output.APIError, "no action info returned", nil)
}
showFields := []interface{}{
"Hash",
"Time",
"Status",
"Sender",
"Type",
"To",
"Contract",
"Amount",
}
tbl := table.New(showFields...)
for i := range response.ActionInfo {
k := len(response.ActionInfo) - 1 - i
actionInfo := response.ActionInfo[k]
requestGetReceipt := &iotexapi.GetReceiptByActionRequest{ActionHash: actionInfo.ActHash}
responseReceipt, err := cli.GetReceiptByAction(ctx, requestGetReceipt)
if err != nil {
sta, ok := status.FromError(err)
if ok {
fmt.Println(output.NewError(output.APIError, sta.Message(), nil))
} else {
fmt.Println(output.NewError(output.NetworkError, "failed to invoke GetReceiptByAction api", err))
}
continue
}
amount := "0"
transfer := actionInfo.Action.Core.GetTransfer()
if transfer != nil {
amount, _ = util.StringToIOTX(transfer.Amount)
}
tbl.AddRow(
actionInfo.ActHash,
getActionTime(actionInfo),
iotextypes.ReceiptStatus_name[int32(responseReceipt.ReceiptInfo.Receipt.GetStatus())],
actionInfo.Sender,
getActionTypeString(actionInfo),
getActionTo(actionInfo),
getActionContract(responseReceipt),
amount+" IOTX",
)
}
tbl.Print()
return nil
}
func getActionContract(responseReceipt *iotexapi.GetReceiptByActionResponse) string {
contract := responseReceipt.ReceiptInfo.Receipt.GetContractAddress()
if contract == "" {
contract = "-"
}
return contract
}
func getActionTypeString(actionInfo *iotexapi.ActionInfo) string {
actionType := fmt.Sprintf("%T", actionInfo.Action.Core.GetAction())
return strings.TrimLeft(actionType, "*iotextypes.ActionCore_")
}
func getActionTo(actionInfo *iotexapi.ActionInfo) string {
recipient := ""
switch getActionTypeString(actionInfo) {
case "Transfer":
transfer := actionInfo.Action.Core.GetTransfer()
recipient = transfer.GetRecipient()
case "Execution":
execution := actionInfo.Action.Core.GetExecution()
recipient = execution.GetContract()
}
if recipient == "" {
recipient = "-"
}
return recipient
}
func getActionTime(actionInfo *iotexapi.ActionInfo) string {
if actionInfo.Timestamp != nil {
if ts, err := ptypes.Timestamp(actionInfo.Timestamp); err == nil {
return ts.String()
}
}
return ""
}