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

2585 lines
130 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 };
}
};
2020-11-16 23:07:24 -05:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2019-05-14 18:48:48 -04:00
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-07 18:24:04 -05:00
exports.BaseProvider = exports.Resolver = exports.Event = void 0;
2019-05-14 18:48:48 -04:00
var abstract_provider_1 = require("@ethersproject/abstract-provider");
2022-05-20 19:13:03 -04:00
var base64_1 = require("@ethersproject/base64");
2020-09-04 01:37:14 -04:00
var basex_1 = require("@ethersproject/basex");
2019-05-14 18:48:48 -04:00
var bignumber_1 = require("@ethersproject/bignumber");
var bytes_1 = require("@ethersproject/bytes");
2020-09-04 01:37:14 -04:00
var constants_1 = require("@ethersproject/constants");
2019-05-14 18:48:48 -04:00
var hash_1 = require("@ethersproject/hash");
var networks_1 = require("@ethersproject/networks");
var properties_1 = require("@ethersproject/properties");
2020-09-04 01:37:14 -04:00
var sha2_1 = require("@ethersproject/sha2");
2019-05-14 18:48:48 -04:00
var strings_1 = require("@ethersproject/strings");
var web_1 = require("@ethersproject/web");
2020-11-16 23:07:24 -05:00
var bech32_1 = __importDefault(require("bech32"));
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 formatter_1 = require("./formatter");
2022-03-09 02:56:08 -05:00
var MAX_CCIP_REDIRECTS = 10;
2019-05-14 18:48:48 -04:00
//////////////////////////////
// Event Serializeing
function checkTopic(topic) {
if (topic == null) {
return "null";
}
2021-10-16 02:29:27 -04:00
if ((0, bytes_1.hexDataLength)(topic) !== 32) {
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("invalid topic", "topic", topic);
2019-05-14 18:48:48 -04:00
}
return topic.toLowerCase();
}
function serializeTopics(topics) {
// Remove trailing null AND-topics; they are redundant
topics = topics.slice();
2020-04-16 21:59:53 -04:00
while (topics.length > 0 && topics[topics.length - 1] == null) {
2019-05-14 18:48:48 -04:00
topics.pop();
}
return topics.map(function (topic) {
if (Array.isArray(topic)) {
// Only track unique OR-topics
var unique_1 = {};
topic.forEach(function (topic) {
unique_1[checkTopic(topic)] = true;
});
// The order of OR-topics does not matter
var sorted = Object.keys(unique_1);
sorted.sort();
return sorted.join("|");
}
else {
return checkTopic(topic);
}
}).join("&");
}
function deserializeTopics(data) {
2020-04-16 21:59:53 -04:00
if (data === "") {
return [];
}
2019-05-14 18:48:48 -04:00
return data.split(/&/g).map(function (topic) {
2020-04-28 05:03:49 -04:00
if (topic === "") {
return [];
}
var comps = topic.split("|").map(function (topic) {
2019-05-14 18:48:48 -04:00
return ((topic === "null") ? null : topic);
});
2020-04-28 05:03:49 -04:00
return ((comps.length === 1) ? comps[0] : comps);
2019-05-14 18:48:48 -04:00
});
}
function getEventTag(eventName) {
if (typeof (eventName) === "string") {
eventName = eventName.toLowerCase();
2021-10-16 02:29:27 -04:00
if ((0, bytes_1.hexDataLength)(eventName) === 32) {
2019-05-14 18:48:48 -04:00
return "tx:" + eventName;
}
if (eventName.indexOf(":") === -1) {
return eventName;
}
}
else if (Array.isArray(eventName)) {
return "filter:*:" + serializeTopics(eventName);
}
2019-06-11 17:57:04 -04:00
else if (abstract_provider_1.ForkEvent.isForkEvent(eventName)) {
2019-08-02 02:10:58 -04:00
logger.warn("not implemented");
2019-05-14 18:48:48 -04:00
throw new Error("not implemented");
}
else if (eventName && typeof (eventName) === "object") {
return "filter:" + (eventName.address || "*") + ":" + serializeTopics(eventName.topics || []);
}
throw new Error("invalid event - " + eventName);
}
//////////////////////////////
// Helper Object
function getTime() {
return (new Date()).getTime();
}
2020-06-03 03:47:17 -04:00
function stall(duration) {
return new Promise(function (resolve) {
setTimeout(resolve, duration);
});
}
2019-05-14 18:48:48 -04:00
//////////////////////////////
// Provider Object
/**
* EventType
* - "block"
2020-05-04 23:01:04 -04:00
* - "poll"
2020-06-03 03:47:17 -04:00
* - "didPoll"
2019-05-14 18:48:48 -04:00
* - "pending"
* - "error"
2020-06-03 03:47:17 -04:00
* - "network"
2019-05-14 18:48:48 -04:00
* - filter
* - topics array
* - transaction hash
*/
2020-06-03 03:47:17 -04:00
var PollableEvents = ["block", "network", "pending", "poll"];
2019-05-14 18:48:48 -04:00
var Event = /** @class */ (function () {
function Event(tag, listener, once) {
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(this, "tag", tag);
(0, properties_1.defineReadOnly)(this, "listener", listener);
(0, properties_1.defineReadOnly)(this, "once", once);
2022-03-09 14:58:07 -05:00
this._lastBlockNumber = -2;
this._inflight = false;
2019-05-14 18:48:48 -04:00
}
2020-04-15 18:28:04 -04:00
Object.defineProperty(Event.prototype, "event", {
get: function () {
switch (this.type) {
case "tx":
return this.hash;
case "filter":
return this.filter;
}
return this.tag;
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2020-04-15 18:28:04 -04:00
configurable: true
});
2020-03-12 19:14:50 +01:00
Object.defineProperty(Event.prototype, "type", {
get: function () {
return this.tag.split(":")[0];
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2020-03-12 19:14:50 +01:00
configurable: true
});
Object.defineProperty(Event.prototype, "hash", {
get: function () {
var comps = this.tag.split(":");
if (comps[0] !== "tx") {
return null;
}
return comps[1];
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2020-03-12 19:14:50 +01:00
configurable: true
});
Object.defineProperty(Event.prototype, "filter", {
get: function () {
var comps = this.tag.split(":");
if (comps[0] !== "filter") {
return null;
}
2020-04-16 21:59:53 -04:00
var address = comps[1];
var topics = deserializeTopics(comps[2]);
var filter = {};
if (topics.length > 0) {
filter.topics = topics;
}
if (address && address !== "*") {
filter.address = address;
2020-03-12 19:14:50 +01:00
}
return filter;
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2020-03-12 19:14:50 +01:00
configurable: true
});
2019-05-14 18:48:48 -04:00
Event.prototype.pollable = function () {
2020-06-03 03:47:17 -04:00
return (this.tag.indexOf(":") >= 0 || PollableEvents.indexOf(this.tag) >= 0);
2019-05-14 18:48:48 -04:00
};
return Event;
}());
2020-03-12 19:14:50 +01:00
exports.Event = Event;
2020-09-04 01:37:14 -04:00
;
// https://github.com/satoshilabs/slips/blob/master/slip-0044.md
var coinInfos = {
"0": { symbol: "btc", p2pkh: 0x00, p2sh: 0x05, prefix: "bc" },
"2": { symbol: "ltc", p2pkh: 0x30, p2sh: 0x32, prefix: "ltc" },
"3": { symbol: "doge", p2pkh: 0x1e, p2sh: 0x16 },
"60": { symbol: "eth", ilk: "eth" },
"61": { symbol: "etc", ilk: "eth" },
"700": { symbol: "xdai", ilk: "eth" },
};
function bytes32ify(value) {
2021-10-16 02:29:27 -04:00
return (0, bytes_1.hexZeroPad)(bignumber_1.BigNumber.from(value).toHexString(), 32);
2020-09-04 01:37:14 -04:00
}
// Compute the Base58Check encoded data (checksum is first 4 bytes of sha256d)
function base58Encode(data) {
2021-10-16 02:29:27 -04:00
return basex_1.Base58.encode((0, bytes_1.concat)([data, (0, bytes_1.hexDataSlice)((0, sha2_1.sha256)((0, sha2_1.sha256)(data)), 0, 4)]));
2020-09-04 01:37:14 -04:00
}
2021-12-24 03:09:45 -05:00
var matcherIpfs = new RegExp("^(ipfs):/\/(.*)$", "i");
2021-10-19 00:01:37 -04:00
var matchers = [
new RegExp("^(https):/\/(.*)$", "i"),
new RegExp("^(data):(.*)$", "i"),
2021-12-24 03:09:45 -05:00
matcherIpfs,
2021-10-19 00:01:37 -04:00
new RegExp("^eip155:[0-9]+/(erc[0-9]+):(.*)$", "i"),
];
2022-03-09 02:56:08 -05:00
function _parseString(result, start) {
2021-10-19 00:01:37 -04:00
try {
2022-03-09 02:56:08 -05:00
return (0, strings_1.toUtf8String)(_parseBytes(result, start));
2021-10-19 00:01:37 -04:00
}
catch (error) { }
return null;
}
2022-03-09 02:56:08 -05:00
function _parseBytes(result, start) {
2021-10-19 00:01:37 -04:00
if (result === "0x") {
return null;
}
2022-03-09 02:56:08 -05:00
var offset = bignumber_1.BigNumber.from((0, bytes_1.hexDataSlice)(result, start, start + 32)).toNumber();
2021-10-19 00:01:37 -04:00
var length = bignumber_1.BigNumber.from((0, bytes_1.hexDataSlice)(result, offset, offset + 32)).toNumber();
return (0, bytes_1.hexDataSlice)(result, offset + 32, offset + 32 + length);
}
2021-12-24 03:09:45 -05:00
// Trim off the ipfs:// prefix and return the default gateway URL
function getIpfsLink(link) {
2022-01-24 21:33:40 -05:00
if (link.match(/^ipfs:\/\/ipfs\//i)) {
link = link.substring(12);
}
else if (link.match(/^ipfs:\/\//i)) {
link = link.substring(7);
}
else {
logger.throwArgumentError("unsupported IPFS format", "link", link);
}
return "https://gateway.ipfs.io/ipfs/" + link;
2021-12-24 03:09:45 -05:00
}
2022-03-09 02:56:08 -05:00
function numPad(value) {
var result = (0, bytes_1.arrayify)(value);
if (result.length > 32) {
throw new Error("internal; should not happen");
}
var padded = new Uint8Array(32);
padded.set(result, 32 - result.length);
return padded;
}
function bytesPad(value) {
if ((value.length % 32) === 0) {
return value;
}
var result = new Uint8Array(Math.ceil(value.length / 32) * 32);
result.set(value);
return result;
}
// ABI Encodes a series of (bytes, bytes, ...)
function encodeBytes(datas) {
var result = [];
var byteCount = 0;
// Add place-holders for pointers as we add items
for (var i = 0; i < datas.length; i++) {
result.push(null);
byteCount += 32;
}
for (var i = 0; i < datas.length; i++) {
var data = (0, bytes_1.arrayify)(datas[i]);
// Update the bytes offset
result[i] = numPad(byteCount);
// The length and padded value of data
result.push(numPad(data.length));
result.push(bytesPad(data));
byteCount += 32 + Math.ceil(data.length / 32) * 32;
}
return (0, bytes_1.hexConcat)(result);
}
2020-09-04 01:37:14 -04:00
var Resolver = /** @class */ (function () {
2021-10-19 00:01:37 -04:00
// The resolvedAddress is only for creating a ReverseLookup resolver
function Resolver(provider, address, name, resolvedAddress) {
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(this, "provider", provider);
(0, properties_1.defineReadOnly)(this, "name", name);
(0, properties_1.defineReadOnly)(this, "address", provider.formatter.address(address));
2021-10-19 00:01:37 -04:00
(0, properties_1.defineReadOnly)(this, "_resolvedAddress", resolvedAddress);
2020-09-04 01:37:14 -04:00
}
2022-03-09 02:56:08 -05:00
Resolver.prototype.supportsWildcard = function () {
var _this = this;
if (!this._supportsEip2544) {
// supportsInterface(bytes4 = selector("resolve(bytes,bytes)"))
this._supportsEip2544 = this.provider.call({
to: this.address,
data: "0x01ffc9a79061b92300000000000000000000000000000000000000000000000000000000"
}).then(function (result) {
return bignumber_1.BigNumber.from(result).eq(1);
}).catch(function (error) {
if (error.code === logger_1.Logger.errors.CALL_EXCEPTION) {
return false;
}
// Rethrow the error: link is down, etc. Let future attempts retry.
_this._supportsEip2544 = null;
throw error;
});
}
return this._supportsEip2544;
};
Resolver.prototype._fetch = function (selector, parameters) {
2020-09-04 01:37:14 -04:00
return __awaiter(this, void 0, void 0, function () {
2022-03-09 02:56:08 -05:00
var tx, parseBytes, result, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
2020-09-04 01:37:14 -04:00
case 0:
2021-10-19 00:01:37 -04:00
tx = {
2020-09-04 01:37:14 -04:00
to: this.address,
2022-03-09 14:58:07 -05:00
ccipReadEnabled: true,
2021-10-16 02:29:27 -04:00
data: (0, bytes_1.hexConcat)([selector, (0, hash_1.namehash)(this.name), (parameters || "0x")])
2020-09-04 01:37:14 -04:00
};
2022-03-09 02:56:08 -05:00
parseBytes = false;
return [4 /*yield*/, this.supportsWildcard()];
2020-09-04 01:37:14 -04:00
case 1:
2022-03-09 02:56:08 -05:00
if (_a.sent()) {
parseBytes = true;
// selector("resolve(bytes,bytes)")
tx.data = (0, bytes_1.hexConcat)(["0x9061b923", encodeBytes([(0, hash_1.dnsEncode)(this.name), tx.data])]);
}
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
2021-10-19 00:01:37 -04:00
return [4 /*yield*/, this.provider.call(tx)];
2021-06-24 02:13:06 -04:00
case 3:
2022-03-09 02:56:08 -05:00
result = _a.sent();
2022-03-09 14:58:07 -05:00
if (((0, bytes_1.arrayify)(result).length % 32) === 4) {
logger.throwError("resolver threw error", logger_1.Logger.errors.CALL_EXCEPTION, {
transaction: tx, data: result
});
}
2022-03-09 02:56:08 -05:00
if (parseBytes) {
result = _parseBytes(result, 0);
}
return [2 /*return*/, result];
case 4:
error_1 = _a.sent();
2021-06-24 02:13:06 -04:00
if (error_1.code === logger_1.Logger.errors.CALL_EXCEPTION) {
return [2 /*return*/, null];
}
2022-03-09 14:58:07 -05:00
throw error_1;
2022-03-09 02:56:08 -05:00
case 5: return [2 /*return*/];
}
});
});
};
Resolver.prototype._fetchBytes = function (selector, parameters) {
return __awaiter(this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._fetch(selector, parameters)];
case 1:
result = _a.sent();
if (result != null) {
return [2 /*return*/, _parseBytes(result, 0)];
}
return [2 /*return*/, null];
2020-09-04 01:37:14 -04:00
}
});
});
};
Resolver.prototype._getAddress = function (coinType, hexBytes) {
var coinInfo = coinInfos[String(coinType)];
if (coinInfo == null) {
logger.throwError("unsupported coin type: " + coinType, logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "getAddress(" + coinType + ")"
});
}
if (coinInfo.ilk === "eth") {
return this.provider.formatter.address(hexBytes);
}
2021-10-16 02:29:27 -04:00
var bytes = (0, bytes_1.arrayify)(hexBytes);
2020-09-04 01:37:14 -04:00
// P2PKH: OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
if (coinInfo.p2pkh != null) {
var p2pkh = hexBytes.match(/^0x76a9([0-9a-f][0-9a-f])([0-9a-f]*)88ac$/);
if (p2pkh) {
2021-10-19 00:01:37 -04:00
var length_1 = parseInt(p2pkh[1], 16);
if (p2pkh[2].length === length_1 * 2 && length_1 >= 1 && length_1 <= 75) {
2021-10-16 02:29:27 -04:00
return base58Encode((0, bytes_1.concat)([[coinInfo.p2pkh], ("0x" + p2pkh[2])]));
2020-09-04 01:37:14 -04:00
}
}
}
// P2SH: OP_HASH160 <scriptHash> OP_EQUAL
if (coinInfo.p2sh != null) {
var p2sh = hexBytes.match(/^0xa9([0-9a-f][0-9a-f])([0-9a-f]*)87$/);
if (p2sh) {
2021-10-19 00:01:37 -04:00
var length_2 = parseInt(p2sh[1], 16);
if (p2sh[2].length === length_2 * 2 && length_2 >= 1 && length_2 <= 75) {
2021-10-16 02:29:27 -04:00
return base58Encode((0, bytes_1.concat)([[coinInfo.p2sh], ("0x" + p2sh[2])]));
2020-09-04 01:37:14 -04:00
}
}
}
// Bech32
if (coinInfo.prefix != null) {
2021-10-19 00:01:37 -04:00
var length_3 = bytes[1];
2020-09-04 01:37:14 -04:00
// https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program
var version_1 = bytes[0];
if (version_1 === 0x00) {
2021-10-19 00:01:37 -04:00
if (length_3 !== 20 && length_3 !== 32) {
2020-09-04 01:37:14 -04:00
version_1 = -1;
}
}
else {
version_1 = -1;
}
2021-10-19 00:01:37 -04:00
if (version_1 >= 0 && bytes.length === 2 + length_3 && length_3 >= 1 && length_3 <= 75) {
2020-11-16 23:07:24 -05:00
var words = bech32_1.default.toWords(bytes.slice(2));
2020-09-04 01:37:14 -04:00
words.unshift(version_1);
2020-11-16 23:07:24 -05:00
return bech32_1.default.encode(coinInfo.prefix, words);
2020-09-04 01:37:14 -04:00
}
}
return null;
};
Resolver.prototype.getAddress = function (coinType) {
return __awaiter(this, void 0, void 0, function () {
2022-03-09 02:56:08 -05:00
var result, error_2, hexBytes, address;
2020-09-04 01:37:14 -04:00
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (coinType == null) {
coinType = 60;
}
2021-06-24 02:13:06 -04:00
if (!(coinType === 60)) return [3 /*break*/, 4];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
2022-03-09 02:56:08 -05:00
return [4 /*yield*/, this._fetch("0x3b3b57de")];
2021-06-24 02:13:06 -04:00
case 2:
2022-03-09 02:56:08 -05:00
result = _a.sent();
2020-09-04 01:37:14 -04:00
// No address
2022-03-09 02:56:08 -05:00
if (result === "0x" || result === constants_1.HashZero) {
2020-09-04 01:37:14 -04:00
return [2 /*return*/, null];
}
2022-03-09 02:56:08 -05:00
return [2 /*return*/, this.provider.formatter.callAddress(result)];
2020-09-04 01:37:14 -04:00
case 3:
2021-06-24 02:13:06 -04:00
error_2 = _a.sent();
if (error_2.code === logger_1.Logger.errors.CALL_EXCEPTION) {
return [2 /*return*/, null];
}
throw error_2;
case 4: return [4 /*yield*/, this._fetchBytes("0xf1cb7e06", bytes32ify(coinType))];
case 5:
2020-09-04 01:37:14 -04:00
hexBytes = _a.sent();
// No address
if (hexBytes == null || hexBytes === "0x") {
return [2 /*return*/, null];
}
address = this._getAddress(coinType, hexBytes);
if (address == null) {
logger.throwError("invalid or unsupported coin data", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "getAddress(" + coinType + ")",
coinType: coinType,
data: hexBytes
});
}
return [2 /*return*/, address];
}
});
});
};
2021-10-19 00:01:37 -04:00
Resolver.prototype.getAvatar = function () {
return __awaiter(this, void 0, void 0, function () {
2022-01-06 03:53:52 -05:00
var linkage, avatar, i, match, scheme, _a, selector, owner, _b, comps, addr, tokenId, tokenOwner, _c, _d, balance, _e, _f, tx, metadataUrl, _g, metadata, imageUrl, ipfs, error_3;
2021-10-19 00:01:37 -04:00
return __generator(this, function (_h) {
switch (_h.label) {
case 0:
2021-12-24 03:09:45 -05:00
linkage = [{ type: "name", content: this.name }];
2021-10-19 00:01:37 -04:00
_h.label = 1;
case 1:
_h.trys.push([1, 19, , 20]);
return [4 /*yield*/, this.getText("avatar")];
case 2:
avatar = _h.sent();
if (avatar == null) {
return [2 /*return*/, null];
}
i = 0;
_h.label = 3;
case 3:
if (!(i < matchers.length)) return [3 /*break*/, 18];
match = avatar.match(matchers[i]);
if (match == null) {
return [3 /*break*/, 17];
}
2022-01-06 03:53:52 -05:00
scheme = match[1].toLowerCase();
_a = scheme;
2021-10-19 00:01:37 -04:00
switch (_a) {
case "https": return [3 /*break*/, 4];
case "data": return [3 /*break*/, 5];
case "ipfs": return [3 /*break*/, 6];
case "erc721": return [3 /*break*/, 7];
case "erc1155": return [3 /*break*/, 7];
}
return [3 /*break*/, 17];
case 4:
linkage.push({ type: "url", content: avatar });
return [2 /*return*/, { linkage: linkage, url: avatar }];
case 5:
linkage.push({ type: "data", content: avatar });
return [2 /*return*/, { linkage: linkage, url: avatar }];
case 6:
linkage.push({ type: "ipfs", content: avatar });
2021-12-24 03:09:45 -05:00
return [2 /*return*/, { linkage: linkage, url: getIpfsLink(avatar) }];
2021-10-19 00:01:37 -04:00
case 7:
2022-01-06 03:53:52 -05:00
selector = (scheme === "erc721") ? "0xc87b56dd" : "0x0e89341c";
linkage.push({ type: scheme, content: avatar });
2021-10-19 00:01:37 -04:00
_b = this._resolvedAddress;
if (_b) return [3 /*break*/, 9];
return [4 /*yield*/, this.getAddress()];
case 8:
_b = (_h.sent());
_h.label = 9;
case 9:
owner = (_b);
comps = (match[2] || "").split("/");
if (comps.length !== 2) {
return [2 /*return*/, null];
}
return [4 /*yield*/, this.provider.formatter.address(comps[0])];
case 10:
addr = _h.sent();
tokenId = (0, bytes_1.hexZeroPad)(bignumber_1.BigNumber.from(comps[1]).toHexString(), 32);
2022-01-06 03:53:52 -05:00
if (!(scheme === "erc721")) return [3 /*break*/, 12];
2021-10-19 00:01:37 -04:00
_d = (_c = this.provider.formatter).callAddress;
return [4 /*yield*/, this.provider.call({
to: addr, data: (0, bytes_1.hexConcat)(["0x6352211e", tokenId])
})];
case 11:
tokenOwner = _d.apply(_c, [_h.sent()]);
if (owner !== tokenOwner) {
return [2 /*return*/, null];
}
linkage.push({ type: "owner", content: tokenOwner });
return [3 /*break*/, 14];
case 12:
2022-01-06 03:53:52 -05:00
if (!(scheme === "erc1155")) return [3 /*break*/, 14];
2021-10-19 00:01:37 -04:00
_f = (_e = bignumber_1.BigNumber).from;
return [4 /*yield*/, this.provider.call({
to: addr, data: (0, bytes_1.hexConcat)(["0x00fdd58e", (0, bytes_1.hexZeroPad)(owner, 32), tokenId])
})];
case 13:
balance = _f.apply(_e, [_h.sent()]);
if (balance.isZero()) {
return [2 /*return*/, null];
}
linkage.push({ type: "balance", content: balance.toString() });
_h.label = 14;
case 14:
tx = {
to: this.provider.formatter.address(comps[0]),
data: (0, bytes_1.hexConcat)([selector, tokenId])
};
_g = _parseString;
return [4 /*yield*/, this.provider.call(tx)];
case 15:
2022-03-09 02:56:08 -05:00
metadataUrl = _g.apply(void 0, [_h.sent(), 0]);
2021-10-19 00:01:37 -04:00
if (metadataUrl == null) {
return [2 /*return*/, null];
}
2022-01-24 21:33:40 -05:00
linkage.push({ type: "metadata-url-base", content: metadataUrl });
2021-10-19 00:01:37 -04:00
// ERC-1155 allows a generic {id} in the URL
2022-01-06 03:53:52 -05:00
if (scheme === "erc1155") {
2021-10-19 00:01:37 -04:00
metadataUrl = metadataUrl.replace("{id}", tokenId.substring(2));
2021-12-24 03:09:45 -05:00
linkage.push({ type: "metadata-url-expanded", content: metadataUrl });
2021-10-19 00:01:37 -04:00
}
2022-01-24 21:33:40 -05:00
// Transform IPFS metadata links
if (metadataUrl.match(/^ipfs:/i)) {
metadataUrl = getIpfsLink(metadataUrl);
}
linkage.push({ type: "metadata-url", content: metadataUrl });
2021-10-19 00:01:37 -04:00
return [4 /*yield*/, (0, web_1.fetchJson)(metadataUrl)];
case 16:
metadata = _h.sent();
2021-12-24 03:09:45 -05:00
if (!metadata) {
2021-10-19 00:01:37 -04:00
return [2 /*return*/, null];
}
linkage.push({ type: "metadata", content: JSON.stringify(metadata) });
2021-12-24 03:09:45 -05:00
imageUrl = metadata.image;
if (typeof (imageUrl) !== "string") {
return [2 /*return*/, null];
}
if (imageUrl.match(/^(https:\/\/|data:)/i)) {
// Allow
}
else {
ipfs = imageUrl.match(matcherIpfs);
if (ipfs == null) {
return [2 /*return*/, null];
}
linkage.push({ type: "url-ipfs", content: imageUrl });
imageUrl = getIpfsLink(imageUrl);
}
linkage.push({ type: "url", content: imageUrl });
return [2 /*return*/, { linkage: linkage, url: imageUrl }];
2021-10-19 00:01:37 -04:00
case 17:
i++;
return [3 /*break*/, 3];
case 18: return [3 /*break*/, 20];
case 19:
error_3 = _h.sent();
return [3 /*break*/, 20];
case 20: return [2 /*return*/, null];
}
});
});
};
2020-09-04 01:37:14 -04:00
Resolver.prototype.getContentHash = function () {
return __awaiter(this, void 0, void 0, function () {
2022-05-20 19:13:03 -04:00
var hexBytes, ipfs, length_4, ipns, length_5, swarm, skynet, urlSafe_1, hash;
2020-09-04 01:37:14 -04:00
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._fetchBytes("0xbc1c58d1")];
case 1:
hexBytes = _a.sent();
// No contenthash
if (hexBytes == null || hexBytes === "0x") {
return [2 /*return*/, null];
}
ipfs = hexBytes.match(/^0xe3010170(([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f]*))$/);
if (ipfs) {
2021-10-19 00:01:37 -04:00
length_4 = parseInt(ipfs[3], 16);
if (ipfs[4].length === length_4 * 2) {
2020-09-04 01:37:14 -04:00
return [2 /*return*/, "ipfs:/\/" + basex_1.Base58.encode("0x" + ipfs[1])];
}
}
2022-03-25 04:32:41 -04:00
ipns = hexBytes.match(/^0xe5010172(([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f]*))$/);
if (ipns) {
length_5 = parseInt(ipns[3], 16);
if (ipns[4].length === length_5 * 2) {
return [2 /*return*/, "ipns:/\/" + basex_1.Base58.encode("0x" + ipns[1])];
}
}
2020-09-04 01:37:14 -04:00
swarm = hexBytes.match(/^0xe40101fa011b20([0-9a-f]*)$/);
if (swarm) {
if (swarm[1].length === (32 * 2)) {
return [2 /*return*/, "bzz:/\/" + swarm[1]];
}
}
2022-05-20 19:13:03 -04:00
skynet = hexBytes.match(/^0x90b2c605([0-9a-f]*)$/);
if (skynet) {
if (skynet[1].length === (34 * 2)) {
urlSafe_1 = { "=": "", "+": "-", "/": "_" };
hash = (0, base64_1.encode)("0x" + skynet[1]).replace(/[=+\/]/g, function (a) { return (urlSafe_1[a]); });
return [2 /*return*/, "sia:/\/" + hash];
}
}
2020-09-04 01:37:14 -04:00
return [2 /*return*/, logger.throwError("invalid or unsupported content hash data", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "getContentHash()",
data: hexBytes
})];
}
});
});
};
Resolver.prototype.getText = function (key) {
return __awaiter(this, void 0, void 0, function () {
var keyBytes, hexBytes;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
2021-10-16 02:29:27 -04:00
keyBytes = (0, strings_1.toUtf8Bytes)(key);
2020-09-04 01:37:14 -04:00
// The nodehash consumes the first slot, so the string pointer targets
// offset 64, with the length at offset 64 and data starting at offset 96
2021-10-16 02:29:27 -04:00
keyBytes = (0, bytes_1.concat)([bytes32ify(64), bytes32ify(keyBytes.length), keyBytes]);
2020-09-04 01:37:14 -04:00
// Pad to word-size (32 bytes)
if ((keyBytes.length % 32) !== 0) {
2021-10-16 02:29:27 -04:00
keyBytes = (0, bytes_1.concat)([keyBytes, (0, bytes_1.hexZeroPad)("0x", 32 - (key.length % 32))]);
2020-09-04 01:37:14 -04:00
}
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, this._fetchBytes("0x59d1d43c", (0, bytes_1.hexlify)(keyBytes))];
2020-09-04 01:37:14 -04:00
case 1:
hexBytes = _a.sent();
if (hexBytes == null || hexBytes === "0x") {
return [2 /*return*/, null];
}
2021-10-16 02:29:27 -04:00
return [2 /*return*/, (0, strings_1.toUtf8String)(hexBytes)];
2020-09-04 01:37:14 -04:00
}
});
});
};
return Resolver;
}());
exports.Resolver = Resolver;
2019-05-14 18:48:48 -04:00
var defaultFormatter = null;
var nextPollId = 1;
var BaseProvider = /** @class */ (function (_super) {
__extends(BaseProvider, _super);
2020-05-03 17:53:58 -04:00
/**
* ready
*
* A Promise<Network> that resolves only once the provider is ready.
*
* Sub-classes that call the super with a network without a chainId
* MUST set this. Standard named networks have a known chainId.
*
*/
2019-05-14 18:48:48 -04:00
function BaseProvider(network) {
var _newTarget = this.constructor;
2022-05-12 17:30:28 -04:00
var _this = _super.call(this) || this;
2020-06-03 03:47:17 -04:00
// Events being listened to
_this._events = [];
_this._emitted = { block: -2 };
2022-03-09 02:56:08 -05:00
_this.disableCcipRead = false;
2019-05-14 18:48:48 -04:00
_this.formatter = _newTarget.getFormatter();
2020-06-03 03:47:17 -04:00
// If network is any, this Provider allows the underlying
// network to change dynamically, and we auto-detect the
// current network
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(_this, "anyNetwork", (network === "any"));
2020-06-03 03:47:17 -04:00
if (_this.anyNetwork) {
network = _this.detectNetwork();
}
2019-05-14 18:48:48 -04:00
if (network instanceof Promise) {
2020-05-03 17:53:58 -04:00
_this._networkPromise = network;
2019-05-14 18:48:48 -04:00
// Squash any "unhandled promise" errors; that do not need to be handled
2020-05-03 17:53:58 -04:00
network.catch(function (error) { });
2020-06-03 03:47:17 -04:00
// Trigger initial network setting (async)
2020-06-13 21:39:36 -04:00
_this._ready().catch(function (error) { });
2019-05-14 18:48:48 -04:00
}
else {
2021-10-16 02:29:27 -04:00
var knownNetwork = (0, properties_1.getStatic)(_newTarget, "getNetwork")(network);
2019-05-14 18:48:48 -04:00
if (knownNetwork) {
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(_this, "_network", knownNetwork);
2020-06-03 03:47:17 -04:00
_this.emit("network", knownNetwork, null);
2019-05-14 18:48:48 -04:00
}
else {
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("invalid network", "network", network);
2019-05-14 18:48:48 -04:00
}
}
2019-11-20 18:57:38 +09:00
_this._maxInternalBlockNumber = -1024;
2019-05-14 18:48:48 -04:00
_this._lastBlockNumber = -2;
2022-03-09 02:56:08 -05:00
_this._maxFilterBlockRange = 10;
2019-05-14 18:48:48 -04:00
_this._pollingInterval = 4000;
_this._fastQueryDate = 0;
return _this;
}
2020-05-03 17:53:58 -04:00
BaseProvider.prototype._ready = function () {
return __awaiter(this, void 0, void 0, function () {
2021-10-19 00:01:37 -04:00
var network, error_4;
2020-05-03 17:53:58 -04:00
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this._network == null)) return [3 /*break*/, 7];
network = null;
if (!this._networkPromise) return [3 /*break*/, 4];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this._networkPromise];
case 2:
network = _a.sent();
return [3 /*break*/, 4];
case 3:
2021-10-19 00:01:37 -04:00
error_4 = _a.sent();
2020-05-03 17:53:58 -04:00
return [3 /*break*/, 4];
case 4:
if (!(network == null)) return [3 /*break*/, 6];
return [4 /*yield*/, this.detectNetwork()];
case 5:
network = _a.sent();
_a.label = 6;
case 6:
// This should never happen; every Provider sub-class should have
2020-05-29 21:27:59 -04:00
// suggested a network by here (or have thrown).
2020-05-03 17:53:58 -04:00
if (!network) {
logger.throwError("no network detected", logger_1.Logger.errors.UNKNOWN_ERROR, {});
}
2020-05-29 21:27:59 -04:00
// Possible this call stacked so do not call defineReadOnly again
if (this._network == null) {
2020-06-03 03:47:17 -04:00
if (this.anyNetwork) {
this._network = network;
}
else {
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(this, "_network", network);
2020-06-03 03:47:17 -04:00
}
this.emit("network", network, null);
2020-05-29 21:27:59 -04:00
}
2020-05-03 17:53:58 -04:00
_a.label = 7;
case 7: return [2 /*return*/, this._network];
}
});
});
};
Object.defineProperty(BaseProvider.prototype, "ready", {
2020-06-03 03:47:17 -04:00
// This will always return the most recently established network.
// For "any", this can change (a "network" event is emitted before
2021-10-16 02:29:27 -04:00
// any change is reflected); otherwise this cannot change
2020-05-03 17:53:58 -04:00
get: function () {
2020-06-13 21:39:36 -04:00
var _this = this;
2021-10-16 02:29:27 -04:00
return (0, web_1.poll)(function () {
2020-06-13 21:39:36 -04:00
return _this._ready().then(function (network) {
return network;
}, function (error) {
// If the network isn't running yet, we will wait
if (error.code === logger_1.Logger.errors.NETWORK_ERROR && error.event === "noNetwork") {
return undefined;
}
throw error;
});
});
2020-05-03 17:53:58 -04:00
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2020-05-03 17:53:58 -04:00
configurable: true
});
2020-06-03 03:47:17 -04:00
// @TODO: Remove this and just create a singleton formatter
2019-05-14 18:48:48 -04:00
BaseProvider.getFormatter = function () {
if (defaultFormatter == null) {
defaultFormatter = new formatter_1.Formatter();
}
return defaultFormatter;
};
2020-06-03 03:47:17 -04:00
// @TODO: Remove this and just use getNetwork
2019-07-02 16:13:03 -04:00
BaseProvider.getNetwork = function (network) {
2021-10-16 02:29:27 -04:00
return (0, networks_1.getNetwork)((network == null) ? "homestead" : network);
2019-07-02 16:13:03 -04:00
};
2022-03-09 02:56:08 -05:00
BaseProvider.prototype.ccipReadFetch = function (tx, calldata, urls) {
return __awaiter(this, void 0, void 0, function () {
var sender, data, errorMessages, i, url, href, json, result, errorMessage;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.disableCcipRead || urls.length === 0) {
return [2 /*return*/, null];
}
2022-03-15 22:04:05 -04:00
sender = tx.to.toLowerCase();
2022-03-09 02:56:08 -05:00
data = calldata.toLowerCase();
errorMessages = [];
i = 0;
_a.label = 1;
case 1:
if (!(i < urls.length)) return [3 /*break*/, 4];
url = urls[i];
href = url.replace("{sender}", sender).replace("{data}", data);
json = (url.indexOf("{data}") >= 0) ? null : JSON.stringify({ data: data, sender: sender });
return [4 /*yield*/, (0, web_1.fetchJson)({ url: href, errorPassThrough: true }, json, function (value, response) {
value.status = response.statusCode;
return value;
})];
case 2:
result = _a.sent();
if (result.data) {
return [2 /*return*/, result.data];
}
errorMessage = (result.message || "unknown error");
// 4xx indicates the result is not present; stop
if (result.status >= 400 && result.status < 500) {
return [2 /*return*/, logger.throwError("response not found during CCIP fetch: " + errorMessage, logger_1.Logger.errors.SERVER_ERROR, { url: url, errorMessage: errorMessage })];
}
// 5xx indicates server issue; try the next url
errorMessages.push(errorMessage);
_a.label = 3;
case 3:
i++;
return [3 /*break*/, 1];
case 4: return [2 /*return*/, logger.throwError("error encountered during CCIP fetch: " + errorMessages.map(function (m) { return JSON.stringify(m); }).join(", "), logger_1.Logger.errors.SERVER_ERROR, {
urls: urls,
errorMessages: errorMessages
})];
}
});
});
};
2020-06-03 03:47:17 -04:00
// Fetches the blockNumber, but will reuse any result that is less
// than maxAge old or has been requested since the last request
2019-11-20 18:57:38 +09:00
BaseProvider.prototype._getInternalBlockNumber = function (maxAge) {
return __awaiter(this, void 0, void 0, function () {
2021-10-19 00:01:37 -04:00
var internalBlockNumber, result, error_5, reqTime, checkInternalBlockNumber;
2019-11-20 18:57:38 +09:00
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-13 21:39:36 -04:00
case 0: return [4 /*yield*/, this._ready()];
2019-11-20 18:57:38 +09:00
case 1:
_a.sent();
2021-01-13 14:49:25 -05:00
if (!(maxAge > 0)) return [3 /*break*/, 7];
2021-01-13 03:41:29 -05:00
_a.label = 2;
2019-11-20 18:57:38 +09:00
case 2:
2021-01-13 14:49:25 -05:00
if (!this._internalBlockNumber) return [3 /*break*/, 7];
internalBlockNumber = this._internalBlockNumber;
_a.label = 3;
2021-01-13 03:41:29 -05:00
case 3:
2021-01-13 14:49:25 -05:00
_a.trys.push([3, 5, , 6]);
return [4 /*yield*/, internalBlockNumber];
case 4:
2019-11-20 18:57:38 +09:00
result = _a.sent();
if ((getTime() - result.respTime) <= maxAge) {
return [2 /*return*/, result.blockNumber];
2019-05-14 18:48:48 -04:00
}
2021-01-13 14:49:25 -05:00
// Too old; fetch a new value
return [3 /*break*/, 7];
case 5:
2021-10-19 00:01:37 -04:00
error_5 = _a.sent();
2021-01-13 14:49:25 -05:00
// The fetch rejected; if we are the first to get the
// rejection, drop through so we replace it with a new
// fetch; all others blocked will then get that fetch
// which won't match the one they "remembered" and loop
2021-01-13 03:41:29 -05:00
if (this._internalBlockNumber === internalBlockNumber) {
2021-01-13 14:49:25 -05:00
return [3 /*break*/, 7];
2021-01-13 03:41:29 -05:00
}
2021-01-13 14:49:25 -05:00
return [3 /*break*/, 6];
case 6: return [3 /*break*/, 2];
case 7:
2019-11-20 18:57:38 +09:00
reqTime = getTime();
2021-10-16 02:29:27 -04:00
checkInternalBlockNumber = (0, properties_1.resolveProperties)({
2020-06-03 03:47:17 -04:00
blockNumber: this.perform("getBlockNumber", {}),
networkError: this.getNetwork().then(function (network) { return (null); }, function (error) { return (error); })
}).then(function (_a) {
var blockNumber = _a.blockNumber, networkError = _a.networkError;
if (networkError) {
// Unremember this bad internal block number
if (_this._internalBlockNumber === checkInternalBlockNumber) {
_this._internalBlockNumber = null;
}
throw networkError;
}
2019-11-20 18:57:38 +09:00
var respTime = getTime();
blockNumber = bignumber_1.BigNumber.from(blockNumber).toNumber();
if (blockNumber < _this._maxInternalBlockNumber) {
blockNumber = _this._maxInternalBlockNumber;
2019-05-14 18:48:48 -04:00
}
2019-11-20 18:57:38 +09:00
_this._maxInternalBlockNumber = blockNumber;
_this._setFastBlockNumber(blockNumber); // @TODO: Still need this?
return { blockNumber: blockNumber, reqTime: reqTime, respTime: respTime };
});
2020-06-03 03:47:17 -04:00
this._internalBlockNumber = checkInternalBlockNumber;
2021-01-13 03:41:29 -05:00
// Swallow unhandled exceptions; if needed they are handled else where
checkInternalBlockNumber.catch(function (error) {
// Don't null the dead (rejected) fetch, if it has already been updated
if (_this._internalBlockNumber === checkInternalBlockNumber) {
_this._internalBlockNumber = null;
}
});
2020-06-03 03:47:17 -04:00
return [4 /*yield*/, checkInternalBlockNumber];
2021-01-13 14:49:25 -05:00
case 8: return [2 /*return*/, (_a.sent()).blockNumber];
2019-11-20 18:57:38 +09:00
}
});
});
};
BaseProvider.prototype.poll = function () {
return __awaiter(this, void 0, void 0, function () {
2021-10-19 00:01:37 -04:00
var pollId, runners, blockNumber, error_6, i;
2019-11-20 18:57:38 +09:00
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
pollId = nextPollId++;
runners = [];
2021-01-13 03:41:29 -05:00
blockNumber = null;
_a.label = 1;
2019-11-20 18:57:38 +09:00
case 1:
2021-01-13 03:41:29 -05:00
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this._getInternalBlockNumber(100 + this.pollingInterval / 2)];
case 2:
2019-11-20 18:57:38 +09:00
blockNumber = _a.sent();
2021-01-13 03:41:29 -05:00
return [3 /*break*/, 4];
case 3:
2021-10-19 00:01:37 -04:00
error_6 = _a.sent();
this.emit("error", error_6);
2021-01-13 03:41:29 -05:00
return [2 /*return*/];
case 4:
2019-11-20 18:57:38 +09:00
this._setFastBlockNumber(blockNumber);
2020-05-04 23:01:04 -04:00
// Emit a poll event after we have the latest (fast) block number
this.emit("poll", pollId, blockNumber);
2019-11-20 18:57:38 +09:00
// If the block has not changed, meh.
if (blockNumber === this._lastBlockNumber) {
2020-05-04 23:01:04 -04:00
this.emit("didPoll", pollId);
2019-11-20 18:57:38 +09:00
return [2 /*return*/];
}
// First polling cycle, trigger a "block" events
if (this._emitted.block === -2) {
this._emitted.block = blockNumber - 1;
}
2020-06-03 03:47:17 -04:00
if (Math.abs((this._emitted.block) - blockNumber) > 1000) {
2021-05-18 16:02:27 -04:00
logger.warn("network block skew detected; skipping block events (emitted=" + this._emitted.block + " blockNumber" + blockNumber + ")");
2020-06-03 03:47:17 -04:00
this.emit("error", logger.makeError("network block skew detected", logger_1.Logger.errors.NETWORK_ERROR, {
blockNumber: blockNumber,
2020-06-13 21:39:36 -04:00
event: "blockSkew",
2020-06-03 03:47:17 -04:00
previousBlockNumber: this._emitted.block
}));
this.emit("block", blockNumber);
}
else {
// Notify all listener for each block that has passed
for (i = this._emitted.block + 1; i <= blockNumber; i++) {
this.emit("block", i);
}
2019-11-20 18:57:38 +09:00
}
// The emitted block was updated, check for obsolete events
if (this._emitted.block !== blockNumber) {
this._emitted.block = blockNumber;
Object.keys(this._emitted).forEach(function (key) {
// The block event does not expire
if (key === "block") {
return;
}
// The block we were at when we emitted this event
var eventBlockNumber = _this._emitted[key];
// We cannot garbage collect pending transactions or blocks here
// They should be garbage collected by the Provider when setting
// "pending" events
if (eventBlockNumber === "pending") {
return;
}
// Evict any transaction hashes or block hashes over 12 blocks
// old, since they should not return null anyways
if (blockNumber - eventBlockNumber > 12) {
delete _this._emitted[key];
}
2019-05-14 18:48:48 -04:00
});
2019-11-20 18:57:38 +09:00
}
// First polling cycle
if (this._lastBlockNumber === -2) {
this._lastBlockNumber = blockNumber - 1;
}
// Find all transaction hashes we are waiting on
this._events.forEach(function (event) {
2020-03-12 19:14:50 +01:00
switch (event.type) {
2019-11-20 18:57:38 +09:00
case "tx": {
2020-03-12 19:14:50 +01:00
var hash_2 = event.hash;
2019-11-20 18:57:38 +09:00
var runner = _this.getTransactionReceipt(hash_2).then(function (receipt) {
if (!receipt || receipt.blockNumber == null) {
return null;
}
_this._emitted["t:" + hash_2] = receipt.blockNumber;
_this.emit(hash_2, receipt);
return null;
}).catch(function (error) { _this.emit("error", error); });
runners.push(runner);
break;
}
case "filter": {
2022-03-09 02:56:08 -05:00
// We only allow a single getLogs to be in-flight at a time
2022-03-09 14:58:07 -05:00
if (!event._inflight) {
event._inflight = true;
2022-08-18 14:48:39 -04:00
// This is the first filter for this event, so we want to
// restrict events to events that happened no earlier than now
if (event._lastBlockNumber === -2) {
event._lastBlockNumber = blockNumber - 1;
}
// Filter from the last *known* event; due to load-balancing
2022-03-09 02:56:08 -05:00
// and some nodes returning updated block numbers before
// indexing events, a logs result with 0 entries cannot be
// trusted and we must retry a range which includes it again
var filter_1 = event.filter;
2022-03-09 14:58:07 -05:00
filter_1.fromBlock = event._lastBlockNumber + 1;
2022-03-09 02:56:08 -05:00
filter_1.toBlock = blockNumber;
2022-08-18 14:48:39 -04:00
// Prevent fitler ranges from growing too wild, since it is quite
// likely there just haven't been any events to move the lastBlockNumber.
var minFromBlock = filter_1.toBlock - _this._maxFilterBlockRange;
if (minFromBlock > filter_1.fromBlock) {
filter_1.fromBlock = minFromBlock;
}
if (filter_1.fromBlock < 0) {
filter_1.fromBlock = 0;
2019-11-20 18:57:38 +09:00
}
2022-03-09 02:56:08 -05:00
var runner = _this.getLogs(filter_1).then(function (logs) {
// Allow the next getLogs
2022-03-09 14:58:07 -05:00
event._inflight = false;
2022-03-09 02:56:08 -05:00
if (logs.length === 0) {
return;
}
logs.forEach(function (log) {
// Only when we get an event for a given block number
// can we trust the events are indexed
2022-03-09 14:58:07 -05:00
if (log.blockNumber > event._lastBlockNumber) {
event._lastBlockNumber = log.blockNumber;
2022-03-09 02:56:08 -05:00
}
// Make sure we stall requests to fetch blocks and txs
_this._emitted["b:" + log.blockHash] = log.blockNumber;
_this._emitted["t:" + log.transactionHash] = log.blockNumber;
_this.emit(filter_1, log);
});
}).catch(function (error) {
_this.emit("error", error);
// Allow another getLogs (the range was not updated)
2022-03-09 14:58:07 -05:00
event._inflight = false;
2019-11-20 18:57:38 +09:00
});
2022-03-09 02:56:08 -05:00
runners.push(runner);
}
2019-11-20 18:57:38 +09:00
break;
}
}
});
this._lastBlockNumber = blockNumber;
2020-06-03 03:47:17 -04:00
// Once all events for this loop have been processed, emit "didPoll"
2019-11-20 18:57:38 +09:00
Promise.all(runners).then(function () {
_this.emit("didPoll", pollId);
2021-01-13 03:41:29 -05:00
}).catch(function (error) { _this.emit("error", error); });
return [2 /*return*/];
2019-05-14 18:48:48 -04:00
}
});
});
};
2020-06-03 03:47:17 -04:00
// Deprecated; do not use this
2019-05-14 18:48:48 -04:00
BaseProvider.prototype.resetEventsBlock = function (blockNumber) {
this._lastBlockNumber = blockNumber - 1;
if (this.polling) {
this.poll();
}
};
Object.defineProperty(BaseProvider.prototype, "network", {
get: function () {
return this._network;
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2019-05-14 18:48:48 -04:00
configurable: true
});
2020-06-03 03:47:17 -04:00
// This method should query the network if the underlying network
// can change, such as when connected to a JSON-RPC backend
BaseProvider.prototype.detectNetwork = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, logger.throwError("provider does not support network detection", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "provider.detectNetwork"
})];
});
});
};
2019-05-14 18:48:48 -04:00
BaseProvider.prototype.getNetwork = function () {
2020-06-03 03:47:17 -04:00
return __awaiter(this, void 0, void 0, function () {
var network, currentNetwork, error;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-13 21:39:36 -04:00
case 0: return [4 /*yield*/, this._ready()];
2020-06-03 03:47:17 -04:00
case 1:
network = _a.sent();
return [4 /*yield*/, this.detectNetwork()];
case 2:
currentNetwork = _a.sent();
if (!(network.chainId !== currentNetwork.chainId)) return [3 /*break*/, 5];
if (!this.anyNetwork) return [3 /*break*/, 4];
this._network = currentNetwork;
// Reset all internal block number guards and caches
this._lastBlockNumber = -2;
this._fastBlockNumber = null;
this._fastBlockNumberPromise = null;
this._fastQueryDate = 0;
this._emitted.block = -2;
this._maxInternalBlockNumber = -1024;
this._internalBlockNumber = null;
// The "network" event MUST happen before this method resolves
// so any events have a chance to unregister, so we stall an
// additional event loop before returning from /this/ call
this.emit("network", currentNetwork, network);
return [4 /*yield*/, stall(0)];
case 3:
_a.sent();
return [2 /*return*/, this._network];
case 4:
error = logger.makeError("underlying network changed", logger_1.Logger.errors.NETWORK_ERROR, {
event: "changed",
network: network,
detectedNetwork: currentNetwork
});
this.emit("error", error);
throw error;
case 5: return [2 /*return*/, network];
}
});
});
2019-05-14 18:48:48 -04:00
};
Object.defineProperty(BaseProvider.prototype, "blockNumber", {
get: function () {
2020-05-04 23:01:04 -04:00
var _this = this;
this._getInternalBlockNumber(100 + this.pollingInterval / 2).then(function (blockNumber) {
_this._setFastBlockNumber(blockNumber);
2021-01-13 03:41:29 -05:00
}, function (error) { });
2020-05-04 23:01:04 -04:00
return (this._fastBlockNumber != null) ? this._fastBlockNumber : -1;
2019-05-14 18:48:48 -04:00
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2019-05-14 18:48:48 -04:00
configurable: true
});
Object.defineProperty(BaseProvider.prototype, "polling", {
get: function () {
return (this._poller != null);
},
set: function (value) {
var _this = this;
2020-05-04 23:01:04 -04:00
if (value && !this._poller) {
2021-01-13 03:41:29 -05:00
this._poller = setInterval(function () { _this.poll(); }, this.pollingInterval);
2020-05-04 23:01:04 -04:00
if (!this._bootstrapPoll) {
this._bootstrapPoll = setTimeout(function () {
_this.poll();
// We block additional polls until the polling interval
// is done, to prevent overwhelming the poll function
_this._bootstrapPoll = setTimeout(function () {
// If polling was disabled, something may require a poke
// since starting the bootstrap poll and it was disabled
if (!_this._poller) {
_this.poll();
}
// Clear out the bootstrap so we can do another
_this._bootstrapPoll = null;
}, _this.pollingInterval);
}, 0);
2019-05-14 18:48:48 -04:00
}
2020-05-04 23:01:04 -04:00
}
else if (!value && this._poller) {
clearInterval(this._poller);
this._poller = null;
}
2019-05-14 18:48:48 -04:00
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2019-05-14 18:48:48 -04:00
configurable: true
});
Object.defineProperty(BaseProvider.prototype, "pollingInterval", {
get: function () {
return this._pollingInterval;
},
set: function (value) {
var _this = this;
if (typeof (value) !== "number" || value <= 0 || parseInt(String(value)) != value) {
throw new Error("invalid polling interval");
}
this._pollingInterval = value;
if (this._poller) {
clearInterval(this._poller);
this._poller = setInterval(function () { _this.poll(); }, this._pollingInterval);
}
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2019-05-14 18:48:48 -04:00
configurable: true
});
BaseProvider.prototype._getFastBlockNumber = function () {
var _this = this;
var now = getTime();
// Stale block number, request a newer value
if ((now - this._fastQueryDate) > 2 * this._pollingInterval) {
this._fastQueryDate = now;
this._fastBlockNumberPromise = this.getBlockNumber().then(function (blockNumber) {
if (_this._fastBlockNumber == null || blockNumber > _this._fastBlockNumber) {
_this._fastBlockNumber = blockNumber;
}
return _this._fastBlockNumber;
});
}
return this._fastBlockNumberPromise;
};
BaseProvider.prototype._setFastBlockNumber = function (blockNumber) {
// Older block, maybe a stale request
if (this._fastBlockNumber != null && blockNumber < this._fastBlockNumber) {
return;
}
// Update the time we updated the blocknumber
this._fastQueryDate = getTime();
// Newer block number, use it
if (this._fastBlockNumber == null || blockNumber > this._fastBlockNumber) {
this._fastBlockNumber = blockNumber;
this._fastBlockNumberPromise = Promise.resolve(blockNumber);
}
};
2020-02-04 01:06:47 -05:00
BaseProvider.prototype.waitForTransaction = function (transactionHash, confirmations, timeout) {
2021-05-17 16:19:36 -04:00
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._waitForTransaction(transactionHash, (confirmations == null) ? 1 : confirmations, timeout || 0, null)];
});
});
};
BaseProvider.prototype._waitForTransaction = function (transactionHash, confirmations, timeout, replaceable) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
var receipt;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
2021-05-17 16:19:36 -04:00
case 0: return [4 /*yield*/, this.getTransactionReceipt(transactionHash)];
2019-11-20 18:57:38 +09:00
case 1:
receipt = _a.sent();
// Receipt is already good
2020-02-04 01:06:47 -05:00
if ((receipt ? receipt.confirmations : 0) >= confirmations) {
2019-11-20 18:57:38 +09:00
return [2 /*return*/, receipt];
}
// Poll until the receipt is good...
2020-02-04 01:06:47 -05:00
return [2 /*return*/, new Promise(function (resolve, reject) {
2021-05-17 16:19:36 -04:00
var cancelFuncs = [];
2020-02-04 01:06:47 -05:00
var done = false;
2021-05-17 16:19:36 -04:00
var alreadyDone = function () {
if (done) {
return true;
}
done = true;
cancelFuncs.forEach(function (func) { func(); });
return false;
};
var minedHandler = function (receipt) {
2019-11-20 18:57:38 +09:00
if (receipt.confirmations < confirmations) {
return;
}
2021-05-17 16:19:36 -04:00
if (alreadyDone()) {
2020-02-04 01:06:47 -05:00
return;
}
2019-11-20 18:57:38 +09:00
resolve(receipt);
};
2021-05-17 16:19:36 -04:00
_this.on(transactionHash, minedHandler);
cancelFuncs.push(function () { _this.removeListener(transactionHash, minedHandler); });
if (replaceable) {
var lastBlockNumber_1 = replaceable.startBlock;
var scannedBlock_1 = null;
var replaceHandler_1 = function (blockNumber) { return __awaiter(_this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (done) {
return [2 /*return*/];
}
// Wait 1 second; this is only used in the case of a fault, so
// we will trade off a little bit of latency for more consistent
// results and fewer JSON-RPC calls
return [4 /*yield*/, stall(1000)];
case 1:
// Wait 1 second; this is only used in the case of a fault, so
// we will trade off a little bit of latency for more consistent
// results and fewer JSON-RPC calls
_a.sent();
this.getTransactionCount(replaceable.from).then(function (nonce) { return __awaiter(_this, void 0, void 0, function () {
var mined, block, ti, tx, receipt_1, reason;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (done) {
return [2 /*return*/];
}
if (!(nonce <= replaceable.nonce)) return [3 /*break*/, 1];
lastBlockNumber_1 = blockNumber;
return [3 /*break*/, 9];
case 1: return [4 /*yield*/, this.getTransaction(transactionHash)];
case 2:
mined = _a.sent();
if (mined && mined.blockNumber != null) {
return [2 /*return*/];
}
// First time scanning. We start a little earlier for some
// wiggle room here to handle the eventually consistent nature
// of blockchain (e.g. the getTransactionCount was for a
// different block)
if (scannedBlock_1 == null) {
scannedBlock_1 = lastBlockNumber_1 - 3;
if (scannedBlock_1 < replaceable.startBlock) {
scannedBlock_1 = replaceable.startBlock;
}
}
_a.label = 3;
case 3:
if (!(scannedBlock_1 <= blockNumber)) return [3 /*break*/, 9];
if (done) {
return [2 /*return*/];
}
return [4 /*yield*/, this.getBlockWithTransactions(scannedBlock_1)];
case 4:
block = _a.sent();
ti = 0;
_a.label = 5;
case 5:
if (!(ti < block.transactions.length)) return [3 /*break*/, 8];
tx = block.transactions[ti];
// Successfully mined!
if (tx.hash === transactionHash) {
return [2 /*return*/];
}
if (!(tx.from === replaceable.from && tx.nonce === replaceable.nonce)) return [3 /*break*/, 7];
if (done) {
return [2 /*return*/];
}
return [4 /*yield*/, this.waitForTransaction(tx.hash, confirmations)];
case 6:
receipt_1 = _a.sent();
// Already resolved or rejected (prolly a timeout)
if (alreadyDone()) {
return [2 /*return*/];
}
reason = "replaced";
if (tx.data === replaceable.data && tx.to === replaceable.to && tx.value.eq(replaceable.value)) {
reason = "repriced";
}
else if (tx.data === "0x" && tx.from === tx.to && tx.value.isZero()) {
reason = "cancelled";
}
// Explain why we were replaced
reject(logger.makeError("transaction was replaced", logger_1.Logger.errors.TRANSACTION_REPLACED, {
cancelled: (reason === "replaced" || reason === "cancelled"),
reason: reason,
replacement: this._wrapTransaction(tx),
hash: transactionHash,
receipt: receipt_1
}));
return [2 /*return*/];
case 7:
ti++;
return [3 /*break*/, 5];
case 8:
scannedBlock_1++;
return [3 /*break*/, 3];
case 9:
if (done) {
return [2 /*return*/];
}
this.once("block", replaceHandler_1);
return [2 /*return*/];
}
});
}); }, function (error) {
if (done) {
return;
}
_this.once("block", replaceHandler_1);
});
return [2 /*return*/];
}
});
}); };
if (done) {
return;
}
_this.once("block", replaceHandler_1);
cancelFuncs.push(function () {
_this.removeListener("block", replaceHandler_1);
});
}
2020-02-04 01:06:47 -05:00
if (typeof (timeout) === "number" && timeout > 0) {
2021-05-17 16:19:36 -04:00
var timer_1 = setTimeout(function () {
if (alreadyDone()) {
2020-02-04 01:06:47 -05:00
return;
}
reject(logger.makeError("timeout exceeded", logger_1.Logger.errors.TIMEOUT, { timeout: timeout }));
}, timeout);
2021-05-17 16:19:36 -04:00
if (timer_1.unref) {
timer_1.unref();
2020-02-04 01:06:47 -05:00
}
2021-05-17 16:19:36 -04:00
cancelFuncs.push(function () { clearTimeout(timer_1); });
2020-02-04 01:06:47 -05:00
}
2019-11-20 18:57:38 +09:00
})];
2019-05-14 18:48:48 -04:00
}
});
});
};
BaseProvider.prototype.getBlockNumber = function () {
2020-06-03 03:47:17 -04:00
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._getInternalBlockNumber(0)];
});
});
2019-05-14 18:48:48 -04:00
};
BaseProvider.prototype.getGasPrice = function () {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2021-02-12 19:06:01 -05:00
var result;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
2021-02-12 19:06:01 -05:00
_a.sent();
2019-11-20 18:57:38 +09:00
return [4 /*yield*/, this.perform("getGasPrice", {})];
2021-02-12 19:06:01 -05:00
case 2:
result = _a.sent();
try {
return [2 /*return*/, bignumber_1.BigNumber.from(result)];
}
catch (error) {
return [2 /*return*/, logger.throwError("bad result from backend", logger_1.Logger.errors.SERVER_ERROR, {
method: "getGasPrice",
2021-10-16 02:29:27 -04:00
result: result,
error: error
2021-02-12 19:06:01 -05:00
})];
}
return [2 /*return*/];
2019-11-20 18:57:38 +09:00
}
});
2019-05-14 18:48:48 -04:00
});
};
BaseProvider.prototype.getBalance = function (addressOrName, blockTag) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2021-02-12 19:06:01 -05:00
var params, result;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
2021-02-12 19:06:01 -05:00
_a.sent();
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, (0, properties_1.resolveProperties)({
2019-11-20 18:57:38 +09:00
address: this._getAddress(addressOrName),
blockTag: this._getBlockTag(blockTag)
})];
case 2:
2021-02-12 19:06:01 -05:00
params = _a.sent();
2019-11-20 18:57:38 +09:00
return [4 /*yield*/, this.perform("getBalance", params)];
2021-02-12 19:06:01 -05:00
case 3:
result = _a.sent();
try {
return [2 /*return*/, bignumber_1.BigNumber.from(result)];
}
catch (error) {
return [2 /*return*/, logger.throwError("bad result from backend", logger_1.Logger.errors.SERVER_ERROR, {
method: "getBalance",
2021-10-16 02:29:27 -04:00
params: params,
result: result,
error: error
2021-02-12 19:06:01 -05:00
})];
}
return [2 /*return*/];
2019-11-20 18:57:38 +09:00
}
});
2019-05-14 18:48:48 -04:00
});
};
BaseProvider.prototype.getTransactionCount = function (addressOrName, blockTag) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2021-02-12 19:06:01 -05:00
var params, result;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
2021-02-12 19:06:01 -05:00
_a.sent();
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, (0, properties_1.resolveProperties)({
2019-11-20 18:57:38 +09:00
address: this._getAddress(addressOrName),
blockTag: this._getBlockTag(blockTag)
})];
case 2:
2021-02-12 19:06:01 -05:00
params = _a.sent();
2019-11-20 18:57:38 +09:00
return [4 /*yield*/, this.perform("getTransactionCount", params)];
2021-02-12 19:06:01 -05:00
case 3:
result = _a.sent();
try {
return [2 /*return*/, bignumber_1.BigNumber.from(result).toNumber()];
}
catch (error) {
return [2 /*return*/, logger.throwError("bad result from backend", logger_1.Logger.errors.SERVER_ERROR, {
method: "getTransactionCount",
2021-10-16 02:29:27 -04:00
params: params,
result: result,
error: error
2021-02-12 19:06:01 -05:00
})];
}
return [2 /*return*/];
2019-11-20 18:57:38 +09:00
}
});
2019-05-14 18:48:48 -04:00
});
};
BaseProvider.prototype.getCode = function (addressOrName, blockTag) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2021-02-12 19:06:01 -05:00
var params, result;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
2021-02-12 19:06:01 -05:00
_a.sent();
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, (0, properties_1.resolveProperties)({
2019-11-20 18:57:38 +09:00
address: this._getAddress(addressOrName),
blockTag: this._getBlockTag(blockTag)
})];
case 2:
2021-02-12 19:06:01 -05:00
params = _a.sent();
2019-11-20 18:57:38 +09:00
return [4 /*yield*/, this.perform("getCode", params)];
2021-02-12 19:06:01 -05:00
case 3:
result = _a.sent();
try {
2021-10-16 02:29:27 -04:00
return [2 /*return*/, (0, bytes_1.hexlify)(result)];
2021-02-12 19:06:01 -05:00
}
catch (error) {
return [2 /*return*/, logger.throwError("bad result from backend", logger_1.Logger.errors.SERVER_ERROR, {
method: "getCode",
2021-10-16 02:29:27 -04:00
params: params,
result: result,
error: error
2021-02-12 19:06:01 -05:00
})];
}
return [2 /*return*/];
2019-11-20 18:57:38 +09:00
}
});
2019-05-14 18:48:48 -04:00
});
};
BaseProvider.prototype.getStorageAt = function (addressOrName, position, blockTag) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2021-02-12 19:06:01 -05:00
var params, result;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
2021-02-12 19:06:01 -05:00
_a.sent();
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, (0, properties_1.resolveProperties)({
2019-11-20 18:57:38 +09:00
address: this._getAddress(addressOrName),
blockTag: this._getBlockTag(blockTag),
2021-10-16 02:29:27 -04:00
position: Promise.resolve(position).then(function (p) { return (0, bytes_1.hexValue)(p); })
2019-11-20 18:57:38 +09:00
})];
case 2:
2021-02-12 19:06:01 -05:00
params = _a.sent();
2019-11-20 18:57:38 +09:00
return [4 /*yield*/, this.perform("getStorageAt", params)];
2021-02-12 19:06:01 -05:00
case 3:
result = _a.sent();
try {
2021-10-16 02:29:27 -04:00
return [2 /*return*/, (0, bytes_1.hexlify)(result)];
2021-02-12 19:06:01 -05:00
}
catch (error) {
return [2 /*return*/, logger.throwError("bad result from backend", logger_1.Logger.errors.SERVER_ERROR, {
method: "getStorageAt",
2021-10-16 02:29:27 -04:00
params: params,
result: result,
error: error
2021-02-12 19:06:01 -05:00
})];
}
return [2 /*return*/];
2019-11-20 18:57:38 +09:00
}
});
2019-05-14 18:48:48 -04:00
});
};
// This should be called by any subclass wrapping a TransactionResponse
2021-05-17 16:19:36 -04:00
BaseProvider.prototype._wrapTransaction = function (tx, hash, startBlock) {
2019-05-14 18:48:48 -04:00
var _this = this;
2021-10-16 02:29:27 -04:00
if (hash != null && (0, bytes_1.hexDataLength)(hash) !== 32) {
2019-05-14 18:48:48 -04:00
throw new Error("invalid response - sendTransaction");
}
var result = tx;
// Check the hash we expect is the same as the hash the server reported
if (hash != null && tx.hash !== hash) {
2019-08-02 02:10:58 -04:00
logger.throwError("Transaction hash mismatch from Provider.sendTransaction.", logger_1.Logger.errors.UNKNOWN_ERROR, { expectedHash: tx.hash, returnedHash: hash });
2019-05-14 18:48:48 -04:00
}
2021-05-17 16:19:36 -04:00
result.wait = function (confirms, timeout) { return __awaiter(_this, void 0, void 0, function () {
var replacement, receipt;
2019-11-20 18:57:38 +09:00
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
2021-05-17 16:19:36 -04:00
if (confirms == null) {
confirms = 1;
}
if (timeout == null) {
timeout = 0;
2019-11-20 18:57:38 +09:00
}
2021-05-17 16:19:36 -04:00
replacement = undefined;
if (confirms !== 0 && startBlock != null) {
replacement = {
data: tx.data,
from: tx.from,
nonce: tx.nonce,
to: tx.to,
value: tx.value,
startBlock: startBlock
};
}
return [4 /*yield*/, this._waitForTransaction(tx.hash, confirms, timeout, replacement)];
2019-11-20 18:57:38 +09:00
case 1:
receipt = _a.sent();
2021-05-17 16:19:36 -04:00
if (receipt == null && confirms === 0) {
2019-11-20 18:57:38 +09:00
return [2 /*return*/, null];
}
// No longer pending, allow the polling loop to garbage collect this
this._emitted["t:" + tx.hash] = receipt.blockNumber;
if (receipt.status === 0) {
logger.throwError("transaction failed", logger_1.Logger.errors.CALL_EXCEPTION, {
transactionHash: tx.hash,
transaction: tx,
receipt: receipt
});
}
return [2 /*return*/, receipt];
2019-05-14 18:48:48 -04:00
}
});
2019-11-20 18:57:38 +09:00
}); };
2019-05-14 18:48:48 -04:00
return result;
};
BaseProvider.prototype.sendTransaction = function (signedTransaction) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2021-10-19 00:01:37 -04:00
var hexTx, tx, blockNumber, hash, error_7;
2019-11-20 18:57:38 +09:00
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
_a.sent();
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, Promise.resolve(signedTransaction).then(function (t) { return (0, bytes_1.hexlify)(t); })];
2019-11-20 18:57:38 +09:00
case 2:
hexTx = _a.sent();
tx = this.formatter.transaction(signedTransaction);
2021-07-23 02:21:24 -04:00
if (tx.confirmations == null) {
tx.confirmations = 0;
}
2021-05-17 16:19:36 -04:00
return [4 /*yield*/, this._getInternalBlockNumber(100 + 2 * this.pollingInterval)];
2019-11-20 18:57:38 +09:00
case 3:
2021-05-17 16:19:36 -04:00
blockNumber = _a.sent();
_a.label = 4;
2019-11-20 18:57:38 +09:00
case 4:
2021-05-17 16:19:36 -04:00
_a.trys.push([4, 6, , 7]);
return [4 /*yield*/, this.perform("sendTransaction", { signedTransaction: hexTx })];
2019-11-20 18:57:38 +09:00
case 5:
2021-05-17 16:19:36 -04:00
hash = _a.sent();
return [2 /*return*/, this._wrapTransaction(tx, hash, blockNumber)];
case 6:
2021-10-19 00:01:37 -04:00
error_7 = _a.sent();
error_7.transaction = tx;
error_7.transactionHash = tx.hash;
throw error_7;
2021-05-17 16:19:36 -04:00
case 7: return [2 /*return*/];
2019-11-20 18:57:38 +09:00
}
});
2019-05-14 18:48:48 -04:00
});
};
BaseProvider.prototype._getTransactionRequest = function (transaction) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
var values, tx, _a, _b;
var _this = this;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, transaction];
case 1:
values = _c.sent();
tx = {};
["from", "to"].forEach(function (key) {
if (values[key] == null) {
return;
}
tx[key] = Promise.resolve(values[key]).then(function (v) { return (v ? _this._getAddress(v) : null); });
});
2021-07-29 23:30:17 -04:00
["gasLimit", "gasPrice", "maxFeePerGas", "maxPriorityFeePerGas", "value"].forEach(function (key) {
2019-11-20 18:57:38 +09:00
if (values[key] == null) {
return;
}
tx[key] = Promise.resolve(values[key]).then(function (v) { return (v ? bignumber_1.BigNumber.from(v) : null); });
});
2021-03-30 15:22:45 -04:00
["type"].forEach(function (key) {
if (values[key] == null) {
return;
}
tx[key] = Promise.resolve(values[key]).then(function (v) { return ((v != null) ? v : null); });
});
if (values.accessList) {
tx.accessList = this.formatter.accessList(values.accessList);
}
2019-11-20 18:57:38 +09:00
["data"].forEach(function (key) {
if (values[key] == null) {
return;
}
2021-10-16 02:29:27 -04:00
tx[key] = Promise.resolve(values[key]).then(function (v) { return (v ? (0, bytes_1.hexlify)(v) : null); });
2019-11-20 18:57:38 +09:00
});
_b = (_a = this.formatter).transactionRequest;
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, (0, properties_1.resolveProperties)(tx)];
2019-11-20 18:57:38 +09:00
case 2: return [2 /*return*/, _b.apply(_a, [_c.sent()])];
2019-05-14 18:48:48 -04:00
}
});
});
};
BaseProvider.prototype._getFilter = function (filter) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
var result, _a, _b;
var _this = this;
return __generator(this, function (_c) {
switch (_c.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, filter];
2019-11-20 18:57:38 +09:00
case 1:
filter = _c.sent();
result = {};
if (filter.address != null) {
result.address = this._getAddress(filter.address);
}
["blockHash", "topics"].forEach(function (key) {
if (filter[key] == null) {
return;
}
result[key] = filter[key];
});
["fromBlock", "toBlock"].forEach(function (key) {
if (filter[key] == null) {
return;
}
result[key] = _this._getBlockTag(filter[key]);
});
_b = (_a = this.formatter).filter;
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, (0, properties_1.resolveProperties)(result)];
2020-06-03 03:47:17 -04:00
case 2: return [2 /*return*/, _b.apply(_a, [_c.sent()])];
2019-05-14 18:48:48 -04:00
}
});
});
};
2022-03-09 02:56:08 -05:00
BaseProvider.prototype._call = function (transaction, blockTag, attempt) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2022-03-09 02:56:08 -05:00
var txSender, result, data, sender, urls, urlsOffset, urlsLength, urlsData, u, url, calldata, callbackSelector, extraData, ccipResult, tx, error_8;
2021-02-12 19:06:01 -05:00
return __generator(this, function (_a) {
switch (_a.label) {
2022-03-09 02:56:08 -05:00
case 0:
if (attempt >= MAX_CCIP_REDIRECTS) {
logger.throwError("CCIP read exceeded maximum redirections", logger_1.Logger.errors.SERVER_ERROR, {
redirects: attempt,
transaction: transaction
});
}
txSender = transaction.to;
return [4 /*yield*/, this.perform("call", { transaction: transaction, blockTag: blockTag })];
2019-11-20 18:57:38 +09:00
case 1:
2022-03-09 02:56:08 -05:00
result = _a.sent();
if (!(attempt >= 0 && blockTag === "latest" && txSender != null && result.substring(0, 10) === "0x556f1830" && ((0, bytes_1.hexDataLength)(result) % 32 === 4))) return [3 /*break*/, 5];
_a.label = 2;
2019-11-20 18:57:38 +09:00
case 2:
2022-03-09 02:56:08 -05:00
_a.trys.push([2, 4, , 5]);
data = (0, bytes_1.hexDataSlice)(result, 4);
sender = (0, bytes_1.hexDataSlice)(data, 0, 32);
if (!bignumber_1.BigNumber.from(sender).eq(txSender)) {
logger.throwError("CCIP Read sender did not match", logger_1.Logger.errors.CALL_EXCEPTION, {
name: "OffchainLookup",
signature: "OffchainLookup(address,string[],bytes,bytes4,bytes)",
transaction: transaction,
data: result
});
}
urls = [];
urlsOffset = bignumber_1.BigNumber.from((0, bytes_1.hexDataSlice)(data, 32, 64)).toNumber();
urlsLength = bignumber_1.BigNumber.from((0, bytes_1.hexDataSlice)(data, urlsOffset, urlsOffset + 32)).toNumber();
urlsData = (0, bytes_1.hexDataSlice)(data, urlsOffset + 32);
for (u = 0; u < urlsLength; u++) {
url = _parseString(urlsData, u * 32);
if (url == null) {
logger.throwError("CCIP Read contained corrupt URL string", logger_1.Logger.errors.CALL_EXCEPTION, {
name: "OffchainLookup",
signature: "OffchainLookup(address,string[],bytes,bytes4,bytes)",
transaction: transaction,
data: result
});
}
urls.push(url);
}
calldata = _parseBytes(data, 64);
// Get the callbackSelector (bytes4)
if (!bignumber_1.BigNumber.from((0, bytes_1.hexDataSlice)(data, 100, 128)).isZero()) {
logger.throwError("CCIP Read callback selector included junk", logger_1.Logger.errors.CALL_EXCEPTION, {
name: "OffchainLookup",
signature: "OffchainLookup(address,string[],bytes,bytes4,bytes)",
transaction: transaction,
data: result
});
}
callbackSelector = (0, bytes_1.hexDataSlice)(data, 96, 100);
extraData = _parseBytes(data, 128);
return [4 /*yield*/, this.ccipReadFetch(transaction, calldata, urls)];
2021-02-12 19:06:01 -05:00
case 3:
2022-03-09 02:56:08 -05:00
ccipResult = _a.sent();
if (ccipResult == null) {
logger.throwError("CCIP Read disabled or provided no URLs", logger_1.Logger.errors.CALL_EXCEPTION, {
name: "OffchainLookup",
signature: "OffchainLookup(address,string[],bytes,bytes4,bytes)",
transaction: transaction,
data: result
});
}
tx = {
to: txSender,
data: (0, bytes_1.hexConcat)([callbackSelector, encodeBytes([ccipResult, extraData])])
};
return [2 /*return*/, this._call(tx, blockTag, attempt + 1)];
case 4:
error_8 = _a.sent();
if (error_8.code === logger_1.Logger.errors.SERVER_ERROR) {
throw error_8;
}
return [3 /*break*/, 5];
case 5:
2021-02-12 19:06:01 -05:00
try {
2021-10-16 02:29:27 -04:00
return [2 /*return*/, (0, bytes_1.hexlify)(result)];
2021-02-12 19:06:01 -05:00
}
catch (error) {
return [2 /*return*/, logger.throwError("bad result from backend", logger_1.Logger.errors.SERVER_ERROR, {
method: "call",
2022-03-09 02:56:08 -05:00
params: { transaction: transaction, blockTag: blockTag },
2021-10-16 02:29:27 -04:00
result: result,
error: error
2021-02-12 19:06:01 -05:00
})];
}
return [2 /*return*/];
2019-11-20 18:57:38 +09:00
}
});
2019-05-14 18:48:48 -04:00
});
};
2022-03-09 02:56:08 -05:00
BaseProvider.prototype.call = function (transaction, blockTag) {
return __awaiter(this, void 0, void 0, function () {
var resolved;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getNetwork()];
case 1:
_a.sent();
return [4 /*yield*/, (0, properties_1.resolveProperties)({
transaction: this._getTransactionRequest(transaction),
blockTag: this._getBlockTag(blockTag),
ccipReadEnabled: Promise.resolve(transaction.ccipReadEnabled)
})];
case 2:
resolved = _a.sent();
return [2 /*return*/, this._call(resolved.transaction, resolved.blockTag, resolved.ccipReadEnabled ? 0 : -1)];
}
});
});
};
2019-05-14 18:48:48 -04:00
BaseProvider.prototype.estimateGas = function (transaction) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2021-02-12 19:06:01 -05:00
var params, result;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
2021-02-12 19:06:01 -05:00
_a.sent();
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, (0, properties_1.resolveProperties)({
2019-11-20 18:57:38 +09:00
transaction: this._getTransactionRequest(transaction)
})];
case 2:
2021-02-12 19:06:01 -05:00
params = _a.sent();
2019-11-20 18:57:38 +09:00
return [4 /*yield*/, this.perform("estimateGas", params)];
2021-02-12 19:06:01 -05:00
case 3:
result = _a.sent();
try {
return [2 /*return*/, bignumber_1.BigNumber.from(result)];
}
catch (error) {
return [2 /*return*/, logger.throwError("bad result from backend", logger_1.Logger.errors.SERVER_ERROR, {
method: "estimateGas",
2021-10-16 02:29:27 -04:00
params: params,
result: result,
error: error
2021-02-12 19:06:01 -05:00
})];
}
return [2 /*return*/];
2019-11-20 18:57:38 +09:00
}
});
2019-05-14 18:48:48 -04:00
});
};
2019-05-23 19:13:44 -04:00
BaseProvider.prototype._getAddress = function (addressOrName) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
var address;
return __generator(this, function (_a) {
switch (_a.label) {
2021-10-16 02:29:27 -04:00
case 0: return [4 /*yield*/, addressOrName];
2019-11-20 18:57:38 +09:00
case 1:
2021-10-16 02:29:27 -04:00
addressOrName = _a.sent();
if (typeof (addressOrName) !== "string") {
logger.throwArgumentError("invalid address or ENS name", "name", addressOrName);
}
return [4 /*yield*/, this.resolveName(addressOrName)];
case 2:
2019-11-20 18:57:38 +09:00
address = _a.sent();
if (address == null) {
logger.throwError("ENS name not configured", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "resolveName(" + JSON.stringify(addressOrName) + ")"
});
}
return [2 /*return*/, address];
}
});
2019-05-23 19:13:44 -04:00
});
};
2019-05-14 18:48:48 -04:00
BaseProvider.prototype._getBlock = function (blockHashOrBlockTag, includeTransactions) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2022-03-09 02:56:08 -05:00
var blockNumber, params, _a, error_9;
2019-11-20 18:57:38 +09:00
var _this = this;
2021-10-16 02:29:27 -04:00
return __generator(this, function (_b) {
switch (_b.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
2021-10-16 02:29:27 -04:00
_b.sent();
2019-11-20 18:57:38 +09:00
return [4 /*yield*/, blockHashOrBlockTag];
case 2:
2021-10-16 02:29:27 -04:00
blockHashOrBlockTag = _b.sent();
2019-11-20 18:57:38 +09:00
blockNumber = -128;
params = {
includeTransactions: !!includeTransactions
};
2021-10-16 02:29:27 -04:00
if (!(0, bytes_1.isHexString)(blockHashOrBlockTag, 32)) return [3 /*break*/, 3];
2019-11-20 18:57:38 +09:00
params.blockHash = blockHashOrBlockTag;
2020-06-03 03:47:17 -04:00
return [3 /*break*/, 6];
case 3:
2021-10-16 02:29:27 -04:00
_b.trys.push([3, 5, , 6]);
2019-11-20 18:57:38 +09:00
_a = params;
return [4 /*yield*/, this._getBlockTag(blockHashOrBlockTag)];
2020-06-03 03:47:17 -04:00
case 4:
2021-10-16 02:29:27 -04:00
_a.blockTag = _b.sent();
if ((0, bytes_1.isHexString)(params.blockTag)) {
2019-05-14 18:48:48 -04:00
blockNumber = parseInt(params.blockTag.substring(2), 16);
}
2020-06-03 03:47:17 -04:00
return [3 /*break*/, 6];
case 5:
2022-03-09 02:56:08 -05:00
error_9 = _b.sent();
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("invalid block hash or block tag", "blockHashOrBlockTag", blockHashOrBlockTag);
2020-06-03 03:47:17 -04:00
return [3 /*break*/, 6];
2021-10-16 02:29:27 -04:00
case 6: return [2 /*return*/, (0, web_1.poll)(function () { return __awaiter(_this, void 0, void 0, function () {
2021-07-23 02:21:24 -04:00
var block, blockNumber_1, i, tx, confirmations, blockWithTxs;
var _this = this;
2019-11-20 18:57:38 +09:00
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.perform("getBlock", params)];
case 1:
block = _a.sent();
// Block was not found
if (block == null) {
// For blockhashes, if we didn't say it existed, that blockhash may
// not exist. If we did see it though, perhaps from a log, we know
// it exists, and this node is just not caught up yet.
if (params.blockHash != null) {
if (this._emitted["b:" + params.blockHash] == null) {
return [2 /*return*/, null];
}
}
// For block tags, if we are asking for a future block, we return null
if (params.blockTag != null) {
if (blockNumber > this._emitted.block) {
return [2 /*return*/, null];
}
}
// Retry on the next block
return [2 /*return*/, undefined];
}
2020-01-18 21:48:12 -05:00
if (!includeTransactions) return [3 /*break*/, 8];
blockNumber_1 = null;
i = 0;
_a.label = 2;
case 2:
if (!(i < block.transactions.length)) return [3 /*break*/, 7];
tx = block.transactions[i];
if (!(tx.blockNumber == null)) return [3 /*break*/, 3];
tx.confirmations = 0;
return [3 /*break*/, 6];
case 3:
if (!(tx.confirmations == null)) return [3 /*break*/, 6];
if (!(blockNumber_1 == null)) return [3 /*break*/, 5];
return [4 /*yield*/, this._getInternalBlockNumber(100 + 2 * this.pollingInterval)];
case 4:
blockNumber_1 = _a.sent();
_a.label = 5;
case 5:
confirmations = (blockNumber_1 - tx.blockNumber) + 1;
if (confirmations <= 0) {
confirmations = 1;
2019-11-20 18:57:38 +09:00
}
2020-01-18 21:48:12 -05:00
tx.confirmations = confirmations;
_a.label = 6;
case 6:
i++;
return [3 /*break*/, 2];
2021-07-23 02:21:24 -04:00
case 7:
blockWithTxs = this.formatter.blockWithTransactions(block);
2021-08-18 03:05:48 -03:00
blockWithTxs.transactions = blockWithTxs.transactions.map(function (tx) { return _this._wrapTransaction(tx); });
2021-07-23 02:21:24 -04:00
return [2 /*return*/, blockWithTxs];
2020-01-18 21:48:12 -05:00
case 8: return [2 /*return*/, this.formatter.block(block)];
2019-05-14 18:48:48 -04:00
}
2019-11-20 18:57:38 +09:00
});
2020-05-29 21:27:59 -04:00
}); }, { oncePoll: this })];
2019-11-20 18:57:38 +09:00
}
2019-05-14 18:48:48 -04:00
});
});
};
BaseProvider.prototype.getBlock = function (blockHashOrBlockTag) {
return (this._getBlock(blockHashOrBlockTag, false));
};
BaseProvider.prototype.getBlockWithTransactions = function (blockHashOrBlockTag) {
return (this._getBlock(blockHashOrBlockTag, true));
};
BaseProvider.prototype.getTransaction = function (transactionHash) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
var params;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
_a.sent();
return [4 /*yield*/, transactionHash];
case 2:
transactionHash = _a.sent();
params = { transactionHash: this.formatter.hash(transactionHash, true) };
2021-10-16 02:29:27 -04:00
return [2 /*return*/, (0, web_1.poll)(function () { return __awaiter(_this, void 0, void 0, function () {
2019-11-20 18:57:38 +09:00
var result, tx, blockNumber, confirmations;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.perform("getTransaction", params)];
case 1:
result = _a.sent();
if (result == null) {
if (this._emitted["t:" + transactionHash] == null) {
return [2 /*return*/, null];
}
return [2 /*return*/, undefined];
}
tx = this.formatter.transactionResponse(result);
if (!(tx.blockNumber == null)) return [3 /*break*/, 2];
tx.confirmations = 0;
return [3 /*break*/, 4];
case 2:
if (!(tx.confirmations == null)) return [3 /*break*/, 4];
return [4 /*yield*/, this._getInternalBlockNumber(100 + 2 * this.pollingInterval)];
case 3:
blockNumber = _a.sent();
confirmations = (blockNumber - tx.blockNumber) + 1;
if (confirmations <= 0) {
confirmations = 1;
}
tx.confirmations = confirmations;
_a.label = 4;
case 4: return [2 /*return*/, this._wrapTransaction(tx)];
}
});
2020-05-29 21:27:59 -04:00
}); }, { oncePoll: this })];
2019-11-20 18:57:38 +09:00
}
2019-05-14 18:48:48 -04:00
});
});
};
BaseProvider.prototype.getTransactionReceipt = function (transactionHash) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
var params;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
_a.sent();
return [4 /*yield*/, transactionHash];
case 2:
transactionHash = _a.sent();
params = { transactionHash: this.formatter.hash(transactionHash, true) };
2021-10-16 02:29:27 -04:00
return [2 /*return*/, (0, web_1.poll)(function () { return __awaiter(_this, void 0, void 0, function () {
2019-11-20 18:57:38 +09:00
var result, receipt, blockNumber, confirmations;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.perform("getTransactionReceipt", params)];
case 1:
result = _a.sent();
if (result == null) {
if (this._emitted["t:" + transactionHash] == null) {
return [2 /*return*/, null];
}
return [2 /*return*/, undefined];
}
// "geth-etc" returns receipts before they are ready
if (result.blockHash == null) {
return [2 /*return*/, undefined];
}
receipt = this.formatter.receipt(result);
if (!(receipt.blockNumber == null)) return [3 /*break*/, 2];
receipt.confirmations = 0;
return [3 /*break*/, 4];
case 2:
if (!(receipt.confirmations == null)) return [3 /*break*/, 4];
return [4 /*yield*/, this._getInternalBlockNumber(100 + 2 * this.pollingInterval)];
case 3:
blockNumber = _a.sent();
confirmations = (blockNumber - receipt.blockNumber) + 1;
if (confirmations <= 0) {
confirmations = 1;
}
receipt.confirmations = confirmations;
_a.label = 4;
case 4: return [2 /*return*/, receipt];
}
});
2020-05-29 21:27:59 -04:00
}); }, { oncePoll: this })];
2019-11-20 18:57:38 +09:00
}
2019-05-14 18:48:48 -04:00
});
});
};
BaseProvider.prototype.getLogs = function (filter) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
var params, logs;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
_a.sent();
2021-10-16 02:29:27 -04:00
return [4 /*yield*/, (0, properties_1.resolveProperties)({ filter: this._getFilter(filter) })];
2019-11-20 18:57:38 +09:00
case 2:
params = _a.sent();
return [4 /*yield*/, this.perform("getLogs", params)];
case 3:
logs = _a.sent();
2020-01-18 21:48:12 -05:00
logs.forEach(function (log) {
if (log.removed == null) {
log.removed = false;
}
});
2019-11-20 18:57:38 +09:00
return [2 /*return*/, formatter_1.Formatter.arrayOf(this.formatter.filterLog.bind(this.formatter))(logs)];
}
});
2019-05-14 18:48:48 -04:00
});
};
BaseProvider.prototype.getEtherPrice = function () {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
_a.sent();
return [2 /*return*/, this.perform("getEtherPrice", {})];
}
});
2019-05-14 18:48:48 -04:00
});
};
BaseProvider.prototype._getBlockTag = function (blockTag) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
var blockNumber;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, blockTag];
2019-11-20 18:57:38 +09:00
case 1:
blockTag = _a.sent();
2020-06-03 03:47:17 -04:00
if (!(typeof (blockTag) === "number" && blockTag < 0)) return [3 /*break*/, 3];
2019-11-20 18:57:38 +09:00
if (blockTag % 1) {
logger.throwArgumentError("invalid BlockTag", "blockTag", blockTag);
}
return [4 /*yield*/, this._getInternalBlockNumber(100 + 2 * this.pollingInterval)];
2020-06-03 03:47:17 -04:00
case 2:
2019-11-20 18:57:38 +09:00
blockNumber = _a.sent();
blockNumber += blockTag;
if (blockNumber < 0) {
blockNumber = 0;
}
return [2 /*return*/, this.formatter.blockTag(blockNumber)];
2020-06-03 03:47:17 -04:00
case 3: return [2 /*return*/, this.formatter.blockTag(blockTag)];
2019-05-14 18:48:48 -04:00
}
});
2019-11-20 18:57:38 +09:00
});
2019-05-14 18:48:48 -04:00
};
2020-09-04 01:37:14 -04:00
BaseProvider.prototype.getResolver = function (name) {
return __awaiter(this, void 0, void 0, function () {
2022-03-09 02:56:08 -05:00
var currentName, addr, resolver, _a;
return __generator(this, function (_b) {
switch (_b.label) {
2021-06-24 02:13:06 -04:00
case 0:
2022-03-09 02:56:08 -05:00
currentName = name;
_b.label = 1;
2020-09-04 01:37:14 -04:00
case 1:
2022-03-09 02:56:08 -05:00
if (!true) return [3 /*break*/, 6];
if (currentName === "" || currentName === ".") {
return [2 /*return*/, null];
}
// Optimization since the eth node cannot change and does
2022-05-01 02:11:22 -04:00
// not have a wildcard resolver
2022-03-09 02:56:08 -05:00
if (name !== "eth" && currentName === "eth") {
2020-09-04 01:37:14 -04:00
return [2 /*return*/, null];
}
2022-03-09 02:56:08 -05:00
return [4 /*yield*/, this._getResolver(currentName, "getResolver")];
2021-06-24 02:13:06 -04:00
case 2:
2022-03-09 02:56:08 -05:00
addr = _b.sent();
if (!(addr != null)) return [3 /*break*/, 5];
resolver = new Resolver(this, addr, name);
_a = currentName !== name;
if (!_a) return [3 /*break*/, 4];
return [4 /*yield*/, resolver.supportsWildcard()];
case 3:
_a = !(_b.sent());
_b.label = 4;
case 4:
// Legacy resolver found, using EIP-2544 so it isn't safe to use
if (_a) {
2021-06-24 02:13:06 -04:00
return [2 /*return*/, null];
}
2022-03-09 02:56:08 -05:00
return [2 /*return*/, resolver];
case 5:
// Get the parent node
currentName = currentName.split(".").slice(1).join(".");
return [3 /*break*/, 1];
case 6: return [2 /*return*/];
2020-09-04 01:37:14 -04:00
}
});
});
};
2022-03-09 02:56:08 -05:00
BaseProvider.prototype._getResolver = function (name, operation) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2022-03-09 02:56:08 -05:00
var network, addrData, error_10;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (operation == null) {
operation = "ENS";
}
return [4 /*yield*/, this.getNetwork()];
2019-11-20 18:57:38 +09:00
case 1:
2022-03-09 02:56:08 -05:00
network = _a.sent();
2019-11-20 18:57:38 +09:00
// No ENS...
if (!network.ensAddress) {
2022-03-09 02:56:08 -05:00
logger.throwError("network does not support ENS", logger_1.Logger.errors.UNSUPPORTED_OPERATION, { operation: operation, network: network.name });
2019-11-20 18:57:38 +09:00
}
2022-03-09 02:56:08 -05:00
_a.label = 2;
2021-06-24 02:13:06 -04:00
case 2:
2022-03-09 02:56:08 -05:00
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, this.call({
to: network.ensAddress,
data: ("0x0178b8bf" + (0, hash_1.namehash)(name).substring(2))
})];
case 3:
addrData = _a.sent();
return [2 /*return*/, this.formatter.callAddress(addrData)];
2021-06-24 02:13:06 -04:00
case 4:
2022-03-09 02:56:08 -05:00
error_10 = _a.sent();
return [3 /*break*/, 5];
case 5: return [2 /*return*/, null];
2019-11-20 18:57:38 +09:00
}
2019-05-14 18:48:48 -04:00
});
});
};
BaseProvider.prototype.resolveName = function (name) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2020-09-04 01:37:14 -04:00
var resolver;
return __generator(this, function (_a) {
switch (_a.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, name];
2019-11-20 18:57:38 +09:00
case 1:
2020-09-04 01:37:14 -04:00
name = _a.sent();
2019-11-20 18:57:38 +09:00
// If it is already an address, nothing to resolve
try {
return [2 /*return*/, Promise.resolve(this.formatter.address(name))];
}
2020-01-07 19:58:04 -05:00
catch (error) {
// If is is a hexstring, the address is bad (See #694)
2021-10-16 02:29:27 -04:00
if ((0, bytes_1.isHexString)(name)) {
2020-01-07 19:58:04 -05:00
throw error;
}
}
2020-04-03 22:13:06 -04:00
if (typeof (name) !== "string") {
logger.throwArgumentError("invalid ENS name", "name", name);
}
2020-09-04 01:37:14 -04:00
return [4 /*yield*/, this.getResolver(name)];
2020-06-03 03:47:17 -04:00
case 2:
2020-09-04 01:37:14 -04:00
resolver = _a.sent();
if (!resolver) {
2019-11-20 18:57:38 +09:00
return [2 /*return*/, null];
}
2020-09-04 01:37:14 -04:00
return [4 /*yield*/, resolver.getAddress()];
case 3: return [2 /*return*/, _a.sent()];
2019-11-20 18:57:38 +09:00
}
2019-05-14 18:48:48 -04:00
});
});
};
BaseProvider.prototype.lookupAddress = function (address) {
2019-11-20 18:57:38 +09:00
return __awaiter(this, void 0, void 0, function () {
2022-03-09 02:56:08 -05:00
var node, resolverAddr, name, _a, addr;
2019-11-20 18:57:38 +09:00
return __generator(this, function (_b) {
switch (_b.label) {
2020-06-03 03:47:17 -04:00
case 0: return [4 /*yield*/, address];
2019-11-20 18:57:38 +09:00
case 1:
address = _b.sent();
address = this.formatter.address(address);
2022-03-09 02:56:08 -05:00
node = address.substring(2).toLowerCase() + ".addr.reverse";
return [4 /*yield*/, this._getResolver(node, "lookupAddress")];
2020-06-03 03:47:17 -04:00
case 2:
2022-03-09 02:56:08 -05:00
resolverAddr = _b.sent();
if (resolverAddr == null) {
2019-11-20 18:57:38 +09:00
return [2 /*return*/, null];
}
2022-03-09 02:56:08 -05:00
_a = _parseString;
2019-11-20 18:57:38 +09:00
return [4 /*yield*/, this.call({
2022-03-09 02:56:08 -05:00
to: resolverAddr,
data: ("0x691f3431" + (0, hash_1.namehash)(node).substring(2))
2019-11-20 18:57:38 +09:00
})];
2020-06-03 03:47:17 -04:00
case 3:
2022-03-09 02:56:08 -05:00
name = _a.apply(void 0, [_b.sent(), 0]);
2019-11-20 18:57:38 +09:00
return [4 /*yield*/, this.resolveName(name)];
2020-06-03 03:47:17 -04:00
case 4:
2019-11-20 18:57:38 +09:00
addr = _b.sent();
if (addr != address) {
return [2 /*return*/, null];
}
return [2 /*return*/, name];
2019-05-14 18:48:48 -04:00
}
});
});
};
2021-10-19 00:01:37 -04:00
BaseProvider.prototype.getAvatar = function (nameOrAddress) {
return __awaiter(this, void 0, void 0, function () {
2022-03-09 02:56:08 -05:00
var resolver, address, node, resolverAddress, avatar_1, error_11, name_1, _a, error_12, avatar;
return __generator(this, function (_b) {
switch (_b.label) {
2021-10-19 00:01:37 -04:00
case 0:
resolver = null;
2022-03-09 02:56:08 -05:00
if (!(0, bytes_1.isHexString)(nameOrAddress)) return [3 /*break*/, 10];
2021-10-19 00:01:37 -04:00
address = this.formatter.address(nameOrAddress);
2022-03-09 02:56:08 -05:00
node = address.substring(2).toLowerCase() + ".addr.reverse";
return [4 /*yield*/, this._getResolver(node, "getAvatar")];
2021-10-19 00:01:37 -04:00
case 1:
2022-03-09 02:56:08 -05:00
resolverAddress = _b.sent();
2021-10-19 00:01:37 -04:00
if (!resolverAddress) {
return [2 /*return*/, null];
}
2022-03-09 02:56:08 -05:00
// Try resolving the avatar against the addr.reverse resolver
resolver = new Resolver(this, resolverAddress, node);
_b.label = 2;
case 2:
_b.trys.push([2, 4, , 5]);
return [4 /*yield*/, resolver.getAvatar()];
2021-10-19 00:01:37 -04:00
case 3:
2022-03-09 02:56:08 -05:00
avatar_1 = _b.sent();
if (avatar_1) {
return [2 /*return*/, avatar_1.url];
}
return [3 /*break*/, 5];
case 4:
error_11 = _b.sent();
if (error_11.code !== logger_1.Logger.errors.CALL_EXCEPTION) {
throw error_11;
}
return [3 /*break*/, 5];
case 5:
_b.trys.push([5, 8, , 9]);
_a = _parseString;
return [4 /*yield*/, this.call({
to: resolverAddress,
data: ("0x691f3431" + (0, hash_1.namehash)(node).substring(2))
})];
case 6:
name_1 = _a.apply(void 0, [_b.sent(), 0]);
return [4 /*yield*/, this.getResolver(name_1)];
case 7:
resolver = _b.sent();
return [3 /*break*/, 9];
case 8:
error_12 = _b.sent();
if (error_12.code !== logger_1.Logger.errors.CALL_EXCEPTION) {
throw error_12;
}
return [2 /*return*/, null];
case 9: return [3 /*break*/, 12];
case 10: return [4 /*yield*/, this.getResolver(nameOrAddress)];
case 11:
// ENS name; forward lookup with wildcard
resolver = _b.sent();
2021-11-30 11:19:37 -05:00
if (!resolver) {
return [2 /*return*/, null];
}
2022-03-09 02:56:08 -05:00
_b.label = 12;
case 12: return [4 /*yield*/, resolver.getAvatar()];
case 13:
avatar = _b.sent();
2021-10-19 00:01:37 -04:00
if (avatar == null) {
return [2 /*return*/, null];
}
return [2 /*return*/, avatar.url];
}
});
});
};
2019-05-14 18:48:48 -04:00
BaseProvider.prototype.perform = function (method, params) {
2019-08-02 02:10:58 -04:00
return logger.throwError(method + " not implemented", logger_1.Logger.errors.NOT_IMPLEMENTED, { operation: method });
2019-05-14 18:48:48 -04:00
};
2020-03-12 19:14:50 +01:00
BaseProvider.prototype._startEvent = function (event) {
this.polling = (this._events.filter(function (e) { return e.pollable(); }).length > 0);
2019-05-14 18:48:48 -04:00
};
2020-03-12 19:14:50 +01:00
BaseProvider.prototype._stopEvent = function (event) {
2019-05-14 18:48:48 -04:00
this.polling = (this._events.filter(function (e) { return e.pollable(); }).length > 0);
};
BaseProvider.prototype._addEventListener = function (eventName, listener, once) {
2020-03-12 19:14:50 +01:00
var event = new Event(getEventTag(eventName), listener, once);
this._events.push(event);
this._startEvent(event);
2019-05-14 18:48:48 -04:00
return this;
};
BaseProvider.prototype.on = function (eventName, listener) {
return this._addEventListener(eventName, listener, false);
};
BaseProvider.prototype.once = function (eventName, listener) {
return this._addEventListener(eventName, listener, true);
};
BaseProvider.prototype.emit = function (eventName) {
var _this = this;
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var result = false;
2020-03-12 19:14:50 +01:00
var stopped = [];
2019-05-14 18:48:48 -04:00
var eventTag = getEventTag(eventName);
this._events = this._events.filter(function (event) {
if (event.tag !== eventTag) {
return true;
}
setTimeout(function () {
event.listener.apply(_this, args);
}, 0);
result = true;
2020-03-12 19:14:50 +01:00
if (event.once) {
stopped.push(event);
return false;
}
return true;
2019-05-14 18:48:48 -04:00
});
2020-03-12 19:14:50 +01:00
stopped.forEach(function (event) { _this._stopEvent(event); });
2019-05-14 18:48:48 -04:00
return result;
};
BaseProvider.prototype.listenerCount = function (eventName) {
if (!eventName) {
return this._events.length;
}
var eventTag = getEventTag(eventName);
return this._events.filter(function (event) {
return (event.tag === eventTag);
}).length;
};
BaseProvider.prototype.listeners = function (eventName) {
if (eventName == null) {
return this._events.map(function (event) { return event.listener; });
}
var eventTag = getEventTag(eventName);
return this._events
.filter(function (event) { return (event.tag === eventTag); })
.map(function (event) { return event.listener; });
};
BaseProvider.prototype.off = function (eventName, listener) {
2020-03-12 19:14:50 +01:00
var _this = this;
2019-05-14 18:48:48 -04:00
if (listener == null) {
return this.removeAllListeners(eventName);
}
2020-03-12 19:14:50 +01:00
var stopped = [];
2019-05-14 18:48:48 -04:00
var found = false;
var eventTag = getEventTag(eventName);
this._events = this._events.filter(function (event) {
if (event.tag !== eventTag || event.listener != listener) {
return true;
}
if (found) {
return true;
}
found = true;
2020-03-12 19:14:50 +01:00
stopped.push(event);
2019-05-14 18:48:48 -04:00
return false;
});
2020-03-12 19:14:50 +01:00
stopped.forEach(function (event) { _this._stopEvent(event); });
2019-05-14 18:48:48 -04:00
return this;
};
BaseProvider.prototype.removeAllListeners = function (eventName) {
2020-03-12 19:14:50 +01:00
var _this = this;
var stopped = [];
2019-05-14 18:48:48 -04:00
if (eventName == null) {
2020-03-12 19:14:50 +01:00
stopped = this._events;
2019-05-14 18:48:48 -04:00
this._events = [];
}
else {
var eventTag_1 = getEventTag(eventName);
this._events = this._events.filter(function (event) {
2020-03-12 19:14:50 +01:00
if (event.tag !== eventTag_1) {
return true;
}
stopped.push(event);
return false;
2019-05-14 18:48:48 -04:00
});
}
2020-03-12 19:14:50 +01:00
stopped.forEach(function (event) { _this._stopEvent(event); });
2019-05-14 18:48:48 -04:00
return this;
};
return BaseProvider;
}(abstract_provider_1.Provider));
exports.BaseProvider = BaseProvider;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=base-provider.js.map