-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
etherscan-provider.ts
454 lines (378 loc) · 16.9 KB
/
etherscan-provider.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
"use strict";
import { BlockTag, TransactionRequest, TransactionResponse } from "@ethersproject/abstract-provider";
import { hexlify, hexValue, isHexString } from "@ethersproject/bytes";
import { Network, Networkish } from "@ethersproject/networks";
import { deepCopy, defineReadOnly } from "@ethersproject/properties";
import { accessListify } from "@ethersproject/transactions";
import { ConnectionInfo, fetchJson } from "@ethersproject/web";
import { showThrottleMessage } from "./formatter";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
import { BaseProvider } from "./base-provider";
// The transaction has already been sanitized by the calls in Provider
function getTransactionPostData(transaction: TransactionRequest): Record<string, string> {
const result: Record<string, string> = { };
for (let key in transaction) {
if ((<any>transaction)[key] == null) { continue; }
let value = (<any>transaction)[key];
if (key === "type" && value === 0) { continue; }
// Quantity-types require no leading zero, unless 0
if ((<any>{ type: true, gasLimit: true, gasPrice: true, maxFeePerGs: true, maxPriorityFeePerGas: true, nonce: true, value: true })[key]) {
value = hexValue(hexlify(value));
} else if (key === "accessList") {
value = "[" + accessListify(value).map((set) => {
return `{address:"${ set.address }",storageKeys:["${ set.storageKeys.join('","') }"]}`;
}).join(",") + "]";
} else {
value = hexlify(value);
}
result[key] = value;
}
return result;
}
function getResult(result: { status?: number, message?: string, result?: any }): any {
// getLogs, getHistory have weird success responses
if (result.status == 0 && (result.message === "No records found" || result.message === "No transactions found")) {
return result.result;
}
if (result.status != 1 || typeof(result.message) !== "string" || !result.message.match(/^OK/)) {
const error: any = new Error("invalid response");
error.result = JSON.stringify(result);
if ((result.result || "").toLowerCase().indexOf("rate limit") >= 0) {
error.throttleRetry = true;
}
throw error;
}
return result.result;
}
function getJsonResult(result: { jsonrpc: string, result?: any, error?: { code?: number, data?: any, message?: string} } ): any {
// This response indicates we are being throttled
if (result && (<any>result).status == 0 && (<any>result).message == "NOTOK" && (result.result || "").toLowerCase().indexOf("rate limit") >= 0) {
const error: any = new Error("throttled response");
error.result = JSON.stringify(result);
error.throttleRetry = true;
throw error;
}
if (result.jsonrpc != "2.0") {
// @TODO: not any
const error: any = new Error("invalid response");
error.result = JSON.stringify(result);
throw error;
}
if (result.error) {
// @TODO: not any
const error: any = new Error(result.error.message || "unknown error");
if (result.error.code) { error.code = result.error.code; }
if (result.error.data) { error.data = result.error.data; }
throw error;
}
return result.result;
}
// The blockTag was normalized as a string by the Provider pre-perform operations
function checkLogTag(blockTag: string): number | "latest" {
if (blockTag === "pending") { throw new Error("pending not supported"); }
if (blockTag === "latest") { return blockTag; }
return parseInt(blockTag.substring(2), 16);
}
function checkError(method: string, error: any, transaction: any): any {
// Undo the "convenience" some nodes are attempting to prevent backwards
// incompatibility; maybe for v6 consider forwarding reverts as errors
if (method === "call" && error.code === Logger.errors.SERVER_ERROR) {
const e = error.error;
// Etherscan keeps changing their string
if (e && (e.message.match(/reverted/i) || e.message.match(/VM execution error/i))) {
// Etherscan prefixes the data like "Reverted 0x1234"
let data = e.data;
if (data) { data = "0x" + data.replace(/^.*0x/i, ""); }
if (isHexString(data)) { return data; }
logger.throwError("missing revert data in call exception", Logger.errors.CALL_EXCEPTION, {
error, data: "0x"
});
}
}
// Get the message from any nested error structure
let message = error.message;
if (error.code === Logger.errors.SERVER_ERROR) {
if (error.error && typeof(error.error.message) === "string") {
message = error.error.message;
} else if (typeof(error.body) === "string") {
message = error.body;
} else if (typeof(error.responseText) === "string") {
message = error.responseText;
}
}
message = (message || "").toLowerCase();
// "Insufficient funds. The account you tried to send transaction from does not have enough funds. Required 21464000000000 and got: 0"
if (message.match(/insufficient funds/)) {
logger.throwError("insufficient funds for intrinsic transaction cost", Logger.errors.INSUFFICIENT_FUNDS, {
error, method, transaction
});
}
// "Transaction with the same hash was already imported."
if (message.match(/same hash was already imported|transaction nonce is too low|nonce too low/)) {
logger.throwError("nonce has already been used", Logger.errors.NONCE_EXPIRED, {
error, method, transaction
});
}
// "Transaction gas price is too low. There is another transaction with same nonce in the queue. Try increasing the gas price or incrementing the nonce."
if (message.match(/another transaction with same nonce/)) {
logger.throwError("replacement fee too low", Logger.errors.REPLACEMENT_UNDERPRICED, {
error, method, transaction
});
}
if (message.match(/execution failed due to an exception|execution reverted/)) {
logger.throwError("cannot estimate gas; transaction may fail or may require manual gas limit", Logger.errors.UNPREDICTABLE_GAS_LIMIT, {
error, method, transaction
});
}
throw error;
}
export class EtherscanProvider extends BaseProvider{
readonly baseUrl: string;
readonly apiKey: string | null;
constructor(network?: Networkish, apiKey?: string) {
super(network);
defineReadOnly(this, "baseUrl", this.getBaseUrl());
defineReadOnly(this, "apiKey", apiKey || null);
}
getBaseUrl(): string {
switch(this.network ? this.network.name: "invalid") {
case "homestead":
return "https:/\/api.etherscan.io";
case "goerli":
return "https:/\/api-goerli.etherscan.io";
case "sepolia":
return "https:/\/api-sepolia.etherscan.io";
case "matic":
return "https:/\/api.polygonscan.com";
case "maticmum":
return "https:/\/api-testnet.polygonscan.com";
case "arbitrum":
return "https:/\/api.arbiscan.io";
case "arbitrum-goerli":
return "https:/\/api-goerli.arbiscan.io";
case "optimism":
return "https:/\/api-optimistic.etherscan.io";
case "optimism-goerli":
return "https:/\/api-goerli-optimistic.etherscan.io";
default:
}
return logger.throwArgumentError("unsupported network", "network", this.network.name);
}
getUrl(module: string, params: Record<string, string>): string {
const query = Object.keys(params).reduce((accum, key) => {
const value = params[key];
if (value != null) {
accum += `&${ key }=${ value }`
}
return accum
}, "");
const apiKey = ((this.apiKey) ? `&apikey=${ this.apiKey }`: "");
return `${ this.baseUrl }/api?module=${ module }${ query }${ apiKey }`;
}
getPostUrl(): string {
return `${ this.baseUrl }/api`;
}
getPostData(module: string, params: Record<string, any>): Record<string, any> {
params.module = module;
params.apikey = this.apiKey;
return params;
}
async fetch(module: string, params: Record<string, any>, post?: boolean): Promise<any> {
const url = (post ? this.getPostUrl(): this.getUrl(module, params));
const payload = (post ? this.getPostData(module, params): null);
const procFunc = (module === "proxy") ? getJsonResult: getResult;
this.emit("debug", {
action: "request",
request: url,
provider: this
});
const connection: ConnectionInfo = {
url: url,
throttleSlotInterval: 1000,
throttleCallback: (attempt: number, url: string) => {
if (this.isCommunityResource()) {
showThrottleMessage();
}
return Promise.resolve(true);
}
};
let payloadStr: string = null;
if (payload) {
connection.headers = { "content-type": "application/x-www-form-urlencoded; charset=UTF-8" };
payloadStr = Object.keys(payload).map((key) => {
return `${ key }=${ payload[key] }`
}).join("&");
}
const result = await fetchJson(connection, payloadStr, procFunc || getJsonResult);
this.emit("debug", {
action: "response",
request: url,
response: deepCopy(result),
provider: this
});
return result;
}
async detectNetwork(): Promise<Network> {
return this.network;
}
async perform(method: string, params: any): Promise<any> {
switch (method) {
case "getBlockNumber":
return this.fetch("proxy", { action: "eth_blockNumber" });
case "getGasPrice":
return this.fetch("proxy", { action: "eth_gasPrice" });
case "getBalance":
// Returns base-10 result
return this.fetch("account", {
action: "balance",
address: params.address,
tag: params.blockTag
});
case "getTransactionCount":
return this.fetch("proxy", {
action: "eth_getTransactionCount",
address: params.address,
tag: params.blockTag
});
case "getCode":
return this.fetch("proxy", {
action: "eth_getCode",
address: params.address,
tag: params.blockTag
});
case "getStorageAt":
return this.fetch("proxy", {
action: "eth_getStorageAt",
address: params.address,
position: params.position,
tag: params.blockTag
});
case "sendTransaction":
return this.fetch("proxy", {
action: "eth_sendRawTransaction",
hex: params.signedTransaction
}, true).catch((error) => {
return checkError("sendTransaction", error, params.signedTransaction);
});
case "getBlock":
if (params.blockTag) {
return this.fetch("proxy", {
action: "eth_getBlockByNumber",
tag: params.blockTag,
boolean: (params.includeTransactions ? "true": "false")
});
}
throw new Error("getBlock by blockHash not implemented");
case "getTransaction":
return this.fetch("proxy", {
action: "eth_getTransactionByHash",
txhash: params.transactionHash
});
case "getTransactionReceipt":
return this.fetch("proxy", {
action: "eth_getTransactionReceipt",
txhash: params.transactionHash
});
case "call": {
if (params.blockTag !== "latest") {
throw new Error("EtherscanProvider does not support blockTag for call");
}
const postData = getTransactionPostData(params.transaction);
postData.module = "proxy";
postData.action = "eth_call";
try {
return await this.fetch("proxy", postData, true);
} catch (error) {
return checkError("call", error, params.transaction);
}
}
case "estimateGas": {
const postData = getTransactionPostData(params.transaction);
postData.module = "proxy";
postData.action = "eth_estimateGas";
try {
return await this.fetch("proxy", postData, true);
} catch (error) {
return checkError("estimateGas", error, params.transaction);
}
}
case "getLogs": {
const args: Record<string, any> = { action: "getLogs" }
if (params.filter.fromBlock) {
args.fromBlock = checkLogTag(params.filter.fromBlock);
}
if (params.filter.toBlock) {
args.toBlock = checkLogTag(params.filter.toBlock);
}
if (params.filter.address) {
args.address = params.filter.address;
}
// @TODO: We can handle slightly more complicated logs using the logs API
if (params.filter.topics && params.filter.topics.length > 0) {
if (params.filter.topics.length > 1) {
logger.throwError("unsupported topic count", Logger.errors.UNSUPPORTED_OPERATION, { topics: params.filter.topics });
}
if (params.filter.topics.length === 1) {
const topic0 = params.filter.topics[0];
if (typeof(topic0) !== "string" || topic0.length !== 66) {
logger.throwError("unsupported topic format", Logger.errors.UNSUPPORTED_OPERATION, { topic0: topic0 });
}
args.topic0 = topic0;
}
}
const logs: Array<any> = await this.fetch("logs", args);
// Cache txHash => blockHash
let blocks: { [tag: string]: string } = {};
// Add any missing blockHash to the logs
for (let i = 0; i < logs.length; i++) {
const log = logs[i];
if (log.blockHash != null) { continue; }
if (blocks[log.blockNumber] == null) {
const block = await this.getBlock(log.blockNumber);
if (block) {
blocks[log.blockNumber] = block.hash;
}
}
log.blockHash = blocks[log.blockNumber];
}
return logs;
}
case "getEtherPrice":
if (this.network.name !== "homestead") { return 0.0; }
return parseFloat((await this.fetch("stats", { action: "ethprice" })).ethusd);
default:
break;
}
return super.perform(method, params);
}
// Note: The `page` page parameter only allows pagination within the
// 10,000 window available without a page and offset parameter
// Error: Result window is too large, PageNo x Offset size must
// be less than or equal to 10000
async getHistory(addressOrName: string | Promise<string>, startBlock?: BlockTag, endBlock?: BlockTag): Promise<Array<TransactionResponse>> {
const params = {
action: "txlist",
address: (await this.resolveName(addressOrName)),
startblock: ((startBlock == null) ? 0: startBlock),
endblock: ((endBlock == null) ? 99999999: endBlock),
sort: "asc"
};
const result = await this.fetch("account", params);
return result.map((tx: any) => {
["contractAddress", "to"].forEach(function(key) {
if (tx[key] == "") { delete tx[key]; }
});
if (tx.creates == null && tx.contractAddress != null) {
tx.creates = tx.contractAddress;
}
const item = this.formatter.transactionResponse(tx);
if (tx.timeStamp) { item.timestamp = parseInt(tx.timeStamp); }
return item;
});
}
isCommunityResource(): boolean {
return (this.apiKey == null);
}
}