-
Notifications
You must be signed in to change notification settings - Fork 6
/
rpc.go
558 lines (455 loc) · 13.9 KB
/
rpc.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package zillean
import (
"errors"
"github.com/GincoInc/jsonrpc"
)
// RPC represents a JSON-RPC API client object.
type RPC struct {
client *jsonrpc.RPCClient
}
// NewRPC returns a new RPC object.
func NewRPC(endpoint string) *RPC {
return &RPC{
client: jsonrpc.NewRPCClient(endpoint),
}
}
// GetNetworkID returns the network ID of the specified zilliqa node.
func (r *RPC) GetNetworkID() (string, error) {
resp, err := r.client.Call("GetNetworkId", []interface{}{})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetBlockchainInfo returns statistics about the specified zilliqa node.
func (r *RPC) GetBlockchainInfo() (*BlockchainInfo, error) {
resp, err := r.client.Call("GetBlockchainInfo", []interface{}{})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result BlockchainInfo
resp.GetObject(&result)
return &result, nil
}
// GetShardingStructure returns the current sharding structure of the network from the specified network's lookup node.
func (r *RPC) GetShardingStructure() (*ShardingStructure, error) {
resp, err := r.client.Call("GetShardingStructure", []interface{}{})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result ShardingStructure
resp.GetObject(&result)
return &result, nil
}
// GetDsBlock returns details of a Directory Service block by block number.
func (r *RPC) GetDsBlock(blockNumber string) (*DsBlock, error) {
resp, err := r.client.Call("GetDsBlock", []interface{}{blockNumber})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result DsBlock
resp.GetObject(&result)
return &result, nil
}
// GetLatestDsBlock returns details of the most recent Directory Service block.
func (r *RPC) GetLatestDsBlock() (*DsBlock, error) {
resp, err := r.client.Call("GetLatestDsBlock", []interface{}{})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result DsBlock
resp.GetObject(&result)
return &result, nil
}
// GetNumDSBlocks returns the number of Directory Service blocks in the network so far. This is represented as a String.
func (r *RPC) GetNumDSBlocks() (string, error) {
resp, err := r.client.Call("GetNumDSBlocks", []interface{}{})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetDSBlockRate returns the current Directory Service blockrate per second.
func (r *RPC) GetDSBlockRate() (float64, error) {
resp, err := r.client.Call("GetDSBlockRate", []interface{}{})
if err != nil {
return 0, err
}
if resp.Error != nil {
return 0, errors.New(resp.Error.Message)
}
var result float64
resp.GetObject(&result)
return result, nil
}
// DSBlockListing returns a paginated list of Directory Service blocks.
// Pass in page number as parameter.
// Returns a maxPages variable that specifies the max number of pages.
// 1 - latest blocks, maxPages - oldest blocks.
func (r *RPC) DSBlockListing(pageNumber int64) (*ListedBlocks, error) {
resp, err := r.client.Call("DSBlockListing", []interface{}{pageNumber})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result ListedBlocks
resp.GetObject(&result)
return &result, nil
}
// GetTxBlock returns details of a Transaction block by block number.
func (r *RPC) GetTxBlock(blockNumber string) (*TxBlock, error) {
resp, err := r.client.Call("GetTxBlock", []interface{}{blockNumber})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result TxBlock
resp.GetObject(&result)
return &result, nil
}
// GetLatestTxBlock returns details of the most recent Transaction block.
func (r *RPC) GetLatestTxBlock() (*TxBlock, error) {
resp, err := r.client.Call("GetLatestTxBlock", []interface{}{})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result TxBlock
resp.GetObject(&result)
return &result, nil
}
// GetNumTxBlocks returns the number of Transaction blocks in the network so far, this is represented as String.
func (r *RPC) GetNumTxBlocks() (string, error) {
resp, err := r.client.Call("GetNumTxBlocks", []interface{}{})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetTxBlockRate returns the current Transaction blockrate per second.
func (r *RPC) GetTxBlockRate() (float64, error) {
resp, err := r.client.Call("GetTxBlockRate", []interface{}{})
if err != nil {
return 0, err
}
if resp.Error != nil {
return 0, errors.New(resp.Error.Message)
}
var result float64
resp.GetObject(&result)
return result, nil
}
// TxBlockListing returns a paginated list of Transaction blocks.
// Pass in page number as parameter.
// Returns a maxPages variable that specifies the max number of pages.
// 1 - latest blocks, maxPages - oldest blocks.
func (r *RPC) TxBlockListing(pageNumber int64) (*ListedBlocks, error) {
resp, err := r.client.Call("TxBlockListing", []interface{}{pageNumber})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result ListedBlocks
resp.GetObject(&result)
return &result, nil
}
// GetNumTransactions returns the number of Transactions validated in the network so far. This is represented as a String.
func (r *RPC) GetNumTransactions() (string, error) {
resp, err := r.client.Call("GetNumTransactions", []interface{}{})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetTransactionRate returns the current Transaction rate of the network.
func (r *RPC) GetTransactionRate() (float64, error) {
resp, err := r.client.Call("GetTransactionRate", []interface{}{})
if err != nil {
return 0, err
}
if resp.Error != nil {
return 0, errors.New(resp.Error.Message)
}
var result float64
resp.GetObject(&result)
return result, nil
}
// GetCurrentMiniEpoch returns the number of TX epochs in the network so far represented as String.
func (r *RPC) GetCurrentMiniEpoch() (string, error) {
resp, err := r.client.Call("GetCurrentMiniEpoch", []interface{}{})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetCurrentDSEpoch returns the number of DS epochs in the network so far represented as String.
func (r *RPC) GetCurrentDSEpoch() (string, error) {
resp, err := r.client.Call("GetCurrentDSEpoch", []interface{}{})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetPrevDifficulty returns the minimum shard difficulty of the previous block, this is represented as an Number.
func (r *RPC) GetPrevDifficulty() (int64, error) {
resp, err := r.client.Call("GetPrevDifficulty", []interface{}{})
if err != nil {
return 0, err
}
if resp.Error != nil {
return 0, errors.New(resp.Error.Message)
}
var result int64
resp.GetObject(&result)
return result, nil
}
// GetPrevDSDifficulty returns the minimum DS difficulty of the previous block, this is represented as an Number.
func (r *RPC) GetPrevDSDifficulty() (int64, error) {
resp, err := r.client.Call("GetPrevDSDifficulty", []interface{}{})
if err != nil {
return 0, err
}
if resp.Error != nil {
return 0, errors.New(resp.Error.Message)
}
var result int64
resp.GetObject(&result)
return result, nil
}
// CreateTransaction create a new Transaction.
// See https://github.com/Zilliqa/Zilliqa-JavaScript-Library/#createtransactionjson in javascript
// for an example of how to construct the transaction object.
func (r *RPC) CreateTransaction(rawTx RawTransaction, signature string) (string, error) {
resp, err := r.client.Call("CreateTransaction", []interface{}{RawTransaction{
Version: rawTx.Version,
Nonce: rawTx.Nonce,
To: rawTx.To,
Amount: rawTx.Amount,
PubKey: rawTx.PubKey,
GasPrice: rawTx.GasPrice,
GasLimit: rawTx.GasLimit,
Code: rawTx.Code,
Data: rawTx.Data,
Signature: signature,
}})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result struct {
Info string `json:"Info"`
TranID string `json:"TranID"`
}
resp.GetObject(&result)
return result.TranID, nil
}
// GetTransaction returns details of a Transaction by its hash.
func (r *RPC) GetTransaction(txHash string) (*Transaction, error) {
resp, err := r.client.Call("GetTransaction", []interface{}{txHash})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result Transaction
resp.GetObject(&result)
return &result, nil
}
// GetRecentTransactions returns the most recent transactions (upto 100) accepted by the specified zilliqa node.
func (r *RPC) GetRecentTransactions() (*RecentTransactions, error) {
resp, err := r.client.Call("GetRecentTransactions", []interface{}{})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result RecentTransactions
resp.GetObject(&result)
return &result, nil
}
// GetTransactionsForTxBlock returns the transactions included within a micro-block created by a specific shard.
func (r *RPC) GetTransactionsForTxBlock(blockNumber string) ([][]string, error) {
resp, err := r.client.Call("GetTransactionsForTxBlock", []interface{}{blockNumber})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result [][]string
resp.GetObject(&result)
return result, nil
}
// GetNumTxnsTxEpoch returns the number of transactions in this Transaction epoch, this is represented as String.
func (r *RPC) GetNumTxnsTxEpoch() (string, error) {
resp, err := r.client.Call("GetNumTxnsTxEpoch", []interface{}{})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetNumTxnsDSEpoch returns the number of transactions in this Directory Service epoch, this is represented as String.
func (r *RPC) GetNumTxnsDSEpoch() (string, error) {
resp, err := r.client.Call("GetNumTxnsDSEpoch", []interface{}{})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetMinimumGasPrice returns the minimum gas price of the last DS epoch represented as String. This is measured in the smallest price unit Qa (10^-12 Zil) in Zilliqa.
func (r *RPC) GetMinimumGasPrice() (string, error) {
resp, err := r.client.Call("GetMinimumGasPrice", []interface{}{})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetSmartContractCode returns the Scilla code of a smart contract address.
func (r *RPC) GetSmartContractCode(contractAddress string) (string, error) {
resp, err := r.client.Call("GetSmartContractCode", []interface{}{contractAddress})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result struct {
Code string `json:"code"`
}
resp.GetObject(&result)
return result.Code, nil
}
// GetSmartContractInit returns the initialization parameters (immutable) of a given smart contract address.
func (r *RPC) GetSmartContractInit(contractAddress string) ([]SmartContractState, error) {
resp, err := r.client.Call("GetSmartContractInit", []interface{}{contractAddress})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result []SmartContractState
resp.GetObject(&result)
return result, nil
}
// GetSmartContractState returns the state variables (mutable) of a smart contract address.
func (r *RPC) GetSmartContractState(contractAddress string) ([]SmartContractState, error) {
resp, err := r.client.Call("GetSmartContractState", []interface{}{contractAddress})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result []SmartContractState
resp.GetObject(&result)
return result, nil
}
// GetSmartContracts returns the list of smart contracts created by an address.
func (r *RPC) GetSmartContracts(address string) ([]SmartContract, error) {
resp, err := r.client.Call("GetSmartContracts", []interface{}{address})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result []SmartContract
resp.GetObject(&result)
return result, nil
}
// GetContractAddressFromTransactionID returns a smart contract address of 20 bytes from a transaction ID, represented as a String .
func (r *RPC) GetContractAddressFromTransactionID(txHash string) (string, error) {
resp, err := r.client.Call("GetContractAddressFromTransactionID", []interface{}{txHash})
if err != nil {
return "", err
}
if resp.Error != nil {
return "", errors.New(resp.Error.Message)
}
var result string
resp.GetObject(&result)
return result, nil
}
// GetBalance returns the balance and nonce of a given address.
func (r *RPC) GetBalance(address string) (*Balance, error) {
resp, err := r.client.Call("GetBalance", []interface{}{address})
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
}
var result Balance
resp.GetObject(&result)
return &result, nil
}