forked from danakt/node-etherscan-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
599 lines (552 loc) · 16.2 KB
/
index.js
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
const BigNumber = require('bignumber.js')
const MODULES = require('./constants/modules')
const ACTIONS = require('./constants/actions')
const UNITS = require('./constants/units')
const NETWORKS = require('./constants/networks')
const etherConvert = require('./utils/etherConvert')
const getHex = require('./utils/getHex')
const createRequest = require('./utils/createRequest')
class EtherscanApi {
/**
* @class EtherscanApi
* @constructor
* @param {string} [token] Etherscan API token
* @param {string} [networkName=MAIN] Network name. Available:
* main, ropsten, kovan and rinkeby
*/
constructor(token = '', networkName = 'MAIN') {
const netName = networkName.toUpperCase()
const network = netName in NETWORKS ? netName : 'MAIN'
this.token = token
this.network = network
this.host = NETWORKS[network]
}
/**
* Creates request
* @private
* @param {object} params Query params
* @return {Promise}
*/
_createRequest(params) {
return createRequest(this.host, {
...params,
apikey: this.token
})
}
/**
* Returns Ether balance for a single address
* @param {string} address Address
* @param {object} [options]
* @param {string} [options.unit=wei] Balance unit
* @param {string} [options.tag=latest]
* @returns {Promise<string>}
* @todo Write test for options
*/
getAccountBalance(address, options = {}) {
const { unit = 'wei', tag = 'latest' } = options
return this._createRequest({
module: MODULES.ACCOUNT,
action: ACTIONS.GET_BALANCE,
tag,
address
}).then(resp => {
// Converting balance to another unit
return unit === 'wei' || !(unit in UNITS)
? resp
: etherConvert(resp, 'wei', unit)
})
}
/**
* Returns Ether balance for multiple addresses in a single call.
* Up to a maximum of 20 accounts in a single batch.
* @param {Array<string>} addresses List of addresses
* @param {object} [options]
* @param {string} [options.unit=wei] Balance unit
* @param {string} [options.tag=latest]
* @return {Promise<object>}
* @todo Write test for options
*/
getAccountBalances(addresses, options = {}) {
const { unit = 'wei', tag = 'latest' } = options
return this._createRequest({
apikey: this.token,
module: MODULES.ACCOUNT,
action: ACTIONS.GET_BALANCE_MULTI,
address: (addresses || []).join(','),
tag
}).then(resp => {
// Converting balances to another unit
return unit === 'wei' || !(unit in UNITS)
? resp
: resp.map(item => {
return {
account: item.account,
balance: etherConvert(item.balance, 'wei', unit)
}
})
})
}
/**
* Returns a list of 'Normal' transactions by address.
* Returns up to a maximum of the last 10000 transactions only.
* @param {string} address Contract address
* @param {object} [options]
* @param {object} [options.startBlock] Starting block number to retrieve
* results
* @param {string|number} [options.endBlock] Ending block number to retrieve
* results
* @param {number} [options.offset] Max records to return
* @param {number} [options.page] Page number
* @param {"asc"|"desc"} [options.sort] Sort type
* @returns {Promise<object[]>}
*/
getTransactions(address, options = {}) {
const { startBlock, endBlock, offset, page, sort } = options
return this._createRequest({
module: MODULES.ACCOUNT,
action: ACTIONS.GET_TRANSACTIONS_LIST,
address,
startblock: startBlock,
endblock: endBlock,
offset: offset,
page: page,
sort: sort
})
}
/**
* Returns a list of 'Internal' Transactions by Address.
* Returns up to a maximum of the last 10000 transactions only.
* @param {string} address Contract address
* @param {object} [options]
* @param {string|number} [options.startBlock] Starting block number to
* retrieve results
* @param {string|number} [options.endBlock] Ending block number to
* retrieve results
* @param {string|number} [options.offset] Max records to return
* @param {string|number} [options.page] Page number
* @param {"asc"|"desc"} [options.sort] Sort type
* @returns {Promise<object[]>}
*/
getInternalTransactions(address, options = {}) {
const { startBlock, endBlock, offset, page, sort } = options
return this._createRequest({
module: MODULES.ACCOUNT,
action: ACTIONS.GET_TRANSACTIONS_LIST_INTERNAL,
address,
startblock: startBlock,
endblock: endBlock,
offset: offset,
page: page,
sort: sort
})
}
/**
* Returns a list of 'Internal' Transactions by Address
* @param txhash Contract address
* @returns {Promise<object[]>}
*/
getInternalTransactionsByHash(txhash) {
return this._createRequest({
module: MODULES.ACCOUNT,
action: ACTIONS.GET_TRANSACTIONS_LIST_INTERNAL,
txhash
})
}
/**
* List of blocks mined by address
* @param {string} address Miner address
* @param {object} [options]
* @param {"blocks"|"uncles"} [options.type] Type of block:
* blocks (full blocks only) or uncles (uncle blocks only)
* @param {number} [options.offset] Max records to return
* @param {number} [options.page] Page number
* @returns {Promise<object[]>}
*/
getMinedBlocks(address, options = {}) {
const { type = 'blocks', offset, page } = options
return this._createRequest({
module: MODULES.ACCOUNT,
action: ACTIONS.GET_MINED_BLOCKS,
blocktype: type,
address,
offset,
page
})
}
/**
* Returns Contract ABI
* @param address
* @returns {Promsie<object[]>}
*/
getContractAbi(address) {
return this._createRequest({
module: MODULES.CONTRACT,
action: ACTIONS.GET_ABI,
address
}).then(resp => {
return JSON.parse(resp)
})
}
/**
* Checks contract execution status (if there was an error during contract
* execution). "isError": "0" = Pass, "isError": "1" = Error during contract
* execution
* @param {string} txhash Contract address
* @returns {Promise<object>}
*/
getContractExecutionStatus(txhash) {
return this._createRequest({
module: MODULES.TRANSACTION,
action: ACTIONS.GET_CONTRACT_STATUS,
txhash
})
}
/**
* Checks transaction receipt status (only applicable for post byzantium fork
* transactions). Status: 0 = Fail, 1 = Pass. Will return null/empty value
* for pre-byzantium fork
* @param {string} txhash Transaction address
* @returns {Promise<object>}
*/
getTransactionStatus(txhash) {
return this._createRequest({
module: MODULES.TRANSACTION,
action: ACTIONS.GET_TRANSACTION_STATUS,
txhash
})
}
/**
* Get block and uncle rewards by block number
* @param {number} blockNumber The number of the block
*/
getBlockReward(blockNumber) {
return this._createRequest({
module: MODULES.BLOCK,
action: ACTIONS.GET_BLOCK_REWARD,
blockno: blockNumber
})
}
/**
* Returns events logs.
* The Event Log API was designed to provide an alternative to the native
* eth_getLogs. Topic Operator choices are either 'and' or 'or' and are
* restricted to the above choices only. For performance and security
* considerations, only the first 1000 results are return.
* @param {string} address
* @param {object} options
* @param {number} options.fromBlock Start block number (integer, NOT hex)
* @param {number|'latest'} options.toBlock End block number or "latest"
* (earliest and pending is NOT supported yet)
* @param {string} options.topic0 Topic 0
* @param {"and"|"or"} [options.topic01operator] Operator between topic0 &
* topic1
* @param {string} [options.topic1] Topic 1
* @param {"and"|"or"} [options.topic12operator] Operator between topic1 &
* topic2
* @param {string} [options.topic2] Topic 2
* @param {"and"|"or"} [options.topic23operator] Operator between topic2 &
* topic3
* @param {string} [options.topic3] Topic 3
* @param {"and"|"or"} [options.topic02operator] Operator between topic0 &
* topic2
* @return {Promise<object>}
*/
getEventsLogs(address, options = {}) {
const {
fromBlock,
toBlock,
topic0,
topic01operator,
topic1,
topic12operator,
topic2,
topic23operator,
topic3,
topic02operator
} = options
return this._createRequest({
module: MODULES.LOGS,
action: ACTIONS.GET_LOGS,
fromBlock,
toBlock,
topic0,
topic0_1_opr: topic01operator,
topic1,
topic1_2_opr: topic12operator,
topic2,
topic2_3_opr: topic23operator,
topic3,
topic0_2_opr: topic02operator
})
}
/**
* Returns the number of the most recent block
* @returns {Promise<number>}
*/
getRecentBlockNumber() {
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_RECENT_BLOCK_NUMBER
}).then(blockNumberHex => {
return parseInt(blockNumberHex, 16)
})
}
/**
* Returns information about a block by block number
* @param {number} blockNumber Block number
* @returns {Promise<object>}
*/
getBlockByNumber(blockNumber) {
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_BLOCK_BY_NUMBER,
tag: '0x' + blockNumber.toString(16),
boolean: 'true'
})
}
/**
* Returns information about a uncle by block number and index
* @param {number} blockNumber
* @param {number} [index=0]
* @returns {Promise<object>}
*/
getUncleByBlockNumberAndIndex(blockNumber, index = 0) {
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_UNCLE_BLOCK_NUMBER_AND_INDEX,
tag: getHex(blockNumber),
index: getHex(index)
})
}
/**
* Returns the number of transactions in a block from a block matching the
* given block number
* @param {number} blockNumber
* @returns {Promise<number>}
*/
getBlockTransactionCount(blockNumber) {
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_BLOCK_TX_COUNT_BY_NUMBER,
tag: getHex(blockNumber)
}).then(countHex => {
return parseInt(countHex, 16)
})
}
/**
* Returns the information about a transaction requested by transaction hash
* @param {string} txhash Transaction hash
* @returns {Promise<object>}
*/
getTransactionByHash(txhash) {
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_TRANSACTION_BY_HASH,
txhash
})
}
/**
* Returns information about a transaction by block number and transaction
* index position
* @param {number} blockNumber
* @param {number} [index=0]
* @returns {Promise<object>}
*/
getTransactionByBlockNumberAndIndex(blockNumber, index = 0) {
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_TX_BY_BLOCK_NUMBER_AND_INDEX,
tag: getHex(blockNumber),
index: getHex(index)
})
}
/**
* Returns the number of transactions sent from an address
* @param {string} address Transaction address
* @param {object} [options]
* @param {string} [options.tag=latest]
* @returns {Promise<number>}
*/
getTransactionCount(address, options = {}) {
const { tag = 'latest' } = options
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_TRANSACTION_COUNT,
tag,
address
}).then(countHex => {
return parseInt(countHex, 16)
})
}
/**
* Creates new message call transaction or a contract creation for signed
* transactions
* @param {string} hex Raw hex encoded transaction that you want to send
* @return {Promise<void>}
*/
sendRawTransaction(hex) {
this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.SEND_RAW_TRANSACTION,
hex
})
}
/**
* Returns the receipt of a transaction by transaction hash
* @param {string} txhash Transaction hash
* @returns {Promise<object>}
*/
getTransactionReceipt(txhash) {
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_TRANSACTION_RECEIPT,
txhash
})
}
/**
* Executes a new message call immediately without creating a transaction on
* the block chain
* @param {string} to Address to execute from
* @param {string} data Data to transfer
* @param {object} [options]
* @param {string} [options.tag=latest]
* @returns {Promise<string>}
*/
call(to, data, options = {}) {
const { tag = 'latest' } = options
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.CALL,
tag,
to,
data
})
}
/**
* Returns code at a given address
* @param {string} address
* @param {object} [options]
* @param {string} [options.tag=latest]
* @returns {Promise<string>}
*/
getCode(address, options = {}) {
const { tag = 'latest' } = options
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_CODE,
address,
tag
})
}
/**
* Returns the value from a storage position at a given address.
* @param {string} address
* @param {number} position
* @param {object} options
* @param {string} [options.tag=latest]
* @returns {Promise<string>}
*/
getStorageAt(address, position, options = {}) {
const { tag = 'latest' } = options
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_STORAGE_AT,
address,
position: getHex(position),
tag
})
}
/**
* Returns the current price per gas (in wei by default)
* @param {object} [options]
* @param {string} [options.unit=wei] Unit of gas
* @returns {Promise<string>}
*/
getGasPrice(options = {}) {
const { unit = 'wei' } = options
return this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.GET_GAS_PRICE
}).then(priceHex => {
const priceBN = new BigNumber(priceHex)
const priceFixed = priceBN.toFixed()
// If unit is wei, don't convert gas price
if (unit === 'wei') {
return priceFixed
}
// else covert to specified ether unit
return etherConvert(priceFixed, 'wei', unit)
})
}
/**
* Makes a call or transaction, which won't be added to the blockchain and
* returns the used gas, which can be used for estimating the used gas
* @param {string} toAddress Address to get code from
* @param {object} options
* @param {string} options.value Storage position
* @param {string} options.gasPrice Gas price in wei
* @param {string} options.gas
* @return {Promise<void>}
*/
estimateGas(toAddress, { value, gasPrice, gas }) {
this._createRequest({
module: MODULES.PROXY,
action: ACTIONS.ESTIMATE_GAS,
to,
value,
gasPrice,
gas
})
}
/**
* Returns ERC20-Token total supply by contract address
* @param {string} contractAddress
* @returns {Promise<string>}
*/
getTokenByContractAddress(contractAddress) {
return this._createRequest({
module: MODULES.STATS,
action: ACTIONS.GET_TOKEN_BY_CONTRACT,
contractaddress: contractAddress
})
}
/**
* Returns ERC20-Token account balance by token's contract address
* @param {string} contractAddress
* @param {string} address
* @param {object} [options]
* @param {object} [options.tag=latest]
* @returns {Promise<string>}
*/
getTokenBalanceByContractAddress(contractAddress, address, options = {}) {
const { tag = 'latest' } = options
return this._createRequest({
module: MODULES.ACCOUNT,
action: ACTIONS.GET_TOKEN_BALANCE_BY_CONTRACT,
contractaddress: contractAddress,
address,
tag
})
}
/**
* Returns total supply of Ether
* @returns {Promise<string>}
*/
getTotalEtherSupply() {
return this._createRequest({
module: MODULES.STATS,
action: ACTIONS.GET_TOTAL_ETHER_SUPPLY
})
}
/**
* Returns Ether last price
* @returns {Promise<object>}
*/
getEtherLastPrice() {
return this._createRequest({
module: MODULES.STATS,
action: ACTIONS.GET_LAST_ETHER_PRICE
})
}
}
module.exports = EtherscanApi