ethers.js/packages/providers/lib/etherscan-provider.js

501 lines
24 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
2021-03-07 18:24:04 -05:00
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
2019-05-14 18:48:48 -04:00
return extendStatics(d, b);
};
return function (d, b) {
2021-03-07 18:24:04 -05:00
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
2019-05-14 18:48:48 -04:00
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
2019-11-20 18:57:38 +09:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
2019-05-14 18:48:48 -04:00
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-07 18:24:04 -05:00
exports.EtherscanProvider = void 0;
2019-05-14 18:48:48 -04:00
var bytes_1 = require("@ethersproject/bytes");
var properties_1 = require("@ethersproject/properties");
2021-04-17 22:41:09 -04:00
var transactions_1 = require("@ethersproject/transactions");
2019-05-14 18:48:48 -04:00
var web_1 = require("@ethersproject/web");
2020-07-14 02:33:30 -04:00
var formatter_1 = require("./formatter");
2019-08-02 02:10:58 -04:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-14 18:48:48 -04:00
var base_provider_1 = require("./base-provider");
// The transaction has already been sanitized by the calls in Provider
2020-10-22 20:01:18 -04:00
function getTransactionPostData(transaction) {
var result = {};
2019-05-14 18:48:48 -04:00
for (var key in transaction) {
if (transaction[key] == null) {
continue;
}
2021-03-30 15:22:45 -04:00
var value = transaction[key];
2020-10-22 20:01:18 -04:00
// Quantity-types require no leading zero, unless 0
2021-03-30 15:22:45 -04:00
if ({ type: true, gasLimit: true, gasPrice: true, nonce: true, value: true }[key]) {
value = bytes_1.hexValue(bytes_1.hexlify(value));
}
else if (key === "accessList") {
2021-05-17 16:19:36 -04:00
value = "[" + transactions_1.accessListify(value).map(function (set) {
2021-04-17 22:41:09 -04:00
return "{address:\"" + set.address + "\",storageKeys:[\"" + set.storageKeys.join('","') + "\"]}";
}).join(",") + "]";
2021-03-30 15:22:45 -04:00
}
else {
value = bytes_1.hexlify(value);
2019-05-14 18:48:48 -04:00
}
2020-10-22 20:01:18 -04:00
result[key] = value;
2019-05-14 18:48:48 -04:00
}
2020-10-22 20:01:18 -04:00
return result;
2019-05-14 18:48:48 -04:00
}
function getResult(result) {
// 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 || result.message != "OK") {
var error = new Error("invalid response");
error.result = JSON.stringify(result);
2020-07-14 02:33:30 -04:00
if ((result.result || "").toLowerCase().indexOf("rate limit") >= 0) {
error.throttleRetry = true;
}
2019-05-14 18:48:48 -04:00
throw error;
}
return result.result;
}
function getJsonResult(result) {
2020-07-14 02:33:30 -04:00
// This response indicates we are being throttled
if (result && result.status == 0 && result.message == "NOTOK" && (result.result || "").toLowerCase().indexOf("rate limit") >= 0) {
var error = new Error("throttled response");
error.result = JSON.stringify(result);
error.throttleRetry = true;
throw error;
}
2019-05-14 18:48:48 -04:00
if (result.jsonrpc != "2.0") {
// @TODO: not any
var error = new Error("invalid response");
error.result = JSON.stringify(result);
throw error;
}
if (result.error) {
// @TODO: not any
var error = 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) {
if (blockTag === "pending") {
throw new Error("pending not supported");
}
if (blockTag === "latest") {
return blockTag;
}
return parseInt(blockTag.substring(2), 16);
}
2020-02-17 20:22:33 -05:00
var defaultApiKey = "9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB";
2020-09-16 03:08:36 -04:00
function checkError(method, error, transaction) {
2020-11-22 22:44:33 -05:00
// 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_1.Logger.errors.SERVER_ERROR) {
var e = error.error;
if (e && e.message.match("reverted") && bytes_1.isHexString(e.data)) {
return e.data;
}
}
2020-09-16 03:08:36 -04:00
// Get the message from any nested error structure
2020-09-11 02:10:58 -04:00
var message = error.message;
2020-09-16 03:08:36 -04:00
if (error.code === logger_1.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_1.Logger.errors.INSUFFICIENT_FUNDS, {
error: error, method: method, transaction: transaction
});
}
// "Transaction with the same hash was already imported."
if (message.match(/same hash was already imported|transaction nonce is too low/)) {
logger.throwError("nonce has already been used", logger_1.Logger.errors.NONCE_EXPIRED, {
error: error, method: method, transaction: 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_1.Logger.errors.REPLACEMENT_UNDERPRICED, {
error: error, method: method, transaction: transaction
});
2020-09-11 02:10:58 -04:00
}
if (message.match(/execution failed due to an exception/)) {
logger.throwError("cannot estimate gas; transaction may fail or may require manual gas limit", logger_1.Logger.errors.UNPREDICTABLE_GAS_LIMIT, {
2020-09-16 03:08:36 -04:00
error: error, method: method, transaction: transaction
2020-09-11 02:10:58 -04:00
});
}
throw error;
}
2019-05-14 18:48:48 -04:00
var EtherscanProvider = /** @class */ (function (_super) {
__extends(EtherscanProvider, _super);
function EtherscanProvider(network, apiKey) {
var _newTarget = this.constructor;
var _this = this;
2019-08-02 02:10:58 -04:00
logger.checkNew(_newTarget, EtherscanProvider);
2019-05-14 18:48:48 -04:00
_this = _super.call(this, network) || this;
2021-05-17 16:19:36 -04:00
properties_1.defineReadOnly(_this, "baseUrl", _this.getBaseUrl());
properties_1.defineReadOnly(_this, "apiKey", apiKey || defaultApiKey);
return _this;
}
EtherscanProvider.prototype.getBaseUrl = function () {
switch (this.network ? this.network.name : "invalid") {
2019-05-14 18:48:48 -04:00
case "homestead":
2021-05-17 16:19:36 -04:00
return "https:/\/api.etherscan.io";
2019-05-14 18:48:48 -04:00
case "ropsten":
2021-05-17 16:19:36 -04:00
return "https:/\/api-ropsten.etherscan.io";
2019-05-14 18:48:48 -04:00
case "rinkeby":
2021-05-17 16:19:36 -04:00
return "https:/\/api-rinkeby.etherscan.io";
2019-05-14 18:48:48 -04:00
case "kovan":
2021-05-17 16:19:36 -04:00
return "https:/\/api-kovan.etherscan.io";
2019-05-14 18:48:48 -04:00
case "goerli":
2021-05-17 16:19:36 -04:00
return "https:/\/api-goerli.etherscan.io";
2019-05-14 18:48:48 -04:00
default:
}
2021-05-17 16:19:36 -04:00
return logger.throwArgumentError("unsupported network", "network", name);
};
EtherscanProvider.prototype.getUrl = function (module, params) {
var query = Object.keys(params).reduce(function (accum, key) {
var value = params[key];
if (value != null) {
accum += "&" + key + "=" + value;
}
return accum;
}, "");
var apiKey = ((this.apiKey) ? "&apikey=" + this.apiKey : "");
return this.baseUrl + "/api?module=" + module + query + apiKey;
};
EtherscanProvider.prototype.getPostUrl = function () {
return this.baseUrl + "/api";
};
EtherscanProvider.prototype.getPostData = function (module, params) {
params.module = module;
params.apikey = this.apiKey;
return params;
};
EtherscanProvider.prototype.fetch = function (module, params, post) {
return __awaiter(this, void 0, void 0, function () {
var url, payload, procFunc, connection, payloadStr, result;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
url = (post ? this.getPostUrl() : this.getUrl(module, params));
payload = (post ? this.getPostData(module, params) : null);
procFunc = (module === "proxy") ? getJsonResult : getResult;
this.emit("debug", {
action: "request",
request: url,
provider: this
});
connection = {
url: url,
throttleSlotInterval: 1000,
throttleCallback: function (attempt, url) {
if (_this.isCommunityResource()) {
formatter_1.showThrottleMessage();
}
return Promise.resolve(true);
}
};
payloadStr = null;
if (payload) {
connection.headers = { "content-type": "application/x-www-form-urlencoded; charset=UTF-8" };
payloadStr = Object.keys(payload).map(function (key) {
return key + "=" + payload[key];
}).join("&");
}
return [4 /*yield*/, web_1.fetchJson(connection, payloadStr, procFunc || getJsonResult)];
case 1:
result = _a.sent();
this.emit("debug", {
action: "response",
request: url,
response: properties_1.deepCopy(result),
provider: this
});
return [2 /*return*/, result];
}
});
});
};
2020-05-03 17:53:58 -04:00
EtherscanProvider.prototype.detectNetwork = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.network];
});
});
};
2019-05-14 18:48:48 -04:00
EtherscanProvider.prototype.perform = function (method, params) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2021-05-17 16:19:36 -04:00
var _a, postData, error_1, postData, error_2, args, topic0, logs, blocks, i, log, block, _b;
2019-11-20 18:57:38 +09:00
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = method;
switch (_a) {
case "getBlockNumber": return [3 /*break*/, 1];
case "getGasPrice": return [3 /*break*/, 2];
case "getBalance": return [3 /*break*/, 3];
case "getTransactionCount": return [3 /*break*/, 4];
case "getCode": return [3 /*break*/, 5];
case "getStorageAt": return [3 /*break*/, 6];
case "sendTransaction": return [3 /*break*/, 7];
case "getBlock": return [3 /*break*/, 8];
case "getTransaction": return [3 /*break*/, 9];
case "getTransactionReceipt": return [3 /*break*/, 10];
case "call": return [3 /*break*/, 11];
2020-09-11 02:10:58 -04:00
case "estimateGas": return [3 /*break*/, 15];
case "getLogs": return [3 /*break*/, 19];
case "getEtherPrice": return [3 /*break*/, 26];
2019-05-14 18:48:48 -04:00
}
2020-09-11 02:10:58 -04:00
return [3 /*break*/, 28];
2021-05-17 16:19:36 -04:00
case 1: return [2 /*return*/, this.fetch("proxy", { action: "eth_blockNumber" })];
case 2: return [2 /*return*/, this.fetch("proxy", { action: "eth_gasPrice" })];
case 3:
// Returns base-10 result
return [2 /*return*/, this.fetch("account", {
action: "balance",
address: params.address,
tag: params.blockTag
})];
case 4: return [2 /*return*/, this.fetch("proxy", {
action: "eth_getTransactionCount",
address: params.address,
tag: params.blockTag
})];
case 5: return [2 /*return*/, this.fetch("proxy", {
action: "eth_getCode",
address: params.address,
tag: params.blockTag
})];
case 6: return [2 /*return*/, this.fetch("proxy", {
action: "eth_getStorageAt",
address: params.address,
position: params.position,
tag: params.blockTag
})];
case 7: return [2 /*return*/, this.fetch("proxy", {
2020-10-22 20:01:18 -04:00
action: "eth_sendRawTransaction",
2021-05-17 16:19:36 -04:00
hex: params.signedTransaction
}, true).catch(function (error) {
2020-10-22 20:01:18 -04:00
return checkError("sendTransaction", error, params.signedTransaction);
})];
2019-11-20 18:57:38 +09:00
case 8:
if (params.blockTag) {
2021-05-17 16:19:36 -04:00
return [2 /*return*/, this.fetch("proxy", {
action: "eth_getBlockByNumber",
tag: params.blockTag,
boolean: (params.includeTransactions ? "true" : "false")
})];
2019-05-14 18:48:48 -04:00
}
2020-04-23 23:35:39 -04:00
throw new Error("getBlock by blockHash not implemented");
2021-05-17 16:19:36 -04:00
case 9: return [2 /*return*/, this.fetch("proxy", {
action: "eth_getTransactionByHash",
txhash: params.transactionHash
})];
case 10: return [2 /*return*/, this.fetch("proxy", {
action: "eth_getTransactionReceipt",
txhash: params.transactionHash
})];
2019-11-20 18:57:38 +09:00
case 11:
2020-09-11 02:10:58 -04:00
if (params.blockTag !== "latest") {
throw new Error("EtherscanProvider does not support blockTag for call");
2019-05-14 18:48:48 -04:00
}
2020-10-22 20:01:18 -04:00
postData = getTransactionPostData(params.transaction);
postData.module = "proxy";
postData.action = "eth_call";
2019-11-20 18:57:38 +09:00
_c.label = 12;
case 12:
2020-09-11 02:10:58 -04:00
_c.trys.push([12, 14, , 15]);
2021-05-17 16:19:36 -04:00
return [4 /*yield*/, this.fetch("proxy", postData, true)];
2020-09-11 02:10:58 -04:00
case 13: return [2 /*return*/, _c.sent()];
case 14:
error_1 = _c.sent();
2020-09-16 03:08:36 -04:00
return [2 /*return*/, checkError("call", error_1, params.transaction)];
2020-09-11 02:10:58 -04:00
case 15:
2020-10-22 20:01:18 -04:00
postData = getTransactionPostData(params.transaction);
postData.module = "proxy";
postData.action = "eth_estimateGas";
2020-09-11 02:10:58 -04:00
_c.label = 16;
case 16:
_c.trys.push([16, 18, , 19]);
2021-05-17 16:19:36 -04:00
return [4 /*yield*/, this.fetch("proxy", postData, true)];
2020-09-11 02:10:58 -04:00
case 17: return [2 /*return*/, _c.sent()];
case 18:
error_2 = _c.sent();
2020-09-16 03:08:36 -04:00
return [2 /*return*/, checkError("estimateGas", error_2, params.transaction)];
2020-09-11 02:10:58 -04:00
case 19:
2021-05-17 16:19:36 -04:00
args = { action: "getLogs" };
2019-11-20 18:57:38 +09:00
if (params.filter.fromBlock) {
2021-05-17 16:19:36 -04:00
args.fromBlock = checkLogTag(params.filter.fromBlock);
2019-11-20 18:57:38 +09:00
}
if (params.filter.toBlock) {
2021-05-17 16:19:36 -04:00
args.toBlock = checkLogTag(params.filter.toBlock);
2019-11-20 18:57:38 +09:00
}
if (params.filter.address) {
2021-05-17 16:19:36 -04:00
args.address = params.filter.address;
2019-11-20 18:57:38 +09:00
}
// @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_1.Logger.errors.UNSUPPORTED_OPERATION, { topics: params.filter.topics });
2019-05-14 18:48:48 -04:00
}
2019-11-20 18:57:38 +09:00
if (params.filter.topics.length === 1) {
topic0 = params.filter.topics[0];
if (typeof (topic0) !== "string" || topic0.length !== 66) {
logger.throwError("unsupported topic format", logger_1.Logger.errors.UNSUPPORTED_OPERATION, { topic0: topic0 });
}
2021-05-17 16:19:36 -04:00
args.topic0 = topic0;
2019-05-14 18:48:48 -04:00
}
2019-11-20 18:57:38 +09:00
}
2021-05-17 16:19:36 -04:00
return [4 /*yield*/, this.fetch("logs", args)];
2020-09-11 02:10:58 -04:00
case 20:
2019-11-20 18:57:38 +09:00
logs = _c.sent();
2020-10-22 21:55:40 -04:00
blocks = {};
2019-11-20 18:57:38 +09:00
i = 0;
2020-09-11 02:10:58 -04:00
_c.label = 21;
case 21:
if (!(i < logs.length)) return [3 /*break*/, 25];
2019-11-20 18:57:38 +09:00
log = logs[i];
if (log.blockHash != null) {
2020-09-11 02:10:58 -04:00
return [3 /*break*/, 24];
2019-11-20 18:57:38 +09:00
}
2020-10-22 21:55:40 -04:00
if (!(blocks[log.blockNumber] == null)) return [3 /*break*/, 23];
return [4 /*yield*/, this.getBlock(log.blockNumber)];
2020-09-11 02:10:58 -04:00
case 22:
2020-10-22 21:55:40 -04:00
block = _c.sent();
if (block) {
blocks[log.blockNumber] = block.hash;
2019-11-20 18:57:38 +09:00
}
2020-09-11 02:10:58 -04:00
_c.label = 23;
case 23:
2020-10-22 21:55:40 -04:00
log.blockHash = blocks[log.blockNumber];
2020-09-11 02:10:58 -04:00
_c.label = 24;
case 24:
2019-11-20 18:57:38 +09:00
i++;
2020-09-11 02:10:58 -04:00
return [3 /*break*/, 21];
case 25: return [2 /*return*/, logs];
case 26:
2019-11-20 18:57:38 +09:00
if (this.network.name !== "homestead") {
return [2 /*return*/, 0.0];
}
_b = parseFloat;
2021-05-17 16:19:36 -04:00
return [4 /*yield*/, this.fetch("stats", { action: "ethprice" })];
2020-09-11 02:10:58 -04:00
case 27: return [2 /*return*/, _b.apply(void 0, [(_c.sent()).ethusd])];
case 28: return [3 /*break*/, 29];
case 29: return [2 /*return*/, _super.prototype.perform.call(this, method, params)];
2019-05-14 18:48:48 -04:00
}
2019-11-20 18:57:38 +09:00
});
});
2019-05-14 18:48:48 -04:00
};
2021-05-17 16:19:36 -04:00
// Note: The `page` page parameter only allows pagination within the
// 10,000 window abailable without a page and offset parameter
// Error: Result window is too large, PageNo x Offset size must
// be less than or equal to 10000
2019-05-14 18:48:48 -04:00
EtherscanProvider.prototype.getHistory = function (addressOrName, startBlock, endBlock) {
2021-05-17 16:19:36 -04:00
return __awaiter(this, void 0, void 0, function () {
var params, result;
var _a;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = {
action: "txlist"
};
return [4 /*yield*/, this.resolveName(addressOrName)];
case 1:
params = (_a.address = (_b.sent()),
_a.startblock = ((startBlock == null) ? 0 : startBlock),
_a.endblock = ((endBlock == null) ? 99999999 : endBlock),
_a.sort = "asc",
_a);
return [4 /*yield*/, this.fetch("account", params)];
case 2:
result = _b.sent();
return [2 /*return*/, result.map(function (tx) {
["contractAddress", "to"].forEach(function (key) {
if (tx[key] == "") {
delete tx[key];
}
});
if (tx.creates == null && tx.contractAddress != null) {
tx.creates = tx.contractAddress;
}
var item = _this.formatter.transactionResponse(tx);
if (tx.timeStamp) {
item.timestamp = parseInt(tx.timeStamp);
}
return item;
})];
2020-07-14 02:33:30 -04:00
}
2019-05-14 18:48:48 -04:00
});
});
};
2020-10-07 20:10:50 -04:00
EtherscanProvider.prototype.isCommunityResource = function () {
return (this.apiKey === defaultApiKey);
};
2019-05-14 18:48:48 -04:00
return EtherscanProvider;
}(base_provider_1.BaseProvider));
exports.EtherscanProvider = EtherscanProvider;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=etherscan-provider.js.map