This repository has been archived by the owner on Dec 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcmd_query.go
232 lines (199 loc) · 5.97 KB
/
cmd_query.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"context"
"encoding/hex"
"fmt"
"log"
"strconv"
"github.com/urfave/cli"
"github.com/the729/go-libra/example/utils"
"github.com/the729/go-libra/language/stdscript"
"github.com/the729/go-libra/types"
"github.com/the729/lcs"
)
func cmdQueryLedgerInfo(ctx *cli.Context) error {
c, err := newClientFromWaypointOrFile(ServerAddr, TrustedWaypoint, KnownVersionFile)
if err != nil {
log.Fatal(err)
}
defer c.Close()
defer saveClientState(c, KnownVersionFile)
ledgerInfo, err := c.QueryLedgerInfo(context.Background())
if err != nil {
return err
}
log.Printf("Ledger info: version %d, epoch %d, time %d",
ledgerInfo.GetVersion(),
ledgerInfo.GetEpochNum(),
ledgerInfo.GetTimestampUsec(),
)
return nil
}
func cmdQueryAccountState(ctx *cli.Context) error {
c, err := newClientFromWaypointOrFile(ServerAddr, TrustedWaypoint, KnownVersionFile)
if err != nil {
log.Fatal(err)
}
defer c.Close()
defer saveClientState(c, KnownVersionFile)
wallet, err := LoadAccounts(WalletFile)
if err != nil {
log.Fatal(err)
}
addr := ctx.Args().Get(0)
account, err := wallet.GetAccount(addr)
if err != nil {
return err
}
accountState, err := c.QueryAccountState(context.Background(), account.Address)
if err != nil {
return err
}
ledgerInfo := accountState.GetLedgerInfo()
log.Printf("Ledger info: version %d, time %d", ledgerInfo.GetVersion(), ledgerInfo.GetTimestampUsec())
if accountState.IsNil() {
fmt.Println("Account is not present in the ledger.")
} else {
log.Printf("Account version: %d", accountState.GetVersion())
ar, br, err := accountState.GetAccountBlob().GetLibraResources()
if err != nil {
log.Printf("Account does not contain libra coin resource, err: %v", err)
return nil
}
addr := accountState.GetAddress()
log.Printf("Address: %v", hex.EncodeToString(addr[:]))
log.Printf("Balance: %d", br.Coin)
log.Printf("Sequence Number: %d", ar.SequenceNumber)
log.Printf("SentEventsCount: %d", ar.SentEvents.Count)
log.Printf(" Key: %x", ar.SentEvents.Key)
log.Printf("ReceivedEventsCount: %d", ar.ReceivedEvents.Count)
log.Printf(" Key: %x", ar.ReceivedEvents.Key)
log.Printf("DelegatedWithdrawalCapability: %v", ar.DelegatedWithdrawalCapability)
log.Printf("Authentication key: %v", hex.EncodeToString(ar.AuthenticationKey))
log.Printf("Event generator: %v", ar.EventGenerator)
}
return nil
}
func cmdQueryTransactionRange(ctx *cli.Context) error {
c, err := newClientFromWaypointOrFile(ServerAddr, TrustedWaypoint, KnownVersionFile)
if err != nil {
log.Fatal(err)
}
defer c.Close()
defer saveClientState(c, KnownVersionFile)
start, err := strconv.Atoi(ctx.Args().Get(0))
if err != nil {
return err
}
limit, err := strconv.Atoi(ctx.Args().Get(1))
if err != nil {
return err
}
if limit > 100 {
log.Printf("Limit>100, set to 100.")
limit = 100
}
provenTxnList, err := c.QueryTransactionRange(context.Background(), uint64(start), uint64(limit), true)
if err != nil {
log.Fatal(err)
}
for _, provenTxn := range provenTxnList.GetTransactions() {
utils.PrintTxn(provenTxn)
}
return nil
}
func cmdQueryTransactionByAccountSeq(ctx *cli.Context) error {
c, err := newClientFromWaypointOrFile(ServerAddr, TrustedWaypoint, KnownVersionFile)
if err != nil {
log.Fatal(err)
}
defer c.Close()
defer saveClientState(c, KnownVersionFile)
wallet, err := LoadAccounts(WalletFile)
if err != nil {
log.Fatal(err)
}
account, err := wallet.GetAccount(ctx.Args().Get(0))
if err != nil {
return err
}
sequence, err := strconv.Atoi(ctx.Args().Get(1))
if err != nil {
return err
}
provenTxn, err := c.QueryTransactionByAccountSeq(context.Background(), account.Address, uint64(sequence), true)
if err != nil {
log.Fatal(err)
}
utils.PrintTxn(provenTxn)
return nil
}
func cmdQueryEvents(ctx *cli.Context) error {
c, err := newClientFromWaypointOrFile(ServerAddr, TrustedWaypoint, KnownVersionFile)
if err != nil {
log.Fatal(err)
}
defer c.Close()
defer saveClientState(c, KnownVersionFile)
wallet, err := LoadAccounts(WalletFile)
if err != nil {
log.Fatal(err)
}
account, err := wallet.GetAccount(ctx.Args().Get(0))
if err != nil {
return err
}
evType := ctx.Args().Get(1)
start, err := strconv.Atoi(ctx.Args().Get(2))
if err != nil {
return err
}
ordering := ctx.Args().Get(3)
limit, err := strconv.Atoi(ctx.Args().Get(4))
if err != nil {
return err
}
ap := &types.AccessPath{
Address: account.Address,
}
switch evType {
case "sent":
ap.Path = types.AccountSentEventPath()
case "received":
ap.Path = types.AccountReceivedEventPath()
default:
return fmt.Errorf("unknown event type: %s, should be either sent or received", evType)
}
var ascending bool
switch ordering {
case "asc":
ascending = true
case "desc":
ascending = false
default:
return fmt.Errorf("unknown ordering: %s, should be either asc or desc", evType)
}
evs, err := c.QueryEventsByAccessPath(context.Background(), ap, uint64(start), ascending, uint64(limit))
if err != nil {
log.Fatal(err)
}
for i, ev := range evs {
log.Printf("#%d: txn #%d event #%d", i, ev.GetTransactionVersion(), ev.GetEventIndex())
evBody := ev.GetEvent().Value.(*types.ContractEventV0)
log.Printf(" Key: %s", hex.EncodeToString(evBody.Key))
log.Printf(" Seq number: %d", evBody.SequenceNumber)
if len(evBody.Data) > 30 {
log.Printf(" Raw event: %s ...", hex.EncodeToString(evBody.Data[:30]))
} else {
log.Printf(" Raw event: %s", hex.EncodeToString(evBody.Data))
}
pev := &stdscript.PaymentEvent{}
if err := lcs.Unmarshal(evBody.Data, pev); err != nil {
log.Printf(" (Unknown event type)")
} else {
log.Printf(" Amount (microLibra): %d", pev.Amount)
log.Printf(" Opponent address: %s", hex.EncodeToString(pev.Address[:]))
}
}
return nil
}