forked from icon-project/icon-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIconService.ts
605 lines (548 loc) · 20.6 KB
/
IconService.ts
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
* Copyright 2021 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import BigNumber from "bignumber.js";
import Request from "./transport/jsonrpc/Request";
import Wallet from "./Wallet";
import Builder from "./builder";
import SignedTransaction from "./SignedTransaction";
import * as Validator from "./data/Validator";
import * as Formatter from "./data/Formatter";
import { DataError } from "./Exception";
import * as Util from "./data/Util";
import Amount from "./data/Amount";
import * as Converter from "./data/Converter";
import HttpProvider from "./transport/http/HttpProvider";
import * as Hexadecimal from "./data/Hexadecimal";
import HttpCall from "./transport/http/client/HttpCall";
import Block from "./data/Formatter/Block";
import ScoreApiList from "./data/Formatter/ScoreApiList";
import Transaction from "./data/Formatter/Transaction";
import TransactionResult from "./data/Formatter/TransactionResult";
import { Call } from "./builder/call/Call";
import { IcxTransaction } from "./builder/transaction/IcxTransaction";
import { MessageTransaction } from "./builder/transaction/MessageTransaction";
import { DepositTransaction } from "./builder/transaction/DepositTransaction";
import { DeployTransaction } from "./builder/transaction/DeployTransaction";
import { CallTransaction } from "./builder/transaction/CallTransaction";
import { Hash } from "./types/hash";
import BTPNetworkInfo from "./data/Formatter/BTPNetworkInfo";
import BTPNetworkTypeInfo from "./data/Formatter/BTPNetworkTypeInfo";
import BTPSourceInformation from "./data/Formatter/BTPSourceInformation";
/**
* Class which provides APIs of ICON network.
*/
export default class IconService {
public static IconAmount = Amount;
public static IconBuilder = Builder;
public static IconConverter = Converter;
public static IconWallet = Wallet;
public static IconUtil = Util;
public static SignedTransaction = SignedTransaction;
public static HttpProvider = HttpProvider;
public static IconHexadecimal = Hexadecimal;
public static IconValidator = Validator;
private provider: HttpProvider;
/**
* Creates an instance of IconService.
* @param {HttpProvider} provider - The HttpProvider instance.
*/
constructor(provider: HttpProvider) {
this.provider = provider;
}
/**
* Get the total number of issued coins.
* @param {Hash} [height] - block Height.
* @return {HttpCall} The HttpCall instance for icx_getTotalSupply JSON-RPC API request.
*/
getTotalSupply(height?: Hash): HttpCall<BigNumber> {
let params;
if (height === undefined) {
params = null;
} else {
params = { height };
}
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "icx_getTotalSupply", params);
return this.provider.request<BigNumber>(request, Converter.toBigNumber);
}
/**
* Get the balance of the address.
* @param {string} address - The EOA or SCORE address.
* @param {Hash} [height] - block Height.
* @return {HttpCall} The HttpCall instance for icx_getBalance JSON-RPC API request.
*/
getBalance(address: string, height?: Hash): HttpCall<BigNumber> {
let params;
if (height === undefined) {
params = { address };
} else {
params = { address, height };
}
if (!Validator.isAddress(address)) {
const error = new DataError(`[${address}] is not valid address.`);
throw error.toString();
} else {
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "icx_getBalance", params);
return this.provider.request<BigNumber>(request, Converter.toBigNumber);
}
}
/**
* @description Get the block information.
* @param {BigNumber} value The value of block number
* @return {object} The HttpCall instance for icx_getBlockByHeight JSON-RPC API request.
*/
getBlockByHeight(value: BigNumber): HttpCall<Block> {
if (Validator.isNonNegative(value)) {
const requestId = Util.getCurrentTime();
const params = { height: Converter.toHex(value) };
const request = new Request(requestId, "icx_getBlockByHeight", params);
return this.provider.request(request, Formatter.toBlock);
}
const error = new DataError(`[${value}] is an unrecognized block height.`);
throw error.toString();
}
/**
* @description Get the block information.
* @param {string} value The value of block hash
* @return {object} The HttpCall instance for icx_getBlockByHash JSON-RPC API request.
*/
getBlockByHash(value: string): HttpCall<Block> {
if (Validator.isValidHash(value)) {
const requestId = Util.getCurrentTime();
const params = { hash: value };
const request = new Request(requestId, "icx_getBlockByHash", params);
return this.provider.request(request, Formatter.toBlock);
}
const error = new DataError(`[${value}] is an unrecognized block hash.`);
throw error.toString();
}
/**
* @description Get the last block information.
* @return {object} The HttpCall instance for icx_getLastBlock JSON-RPC API request.
*/
getLastBlock(): HttpCall<Block> {
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "icx_getLastBlock", null);
return this.provider.request(request, Formatter.toBlock);
}
/**
* @description Get the SCORE API list.
* @param {string} address SCORE address
* @param {Hash} [height] block Height
* @return {array} The HttpCall instance for icx_getScoreApi JSON-RPC API request.
*/
getScoreApi(address: string, height?: Hash): HttpCall<ScoreApiList> {
let params;
if (height === undefined) {
params = { address };
} else {
params = { address, height };
}
if (!Validator.isScoreAddress(address)) {
const error = new DataError(`[${address}] is not a valid SCORE address.`);
throw error.toString();
} else {
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "icx_getScoreApi", params);
return this.provider.request(request, Formatter.toScoreApiList);
}
}
/**
* @description Get the SCORE status
* @param {string} address SCORE address
* @param {Hash} [height] block Height
* @return {array} The HttpCall instance for icx_getScoreStatus JSON-RPC API request.
*/
getScoreStatus(address: string, height?: Hash): HttpCall<ScoreApiList> {
let params;
if (height === undefined) {
params = { address };
} else {
params = { address, height };
}
if (!Validator.isScoreAddress(address)) {
const error = new DataError(`[${address}] is not a valid SCORE address.`);
throw error.toString();
} else {
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "icx_getScoreStatus", params);
return this.provider.request(request);
}
}
/**
* Get the transaction information.
* @param {string} hash - The transaction hash.
* @return {HttpCall} The HttpCall instance for icx_getTransactionByHash JSON-RPC API request.
*/
getTransaction(hash: string): HttpCall<Transaction> {
if (!Validator.isValidHash(hash)) {
const error = new DataError(`[${hash}] is an unrecognized hash value.`);
throw error.toString();
} else {
const requestId = Util.getCurrentTime();
const params = { txHash: hash };
const request = new Request(
requestId,
"icx_getTransactionByHash",
params
);
return this.provider.request(request, Formatter.toTransaction);
}
}
/**
* Get the result of transaction by transaction hash.
* @param {string} hash - The transaction hash.
* @return {HttpCall} The HttpCall instance for icx_getTransactionResult JSON-RPC API request.
*/
getTransactionResult(hash: string): HttpCall<TransactionResult> {
if (!Validator.isValidHash(hash)) {
const error = new DataError(`[${hash}] is an unrecognized hash value.`);
throw error.toString();
} else {
const requestId = Util.getCurrentTime();
const params = { txHash: hash };
const request = new Request(
requestId,
"icx_getTransactionResult",
params
);
return this.provider.request(request, Formatter.toTransactionResult);
}
}
/**
* Send a transaction that changes the states of address.
* @param {SignedTransaction} signedTransaction - Parameters including signature.
* @return {HttpCall} The HttpCall instance for icx_sendTransaction JSON-RPC API request.
*/
sendTransaction(signedTransaction: SignedTransaction): HttpCall<string> {
if (!Validator.isSignedTransaction(signedTransaction)) {
const error = new DataError("Transaction object is invalid.");
throw error.toString();
} else {
const requestId = Util.getCurrentTime();
const params = signedTransaction.getProperties();
const request = new Request(requestId, "icx_sendTransaction", params);
return this.provider.request(request);
}
}
/**
* Returns an estimated step of how much step is necessary to allow the transaction to complete.
* @param {SignedTransaction} transaction - Parameters
* @return {HttpCall} The HttpCall instance for debug_estimateStep JSON-RPC API request.
*/
estimateStep(
transaction:
| IcxTransaction
| MessageTransaction
| DepositTransaction
| DeployTransaction
| CallTransaction
): HttpCall<BigNumber> {
const rawTx = Converter.toRawTransaction(transaction);
if (!Validator.checkTxData(rawTx)) {
const error = new DataError("Transaction object is invalid.");
throw error.toString();
} else {
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "debug_estimateStep", rawTx);
return this.provider.request(request);
}
}
/**
* Calls a SCORE API just for reading.
* @param {Call} call - The call instance exported by CallBuilder
* @return {HttpCall} The HttpCall instance for icx_call JSON-RPC API request.
*/
call(call: Call): HttpCall<any> {
if (!Validator.isCall(call)) {
const error = new DataError("Call object is invalid.");
throw error.toString();
} else {
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "icx_call", call);
return this.provider.request(request);
}
}
/**
* sends a transaction like `icx_sendTransaction`, then it will wait for the
result of it for specified time. If the timeout isn't set by user, it uses
`defaultWaitTimeout` of icon node.
* @param {SignedTransaction} signedTransaction - Parameters including signature.
* @return {HttpCall} The HttpCall instance for icx_sendTransactionAndWait JSON-RPC API request.
*/
sendTransactionAndWait(
signedTransaction: SignedTransaction
): HttpCall<string> {
if (!Validator.isSignedTransaction(signedTransaction)) {
const error = new DataError("Transaction object is invalid.");
throw error.toString();
}
const requestId = Util.getCurrentTime();
const params = signedTransaction.getProperties();
const request = new Request(
requestId,
"icx_sendTransactionAndWait",
params
);
return this.provider.request(request);
}
/**
* It will wait for the result of the transaction for specified time.
* If the timeout isn't set by user, it uses `defaultWaitTimeout` of icon node.
* @param {string} hash - The transaction hash.
* @return {HttpCall} The HttpCall instance for icx_waitTransactionResult JSON-RPC API request.
*/
waitTransactionResult(hash: string): HttpCall<TransactionResult> {
if (!Validator.isValidHash(hash)) {
const error = new DataError(`[${hash}] is an unrecognized hash value.`);
throw error.toString();
}
const requestId = Util.getCurrentTime();
const params = { txHash: hash };
const request = new Request(requestId, "icx_waitTransactionResult", params);
return this.provider.request(request, Formatter.toTransactionResult);
}
/**
* Get data by hash.
It can be used to retrieve data based on the hash algorithm (SHA3-256).
Following data can be retrieved by a hash.
* BlockHeader with the hash of the block
* Validators with BlockHeader.NextValidatorsHash
* Votes with BlockHeader.VotesHash
* etc…
* @param {string} hash - The hash value of the data to retrieve
* @return {HttpCall} The HttpCall instance for icx_getDataByHash JSON-RPC API request.
*/
getDataByHash(hash: string): HttpCall<TransactionResult> {
if (!Validator.isValidHash(hash)) {
const error = new DataError(`[${hash}] is an unrecognized hash value.`);
throw error.toString();
}
const requestId = Util.getCurrentTime();
const params = { hash };
const request = new Request(requestId, "icx_getDataByHash", params);
return this.provider.request(request);
}
/**
* Get block header for specified height.
* @param {BigNumber | string} height - The height of the block.
* @return {HttpCall} The HttpCall instance for icx_getBlockHeaderByHeight JSON-RPC API request.
*/
getBlockHeaderByHeight(
height: string | BigNumber
): HttpCall<TransactionResult> {
if (!Validator.isNonNegative(height)) {
const error = new DataError(
`[${height}] is an unrecognized block height`
);
throw error.toString();
}
const requestId = Util.getCurrentTime();
const params = { height: Converter.toHex(height) };
const request = new Request(
requestId,
"icx_getBlockHeaderByHeight",
params
);
return this.provider.request(request);
}
/**
* Get votes for the block specified by height.
* @param {BigNumber} height - The height of the block.
* @return {HttpCall} The HttpCall instance for icx_getVotesByHeight JSON-RPC API request.
*/
getVotesByHeight(height: string | BigNumber): HttpCall<TransactionResult> {
if (!Validator.isNonNegative(height)) {
const error = new DataError(
`[${height}] is an unrecognized block height`
);
throw error.toString();
}
const requestId = Util.getCurrentTime();
const params = { height: Converter.toHex(height) };
const request = new Request(requestId, "icx_getVotesByHeight", params);
return this.provider.request(request);
}
/**
* Get proof for the receipt
* @param {string} hash - The hash value of the block including the result
* @param {string} index - Index of the receipt in the block
* @return {HttpCall} The HttpCall instance for icx_getProofForResult JSON-RPC API request.
*/
getProofForResult(
hash: string,
index: string | BigNumber
): HttpCall<TransactionResult> {
if (!Validator.isValidHash(hash)) {
const error = new DataError(`[${hash}] is an unrecognized hash value.`);
throw error.toString();
}
if (!Validator.isNonNegative(index)) {
const error = new DataError(`index must be non-negative number`);
throw error.toString();
}
const requestId = Util.getCurrentTime();
const params = { hash, index: Converter.toHex(index) };
const request = new Request(requestId, "icx_getProofForResult", params);
return this.provider.request(request);
}
/**
* Get proof for the receipt and the events in it
* @param {string} hash - The hash value of the block including the result
* @param index - Index of the receipt in the block
* @param events - List of indexes of the events in the receipt
* @return {HttpCall} The HttpCall instance for icx_getProofForEvents JSON-RPC API request.
*/
getProofForEvents(
hash: string,
index: string | BigNumber,
events: Array<string | BigNumber>
): HttpCall<TransactionResult> {
if (!Validator.isValidHash(hash)) {
const error = new DataError(`[${hash}] is an unrecognized hash value.`);
throw error.toString();
}
if (!Validator.isNonNegative(index)) {
const error = new DataError(`index must be none-negative number`);
throw error.toString();
}
if (!events.every(Validator.isNonNegative)) {
const error = new DataError(
`All event index must be none-negative number`
);
throw error.toString();
}
const requestId = Util.getCurrentTime();
const params = {
hash,
index: Converter.toHex(index),
events: events.map(Converter.toHex),
};
const request = new Request(requestId, "icx_getProofForEvents", params);
return this.provider.request(request);
}
/**
* Get BTP network information.
* @param id - network id
* @param height - Main block height
* @return {HttpCall} The HttpCall instance for btp_getNetworkInfo JSON-RPC API request.
*/
getBTPNetworkInfo(
id: string | BigNumber,
height?: string | BigNumber
): HttpCall<BTPNetworkInfo> {
let params;
if (height === undefined) params = { id };
else params = { id, height };
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "btp_getNetworkInfo", params);
return this.provider.request(request, (data) => new BTPNetworkInfo(data));
}
/**
* Get BTP network type information.
* @param id - Network type id
* @param height - Main block height
* @return {HttpCall} The HttpCall instance for btp_getNetworkTypeInfo JSON-RPC API request.
*/
getBTPNetworkTypeInfo(
id: string | BigNumber,
height?: string | BigNumber
): HttpCall<BTPNetworkTypeInfo> {
let params;
if (height === undefined) params = { id };
else params = { id, height };
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "btp_getNetworkTypeInfo", params);
return this.provider.request(
request,
(data) => new BTPNetworkTypeInfo(data)
);
}
/**
* Get BTP messages
* @param networkID - BTP network ID
* @param height - Main block height
* @return {HttpCall} The HttpCall instance for btp_getMessages JSON-RPC API request.
*/
getBTPMessages(
networkID: string | BigNumber,
height: string | BigNumber
): HttpCall<Array<string>> {
const params = { networkID, height };
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "btp_getMessages", params);
return this.provider.request<Array<string>>(request);
}
/**
* Get BTP block header
* @param networkID - Network id
* @param height - Main block height
* @return {HttpCall} The HttpCall instance for btp_getHeader JSON-RPC API request.
*/
getBTPHeader(
networkID: string | BigNumber,
height: string | BigNumber
): HttpCall<string> {
const params = { networkID, height };
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "btp_getHeader", params);
return this.provider.request<string>(request);
}
/**
* Get BTP block proof
* @param networkID - Network id
* @param height - Main block height
* @return {HttpCall} The HttpCall instance for btp_getProof JSON-RPC API request.
*/
getBTPProof(
networkID: string | BigNumber,
height: string | BigNumber
): HttpCall<string> {
const params = { networkID, height };
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "btp_getProof", params);
return this.provider.request<string>(request);
}
/**
* Get source network information
* @return {HttpCall} The HttpCall instance for btp_getSourceInformation JSON-RPC API request.
*/
getBTPSourceInformation(): HttpCall<BTPSourceInformation> {
const requestId = Util.getCurrentTime();
const request = new Request(requestId, "btp_getSourceInformation", null);
return this.provider.request(
request,
(data) => new BTPSourceInformation(data)
);
}
/**
* Get the transaction trace.
* @param {string} hash - The transaction hash.
* @return {HttpCall} The HttpCall instance for debug_getTrace JSON-RPC API request.
*/
getTrace(hash: string): HttpCall<any> {
if (!Validator.isValidHash(hash)) {
const error = new DataError(`[${hash}] is an unrecognized hash value.`);
throw error.toString();
} else {
const requestId = Util.getCurrentTime();
const params = { txHash: hash };
const request = new Request(requestId, "debug_getTrace", params);
return this.provider.request(request);
}
}
}