2019-08-25 09:39:20 +03:00
|
|
|
"use strict";
|
2019-11-20 12:57:38 +03: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());
|
|
|
|
});
|
|
|
|
};
|
2019-08-25 09:39:20 +03:00
|
|
|
import { ForkEvent, Provider } from "@ethersproject/abstract-provider";
|
2020-09-04 08:37:14 +03:00
|
|
|
import { Base58 } from "@ethersproject/basex";
|
2019-08-25 09:39:20 +03:00
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
2020-09-04 08:37:14 +03:00
|
|
|
import { arrayify, concat, hexConcat, hexDataLength, hexDataSlice, hexlify, hexValue, hexZeroPad, isHexString } from "@ethersproject/bytes";
|
|
|
|
import { HashZero } from "@ethersproject/constants";
|
2019-08-25 09:39:20 +03:00
|
|
|
import { namehash } from "@ethersproject/hash";
|
|
|
|
import { getNetwork } from "@ethersproject/networks";
|
|
|
|
import { defineReadOnly, getStatic, resolveProperties } from "@ethersproject/properties";
|
2020-09-04 08:37:14 +03:00
|
|
|
import { sha256 } from "@ethersproject/sha2";
|
|
|
|
import { toUtf8Bytes, toUtf8String } from "@ethersproject/strings";
|
2019-08-25 09:39:20 +03:00
|
|
|
import { poll } from "@ethersproject/web";
|
2020-11-17 07:07:24 +03:00
|
|
|
import bech32 from "bech32";
|
2019-08-25 09:39:20 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
import { Formatter } from "./formatter";
|
|
|
|
//////////////////////////////
|
|
|
|
// Event Serializeing
|
|
|
|
function checkTopic(topic) {
|
|
|
|
if (topic == null) {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
if (hexDataLength(topic) !== 32) {
|
|
|
|
logger.throwArgumentError("invalid topic", "topic", topic);
|
|
|
|
}
|
|
|
|
return topic.toLowerCase();
|
|
|
|
}
|
|
|
|
function serializeTopics(topics) {
|
|
|
|
// Remove trailing null AND-topics; they are redundant
|
|
|
|
topics = topics.slice();
|
2020-04-17 04:59:53 +03:00
|
|
|
while (topics.length > 0 && topics[topics.length - 1] == null) {
|
2019-08-25 09:39:20 +03:00
|
|
|
topics.pop();
|
|
|
|
}
|
|
|
|
return topics.map((topic) => {
|
|
|
|
if (Array.isArray(topic)) {
|
|
|
|
// Only track unique OR-topics
|
2019-11-20 12:57:38 +03:00
|
|
|
const unique = {};
|
2019-08-25 09:39:20 +03:00
|
|
|
topic.forEach((topic) => {
|
|
|
|
unique[checkTopic(topic)] = true;
|
|
|
|
});
|
|
|
|
// The order of OR-topics does not matter
|
2019-11-20 12:57:38 +03:00
|
|
|
const sorted = Object.keys(unique);
|
2019-08-25 09:39:20 +03:00
|
|
|
sorted.sort();
|
|
|
|
return sorted.join("|");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return checkTopic(topic);
|
|
|
|
}
|
|
|
|
}).join("&");
|
|
|
|
}
|
|
|
|
function deserializeTopics(data) {
|
2020-04-17 04:59:53 +03:00
|
|
|
if (data === "") {
|
|
|
|
return [];
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
return data.split(/&/g).map((topic) => {
|
2020-04-28 12:03:49 +03:00
|
|
|
if (topic === "") {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const comps = topic.split("|").map((topic) => {
|
2019-08-25 09:39:20 +03:00
|
|
|
return ((topic === "null") ? null : topic);
|
|
|
|
});
|
2020-04-28 12:03:49 +03:00
|
|
|
return ((comps.length === 1) ? comps[0] : comps);
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
function getEventTag(eventName) {
|
|
|
|
if (typeof (eventName) === "string") {
|
|
|
|
eventName = eventName.toLowerCase();
|
|
|
|
if (hexDataLength(eventName) === 32) {
|
|
|
|
return "tx:" + eventName;
|
|
|
|
}
|
|
|
|
if (eventName.indexOf(":") === -1) {
|
|
|
|
return eventName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Array.isArray(eventName)) {
|
|
|
|
return "filter:*:" + serializeTopics(eventName);
|
|
|
|
}
|
|
|
|
else if (ForkEvent.isForkEvent(eventName)) {
|
|
|
|
logger.warn("not implemented");
|
|
|
|
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 10:47:17 +03:00
|
|
|
function stall(duration) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, duration);
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
//////////////////////////////
|
|
|
|
// Provider Object
|
|
|
|
/**
|
|
|
|
* EventType
|
|
|
|
* - "block"
|
2020-05-05 06:01:04 +03:00
|
|
|
* - "poll"
|
2020-06-03 10:47:17 +03:00
|
|
|
* - "didPoll"
|
2019-08-25 09:39:20 +03:00
|
|
|
* - "pending"
|
|
|
|
* - "error"
|
2020-06-03 10:47:17 +03:00
|
|
|
* - "network"
|
2019-08-25 09:39:20 +03:00
|
|
|
* - filter
|
|
|
|
* - topics array
|
|
|
|
* - transaction hash
|
|
|
|
*/
|
2020-06-03 10:47:17 +03:00
|
|
|
const PollableEvents = ["block", "network", "pending", "poll"];
|
2020-03-12 21:14:50 +03:00
|
|
|
export class Event {
|
2019-08-25 09:39:20 +03:00
|
|
|
constructor(tag, listener, once) {
|
|
|
|
defineReadOnly(this, "tag", tag);
|
|
|
|
defineReadOnly(this, "listener", listener);
|
|
|
|
defineReadOnly(this, "once", once);
|
|
|
|
}
|
2020-04-16 01:28:04 +03:00
|
|
|
get event() {
|
|
|
|
switch (this.type) {
|
|
|
|
case "tx":
|
|
|
|
return this.hash;
|
|
|
|
case "filter":
|
|
|
|
return this.filter;
|
|
|
|
}
|
|
|
|
return this.tag;
|
|
|
|
}
|
2020-03-12 21:14:50 +03:00
|
|
|
get type() {
|
|
|
|
return this.tag.split(":")[0];
|
|
|
|
}
|
|
|
|
get hash() {
|
|
|
|
const comps = this.tag.split(":");
|
|
|
|
if (comps[0] !== "tx") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return comps[1];
|
|
|
|
}
|
|
|
|
get filter() {
|
|
|
|
const comps = this.tag.split(":");
|
|
|
|
if (comps[0] !== "filter") {
|
|
|
|
return null;
|
|
|
|
}
|
2020-04-17 04:59:53 +03:00
|
|
|
const address = comps[1];
|
|
|
|
const topics = deserializeTopics(comps[2]);
|
|
|
|
const filter = {};
|
|
|
|
if (topics.length > 0) {
|
|
|
|
filter.topics = topics;
|
|
|
|
}
|
|
|
|
if (address && address !== "*") {
|
|
|
|
filter.address = address;
|
2020-03-12 21:14:50 +03:00
|
|
|
}
|
|
|
|
return filter;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
pollable() {
|
2020-06-03 10:47:17 +03:00
|
|
|
return (this.tag.indexOf(":") >= 0 || PollableEvents.indexOf(this.tag) >= 0);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-04 08:37:14 +03:00
|
|
|
;
|
|
|
|
// https://github.com/satoshilabs/slips/blob/master/slip-0044.md
|
|
|
|
const 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) {
|
|
|
|
return hexZeroPad(BigNumber.from(value).toHexString(), 32);
|
|
|
|
}
|
|
|
|
// Compute the Base58Check encoded data (checksum is first 4 bytes of sha256d)
|
|
|
|
function base58Encode(data) {
|
|
|
|
return Base58.encode(concat([data, hexDataSlice(sha256(sha256(data)), 0, 4)]));
|
|
|
|
}
|
|
|
|
export class Resolver {
|
|
|
|
constructor(provider, address, name) {
|
|
|
|
defineReadOnly(this, "provider", provider);
|
|
|
|
defineReadOnly(this, "name", name);
|
|
|
|
defineReadOnly(this, "address", provider.formatter.address(address));
|
|
|
|
}
|
|
|
|
_fetchBytes(selector, parameters) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
// keccak256("addr(bytes32,uint256)")
|
|
|
|
const transaction = {
|
|
|
|
to: this.address,
|
|
|
|
data: hexConcat([selector, namehash(this.name), (parameters || "0x")])
|
|
|
|
};
|
|
|
|
const result = yield this.provider.call(transaction);
|
|
|
|
if (result === "0x") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const offset = BigNumber.from(hexDataSlice(result, 0, 32)).toNumber();
|
|
|
|
const length = BigNumber.from(hexDataSlice(result, offset, offset + 32)).toNumber();
|
|
|
|
return hexDataSlice(result, offset + 32, offset + 32 + length);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_getAddress(coinType, hexBytes) {
|
|
|
|
const coinInfo = coinInfos[String(coinType)];
|
|
|
|
if (coinInfo == null) {
|
|
|
|
logger.throwError(`unsupported coin type: ${coinType}`, Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: `getAddress(${coinType})`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (coinInfo.ilk === "eth") {
|
|
|
|
return this.provider.formatter.address(hexBytes);
|
|
|
|
}
|
|
|
|
const bytes = arrayify(hexBytes);
|
|
|
|
// P2PKH: OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
|
|
|
|
if (coinInfo.p2pkh != null) {
|
|
|
|
const p2pkh = hexBytes.match(/^0x76a9([0-9a-f][0-9a-f])([0-9a-f]*)88ac$/);
|
|
|
|
if (p2pkh) {
|
|
|
|
const length = parseInt(p2pkh[1], 16);
|
|
|
|
if (p2pkh[2].length === length * 2 && length >= 1 && length <= 75) {
|
|
|
|
return base58Encode(concat([[coinInfo.p2pkh], ("0x" + p2pkh[2])]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// P2SH: OP_HASH160 <scriptHash> OP_EQUAL
|
|
|
|
if (coinInfo.p2sh != null) {
|
|
|
|
const p2sh = hexBytes.match(/^0xa9([0-9a-f][0-9a-f])([0-9a-f]*)87$/);
|
|
|
|
if (p2sh) {
|
|
|
|
const length = parseInt(p2sh[1], 16);
|
|
|
|
if (p2sh[2].length === length * 2 && length >= 1 && length <= 75) {
|
|
|
|
return base58Encode(concat([[coinInfo.p2sh], ("0x" + p2sh[2])]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Bech32
|
|
|
|
if (coinInfo.prefix != null) {
|
|
|
|
const length = bytes[1];
|
|
|
|
// https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program
|
|
|
|
let version = bytes[0];
|
|
|
|
if (version === 0x00) {
|
|
|
|
if (length !== 20 && length !== 32) {
|
|
|
|
version = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
version = -1;
|
|
|
|
}
|
|
|
|
if (version >= 0 && bytes.length === 2 + length && length >= 1 && length <= 75) {
|
2020-11-17 07:07:24 +03:00
|
|
|
const words = bech32.toWords(bytes.slice(2));
|
2020-09-04 08:37:14 +03:00
|
|
|
words.unshift(version);
|
2020-11-17 07:07:24 +03:00
|
|
|
return bech32.encode(coinInfo.prefix, words);
|
2020-09-04 08:37:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
getAddress(coinType) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
if (coinType == null) {
|
|
|
|
coinType = 60;
|
|
|
|
}
|
|
|
|
// If Ethereum, use the standard `addr(bytes32)`
|
|
|
|
if (coinType === 60) {
|
|
|
|
// keccak256("addr(bytes32)")
|
|
|
|
const transaction = {
|
|
|
|
to: this.address,
|
|
|
|
data: ("0x3b3b57de" + namehash(this.name).substring(2))
|
|
|
|
};
|
|
|
|
const hexBytes = yield this.provider.call(transaction);
|
|
|
|
// No address
|
|
|
|
if (hexBytes === "0x" || hexBytes === HashZero) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this.provider.formatter.callAddress(hexBytes);
|
|
|
|
}
|
|
|
|
// keccak256("addr(bytes32,uint256")
|
|
|
|
const hexBytes = yield this._fetchBytes("0xf1cb7e06", bytes32ify(coinType));
|
|
|
|
// No address
|
|
|
|
if (hexBytes == null || hexBytes === "0x") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Compute the address
|
|
|
|
const address = this._getAddress(coinType, hexBytes);
|
|
|
|
if (address == null) {
|
|
|
|
logger.throwError(`invalid or unsupported coin data`, Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: `getAddress(${coinType})`,
|
|
|
|
coinType: coinType,
|
|
|
|
data: hexBytes
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
getContentHash() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
// keccak256("contenthash()")
|
|
|
|
const hexBytes = yield this._fetchBytes("0xbc1c58d1");
|
|
|
|
// No contenthash
|
|
|
|
if (hexBytes == null || hexBytes === "0x") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// IPFS (CID: 1, Type: DAG-PB)
|
|
|
|
const ipfs = hexBytes.match(/^0xe3010170(([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f]*))$/);
|
|
|
|
if (ipfs) {
|
|
|
|
const length = parseInt(ipfs[3], 16);
|
|
|
|
if (ipfs[4].length === length * 2) {
|
|
|
|
return "ipfs:/\/" + Base58.encode("0x" + ipfs[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Swarm (CID: 1, Type: swarm-manifest; hash/length hard-coded to keccak256/32)
|
|
|
|
const swarm = hexBytes.match(/^0xe40101fa011b20([0-9a-f]*)$/);
|
|
|
|
if (swarm) {
|
|
|
|
if (swarm[1].length === (32 * 2)) {
|
|
|
|
return "bzz:/\/" + swarm[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return logger.throwError(`invalid or unsupported content hash data`, Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "getContentHash()",
|
|
|
|
data: hexBytes
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
getText(key) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
// The key encoded as parameter to fetchBytes
|
|
|
|
let keyBytes = toUtf8Bytes(key);
|
|
|
|
// 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
|
|
|
|
keyBytes = concat([bytes32ify(64), bytes32ify(keyBytes.length), keyBytes]);
|
|
|
|
// Pad to word-size (32 bytes)
|
|
|
|
if ((keyBytes.length % 32) !== 0) {
|
|
|
|
keyBytes = concat([keyBytes, hexZeroPad("0x", 32 - (key.length % 32))]);
|
|
|
|
}
|
|
|
|
const hexBytes = yield this._fetchBytes("0x59d1d43c", hexlify(keyBytes));
|
|
|
|
if (hexBytes == null || hexBytes === "0x") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return toUtf8String(hexBytes);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let defaultFormatter = null;
|
|
|
|
let nextPollId = 1;
|
|
|
|
export class BaseProvider extends Provider {
|
2020-05-04 00:53:58 +03: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-08-25 09:39:20 +03:00
|
|
|
constructor(network) {
|
|
|
|
logger.checkNew(new.target, Provider);
|
|
|
|
super();
|
2020-06-03 10:47:17 +03:00
|
|
|
// Events being listened to
|
|
|
|
this._events = [];
|
|
|
|
this._emitted = { block: -2 };
|
2019-08-25 09:39:20 +03:00
|
|
|
this.formatter = new.target.getFormatter();
|
2020-06-03 10:47:17 +03:00
|
|
|
// If network is any, this Provider allows the underlying
|
|
|
|
// network to change dynamically, and we auto-detect the
|
|
|
|
// current network
|
|
|
|
defineReadOnly(this, "anyNetwork", (network === "any"));
|
|
|
|
if (this.anyNetwork) {
|
|
|
|
network = this.detectNetwork();
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (network instanceof Promise) {
|
2020-05-04 00:53:58 +03:00
|
|
|
this._networkPromise = network;
|
2019-08-25 09:39:20 +03:00
|
|
|
// Squash any "unhandled promise" errors; that do not need to be handled
|
2020-05-04 00:53:58 +03:00
|
|
|
network.catch((error) => { });
|
2020-06-03 10:47:17 +03:00
|
|
|
// Trigger initial network setting (async)
|
2020-06-14 04:39:36 +03:00
|
|
|
this._ready().catch((error) => { });
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-11-20 12:57:38 +03:00
|
|
|
const knownNetwork = getStatic((new.target), "getNetwork")(network);
|
2019-08-25 09:39:20 +03:00
|
|
|
if (knownNetwork) {
|
|
|
|
defineReadOnly(this, "_network", knownNetwork);
|
2020-06-03 10:47:17 +03:00
|
|
|
this.emit("network", knownNetwork, null);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.throwArgumentError("invalid network", "network", network);
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
this._maxInternalBlockNumber = -1024;
|
2019-08-25 09:39:20 +03:00
|
|
|
this._lastBlockNumber = -2;
|
|
|
|
this._pollingInterval = 4000;
|
|
|
|
this._fastQueryDate = 0;
|
|
|
|
}
|
2020-05-04 00:53:58 +03:00
|
|
|
_ready() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
if (this._network == null) {
|
|
|
|
let network = null;
|
|
|
|
if (this._networkPromise) {
|
|
|
|
try {
|
|
|
|
network = yield this._networkPromise;
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
|
|
|
// Try the Provider's network detection (this MUST throw if it cannot)
|
|
|
|
if (network == null) {
|
|
|
|
network = yield this.detectNetwork();
|
|
|
|
}
|
|
|
|
// This should never happen; every Provider sub-class should have
|
2020-05-30 04:27:59 +03:00
|
|
|
// suggested a network by here (or have thrown).
|
2020-05-04 00:53:58 +03:00
|
|
|
if (!network) {
|
|
|
|
logger.throwError("no network detected", Logger.errors.UNKNOWN_ERROR, {});
|
|
|
|
}
|
2020-05-30 04:27:59 +03:00
|
|
|
// Possible this call stacked so do not call defineReadOnly again
|
|
|
|
if (this._network == null) {
|
2020-06-03 10:47:17 +03:00
|
|
|
if (this.anyNetwork) {
|
|
|
|
this._network = network;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
defineReadOnly(this, "_network", network);
|
|
|
|
}
|
|
|
|
this.emit("network", network, null);
|
2020-05-30 04:27:59 +03:00
|
|
|
}
|
2020-05-04 00:53:58 +03:00
|
|
|
}
|
|
|
|
return this._network;
|
|
|
|
});
|
|
|
|
}
|
2020-06-03 10:47:17 +03:00
|
|
|
// This will always return the most recently established network.
|
|
|
|
// For "any", this can change (a "network" event is emitted before
|
|
|
|
// any change is refelcted); otherwise this cannot change
|
2020-05-04 00:53:58 +03:00
|
|
|
get ready() {
|
2020-06-14 04:39:36 +03:00
|
|
|
return poll(() => {
|
|
|
|
return this._ready().then((network) => {
|
|
|
|
return network;
|
|
|
|
}, (error) => {
|
|
|
|
// If the network isn't running yet, we will wait
|
|
|
|
if (error.code === Logger.errors.NETWORK_ERROR && error.event === "noNetwork") {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
});
|
2020-05-04 00:53:58 +03:00
|
|
|
}
|
2020-06-03 10:47:17 +03:00
|
|
|
// @TODO: Remove this and just create a singleton formatter
|
2019-08-25 09:39:20 +03:00
|
|
|
static getFormatter() {
|
|
|
|
if (defaultFormatter == null) {
|
|
|
|
defaultFormatter = new Formatter();
|
|
|
|
}
|
|
|
|
return defaultFormatter;
|
|
|
|
}
|
2020-06-03 10:47:17 +03:00
|
|
|
// @TODO: Remove this and just use getNetwork
|
2019-08-25 09:39:20 +03:00
|
|
|
static getNetwork(network) {
|
|
|
|
return getNetwork((network == null) ? "homestead" : network);
|
|
|
|
}
|
2020-06-03 10:47:17 +03: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 12:57:38 +03:00
|
|
|
_getInternalBlockNumber(maxAge) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-14 04:39:36 +03:00
|
|
|
yield this._ready();
|
2019-11-20 12:57:38 +03:00
|
|
|
const internalBlockNumber = this._internalBlockNumber;
|
2021-01-13 11:41:29 +03:00
|
|
|
if (maxAge > 0 && internalBlockNumber) {
|
|
|
|
try {
|
|
|
|
const result = yield internalBlockNumber;
|
|
|
|
if ((getTime() - result.respTime) <= maxAge) {
|
|
|
|
return result.blockNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
// Don't null the dead (rejected) fetch, if it has already been updated
|
|
|
|
if (this._internalBlockNumber === internalBlockNumber) {
|
|
|
|
this._internalBlockNumber = null;
|
|
|
|
}
|
|
|
|
throw error;
|
2019-11-20 12:57:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const reqTime = getTime();
|
2020-06-03 10:47:17 +03:00
|
|
|
const checkInternalBlockNumber = resolveProperties({
|
|
|
|
blockNumber: this.perform("getBlockNumber", {}),
|
|
|
|
networkError: this.getNetwork().then((network) => (null), (error) => (error))
|
|
|
|
}).then(({ blockNumber, networkError }) => {
|
|
|
|
if (networkError) {
|
|
|
|
// Unremember this bad internal block number
|
|
|
|
if (this._internalBlockNumber === checkInternalBlockNumber) {
|
|
|
|
this._internalBlockNumber = null;
|
|
|
|
}
|
|
|
|
throw networkError;
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
const respTime = getTime();
|
|
|
|
blockNumber = BigNumber.from(blockNumber).toNumber();
|
|
|
|
if (blockNumber < this._maxInternalBlockNumber) {
|
|
|
|
blockNumber = this._maxInternalBlockNumber;
|
|
|
|
}
|
|
|
|
this._maxInternalBlockNumber = blockNumber;
|
|
|
|
this._setFastBlockNumber(blockNumber); // @TODO: Still need this?
|
|
|
|
return { blockNumber, reqTime, respTime };
|
|
|
|
});
|
2020-06-03 10:47:17 +03:00
|
|
|
this._internalBlockNumber = checkInternalBlockNumber;
|
2021-01-13 11:41:29 +03:00
|
|
|
// Swallow unhandled exceptions; if needed they are handled else where
|
|
|
|
checkInternalBlockNumber.catch((error) => {
|
|
|
|
// Don't null the dead (rejected) fetch, if it has already been updated
|
|
|
|
if (this._internalBlockNumber === checkInternalBlockNumber) {
|
|
|
|
this._internalBlockNumber = null;
|
|
|
|
}
|
|
|
|
});
|
2020-06-03 10:47:17 +03:00
|
|
|
return (yield checkInternalBlockNumber).blockNumber;
|
2019-11-20 12:57:38 +03:00
|
|
|
});
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
poll() {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const pollId = nextPollId++;
|
|
|
|
// Track all running promises, so we can trigger a post-poll once they are complete
|
|
|
|
const runners = [];
|
2021-01-13 11:41:29 +03:00
|
|
|
let blockNumber = null;
|
|
|
|
try {
|
|
|
|
blockNumber = yield this._getInternalBlockNumber(100 + this.pollingInterval / 2);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
this.emit("error", error);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
this._setFastBlockNumber(blockNumber);
|
2020-05-05 06:01:04 +03:00
|
|
|
// Emit a poll event after we have the latest (fast) block number
|
|
|
|
this.emit("poll", pollId, blockNumber);
|
2019-08-25 09:39:20 +03:00
|
|
|
// If the block has not changed, meh.
|
|
|
|
if (blockNumber === this._lastBlockNumber) {
|
2020-05-05 06:01:04 +03:00
|
|
|
this.emit("didPoll", pollId);
|
2019-08-25 09:39:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// First polling cycle, trigger a "block" events
|
|
|
|
if (this._emitted.block === -2) {
|
|
|
|
this._emitted.block = blockNumber - 1;
|
|
|
|
}
|
2020-06-03 10:47:17 +03:00
|
|
|
if (Math.abs((this._emitted.block) - blockNumber) > 1000) {
|
|
|
|
logger.warn("network block skew detected; skipping block events");
|
|
|
|
this.emit("error", logger.makeError("network block skew detected", Logger.errors.NETWORK_ERROR, {
|
|
|
|
blockNumber: blockNumber,
|
2020-06-14 04:39:36 +03:00
|
|
|
event: "blockSkew",
|
2020-06-03 10:47:17 +03:00
|
|
|
previousBlockNumber: this._emitted.block
|
|
|
|
}));
|
|
|
|
this.emit("block", blockNumber);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Notify all listener for each block that has passed
|
|
|
|
for (let i = this._emitted.block + 1; i <= blockNumber; i++) {
|
|
|
|
this.emit("block", i);
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
// The emitted block was updated, check for obsolete events
|
|
|
|
if (this._emitted.block !== blockNumber) {
|
|
|
|
this._emitted.block = blockNumber;
|
|
|
|
Object.keys(this._emitted).forEach((key) => {
|
|
|
|
// The block event does not expire
|
|
|
|
if (key === "block") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The block we were at when we emitted this event
|
2019-11-20 12:57:38 +03:00
|
|
|
const eventBlockNumber = this._emitted[key];
|
2019-08-25 09:39:20 +03:00
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// First polling cycle
|
|
|
|
if (this._lastBlockNumber === -2) {
|
|
|
|
this._lastBlockNumber = blockNumber - 1;
|
|
|
|
}
|
|
|
|
// Find all transaction hashes we are waiting on
|
|
|
|
this._events.forEach((event) => {
|
2020-03-12 21:14:50 +03:00
|
|
|
switch (event.type) {
|
2019-08-25 09:39:20 +03:00
|
|
|
case "tx": {
|
2020-03-12 21:14:50 +03:00
|
|
|
const hash = event.hash;
|
2019-08-25 09:39:20 +03:00
|
|
|
let runner = this.getTransactionReceipt(hash).then((receipt) => {
|
|
|
|
if (!receipt || receipt.blockNumber == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
this._emitted["t:" + hash] = receipt.blockNumber;
|
|
|
|
this.emit(hash, receipt);
|
|
|
|
return null;
|
|
|
|
}).catch((error) => { this.emit("error", error); });
|
|
|
|
runners.push(runner);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "filter": {
|
2020-03-12 21:14:50 +03:00
|
|
|
const filter = event.filter;
|
|
|
|
filter.fromBlock = this._lastBlockNumber + 1;
|
|
|
|
filter.toBlock = blockNumber;
|
2019-11-20 12:57:38 +03:00
|
|
|
const runner = this.getLogs(filter).then((logs) => {
|
2019-08-25 09:39:20 +03:00
|
|
|
if (logs.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logs.forEach((log) => {
|
|
|
|
this._emitted["b:" + log.blockHash] = log.blockNumber;
|
|
|
|
this._emitted["t:" + log.transactionHash] = log.blockNumber;
|
|
|
|
this.emit(filter, log);
|
|
|
|
});
|
|
|
|
}).catch((error) => { this.emit("error", error); });
|
|
|
|
runners.push(runner);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this._lastBlockNumber = blockNumber;
|
2020-06-03 10:47:17 +03:00
|
|
|
// Once all events for this loop have been processed, emit "didPoll"
|
2019-11-20 12:57:38 +03:00
|
|
|
Promise.all(runners).then(() => {
|
|
|
|
this.emit("didPoll", pollId);
|
2021-01-13 11:41:29 +03:00
|
|
|
}).catch((error) => { this.emit("error", error); });
|
|
|
|
return;
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
2020-06-03 10:47:17 +03:00
|
|
|
// Deprecated; do not use this
|
2019-08-25 09:39:20 +03:00
|
|
|
resetEventsBlock(blockNumber) {
|
|
|
|
this._lastBlockNumber = blockNumber - 1;
|
|
|
|
if (this.polling) {
|
|
|
|
this.poll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
get network() {
|
|
|
|
return this._network;
|
|
|
|
}
|
2020-06-03 10:47:17 +03:00
|
|
|
// This method should query the network if the underlying network
|
|
|
|
// can change, such as when connected to a JSON-RPC backend
|
|
|
|
detectNetwork() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
return logger.throwError("provider does not support network detection", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "provider.detectNetwork"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
getNetwork() {
|
2020-06-03 10:47:17 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-14 04:39:36 +03:00
|
|
|
const network = yield this._ready();
|
2020-06-03 10:47:17 +03:00
|
|
|
// Make sure we are still connected to the same network; this is
|
|
|
|
// only an external call for backends which can have the underlying
|
|
|
|
// network change spontaneously
|
|
|
|
const currentNetwork = yield this.detectNetwork();
|
|
|
|
if (network.chainId !== currentNetwork.chainId) {
|
|
|
|
// We are allowing network changes, things can get complex fast;
|
|
|
|
// make sure you know what you are doing if you use "any"
|
|
|
|
if (this.anyNetwork) {
|
|
|
|
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);
|
|
|
|
yield stall(0);
|
|
|
|
return this._network;
|
|
|
|
}
|
|
|
|
const error = logger.makeError("underlying network changed", Logger.errors.NETWORK_ERROR, {
|
|
|
|
event: "changed",
|
|
|
|
network: network,
|
|
|
|
detectedNetwork: currentNetwork
|
|
|
|
});
|
|
|
|
this.emit("error", error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return network;
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
get blockNumber() {
|
2020-05-05 06:01:04 +03:00
|
|
|
this._getInternalBlockNumber(100 + this.pollingInterval / 2).then((blockNumber) => {
|
|
|
|
this._setFastBlockNumber(blockNumber);
|
2021-01-13 11:41:29 +03:00
|
|
|
}, (error) => { });
|
2020-05-05 06:01:04 +03:00
|
|
|
return (this._fastBlockNumber != null) ? this._fastBlockNumber : -1;
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
get polling() {
|
|
|
|
return (this._poller != null);
|
|
|
|
}
|
|
|
|
set polling(value) {
|
2020-05-05 06:01:04 +03:00
|
|
|
if (value && !this._poller) {
|
2021-01-13 11:41:29 +03:00
|
|
|
this._poller = setInterval(() => { this.poll(); }, this.pollingInterval);
|
2020-05-05 06:01:04 +03:00
|
|
|
if (!this._bootstrapPoll) {
|
|
|
|
this._bootstrapPoll = setTimeout(() => {
|
|
|
|
this.poll();
|
|
|
|
// We block additional polls until the polling interval
|
|
|
|
// is done, to prevent overwhelming the poll function
|
|
|
|
this._bootstrapPoll = setTimeout(() => {
|
|
|
|
// 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-08-25 09:39:20 +03:00
|
|
|
}
|
2020-05-05 06:01:04 +03:00
|
|
|
}
|
|
|
|
else if (!value && this._poller) {
|
|
|
|
clearInterval(this._poller);
|
|
|
|
this._poller = null;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
get pollingInterval() {
|
|
|
|
return this._pollingInterval;
|
|
|
|
}
|
|
|
|
set pollingInterval(value) {
|
|
|
|
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(() => { this.poll(); }, this._pollingInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_getFastBlockNumber() {
|
2019-11-20 12:57:38 +03:00
|
|
|
const now = getTime();
|
2019-08-25 09:39:20 +03:00
|
|
|
// Stale block number, request a newer value
|
|
|
|
if ((now - this._fastQueryDate) > 2 * this._pollingInterval) {
|
|
|
|
this._fastQueryDate = now;
|
|
|
|
this._fastBlockNumberPromise = this.getBlockNumber().then((blockNumber) => {
|
|
|
|
if (this._fastBlockNumber == null || blockNumber > this._fastBlockNumber) {
|
|
|
|
this._fastBlockNumber = blockNumber;
|
|
|
|
}
|
|
|
|
return this._fastBlockNumber;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._fastBlockNumberPromise;
|
|
|
|
}
|
|
|
|
_setFastBlockNumber(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 09:06:47 +03:00
|
|
|
waitForTransaction(transactionHash, confirmations, timeout) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
if (confirmations == null) {
|
|
|
|
confirmations = 1;
|
|
|
|
}
|
|
|
|
const receipt = yield this.getTransactionReceipt(transactionHash);
|
|
|
|
// Receipt is already good
|
2020-02-04 09:06:47 +03:00
|
|
|
if ((receipt ? receipt.confirmations : 0) >= confirmations) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return receipt;
|
|
|
|
}
|
|
|
|
// Poll until the receipt is good...
|
2020-02-04 09:06:47 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let timer = null;
|
|
|
|
let done = false;
|
2019-11-20 12:57:38 +03:00
|
|
|
const handler = (receipt) => {
|
|
|
|
if (receipt.confirmations < confirmations) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-04 09:06:47 +03:00
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
if (done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
done = true;
|
2019-11-20 12:57:38 +03:00
|
|
|
this.removeListener(transactionHash, handler);
|
|
|
|
resolve(receipt);
|
|
|
|
};
|
|
|
|
this.on(transactionHash, handler);
|
2020-02-04 09:06:47 +03:00
|
|
|
if (typeof (timeout) === "number" && timeout > 0) {
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
if (done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timer = null;
|
|
|
|
done = true;
|
|
|
|
this.removeListener(transactionHash, handler);
|
|
|
|
reject(logger.makeError("timeout exceeded", Logger.errors.TIMEOUT, { timeout: timeout }));
|
|
|
|
}, timeout);
|
|
|
|
if (timer.unref) {
|
|
|
|
timer.unref();
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
getBlockNumber() {
|
2020-06-03 10:47:17 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
return this._getInternalBlockNumber(0);
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
getGasPrice() {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
return BigNumber.from(yield this.perform("getGasPrice", {}));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
getBalance(addressOrName, blockTag) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
const params = yield resolveProperties({
|
|
|
|
address: this._getAddress(addressOrName),
|
|
|
|
blockTag: this._getBlockTag(blockTag)
|
|
|
|
});
|
|
|
|
return BigNumber.from(yield this.perform("getBalance", params));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
getTransactionCount(addressOrName, blockTag) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
const params = yield resolveProperties({
|
|
|
|
address: this._getAddress(addressOrName),
|
|
|
|
blockTag: this._getBlockTag(blockTag)
|
|
|
|
});
|
|
|
|
return BigNumber.from(yield this.perform("getTransactionCount", params)).toNumber();
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
getCode(addressOrName, blockTag) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
const params = yield resolveProperties({
|
|
|
|
address: this._getAddress(addressOrName),
|
|
|
|
blockTag: this._getBlockTag(blockTag)
|
|
|
|
});
|
|
|
|
return hexlify(yield this.perform("getCode", params));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
getStorageAt(addressOrName, position, blockTag) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
const params = yield resolveProperties({
|
|
|
|
address: this._getAddress(addressOrName),
|
|
|
|
blockTag: this._getBlockTag(blockTag),
|
|
|
|
position: Promise.resolve(position).then((p) => hexValue(p))
|
|
|
|
});
|
|
|
|
return hexlify(yield this.perform("getStorageAt", params));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
// This should be called by any subclass wrapping a TransactionResponse
|
|
|
|
_wrapTransaction(tx, hash) {
|
|
|
|
if (hash != null && hexDataLength(hash) !== 32) {
|
|
|
|
throw new Error("invalid response - sendTransaction");
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
const result = tx;
|
2019-08-25 09:39:20 +03:00
|
|
|
// Check the hash we expect is the same as the hash the server reported
|
|
|
|
if (hash != null && tx.hash !== hash) {
|
|
|
|
logger.throwError("Transaction hash mismatch from Provider.sendTransaction.", Logger.errors.UNKNOWN_ERROR, { expectedHash: tx.hash, returnedHash: hash });
|
|
|
|
}
|
|
|
|
// @TODO: (confirmations? number, timeout? number)
|
2019-11-20 12:57:38 +03:00
|
|
|
result.wait = (confirmations) => __awaiter(this, void 0, void 0, function* () {
|
2019-08-25 09:39:20 +03:00
|
|
|
// We know this transaction *must* exist (whether it gets mined is
|
|
|
|
// another story), so setting an emitted value forces us to
|
|
|
|
// wait even if the node returns null for the receipt
|
|
|
|
if (confirmations !== 0) {
|
|
|
|
this._emitted["t:" + tx.hash] = "pending";
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
const receipt = yield this.waitForTransaction(tx.hash, confirmations);
|
|
|
|
if (receipt == null && confirmations === 0) {
|
|
|
|
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.errors.CALL_EXCEPTION, {
|
|
|
|
transactionHash: tx.hash,
|
|
|
|
transaction: tx,
|
|
|
|
receipt: receipt
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return receipt;
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
sendTransaction(signedTransaction) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
const hexTx = yield Promise.resolve(signedTransaction).then(t => hexlify(t));
|
|
|
|
const tx = this.formatter.transaction(signedTransaction);
|
|
|
|
try {
|
|
|
|
const hash = yield this.perform("sendTransaction", { signedTransaction: hexTx });
|
|
|
|
return this._wrapTransaction(tx, hash);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
error.transaction = tx;
|
|
|
|
error.transactionHash = tx.hash;
|
|
|
|
throw error;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
_getTransactionRequest(transaction) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const values = yield transaction;
|
|
|
|
const tx = {};
|
2019-08-25 09:39:20 +03:00
|
|
|
["from", "to"].forEach((key) => {
|
2019-11-20 12:57:38 +03:00
|
|
|
if (values[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tx[key] = Promise.resolve(values[key]).then((v) => (v ? this._getAddress(v) : null));
|
|
|
|
});
|
|
|
|
["gasLimit", "gasPrice", "value"].forEach((key) => {
|
|
|
|
if (values[key] == null) {
|
2019-08-25 09:39:20 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
tx[key] = Promise.resolve(values[key]).then((v) => (v ? BigNumber.from(v) : null));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
2019-11-20 12:57:38 +03:00
|
|
|
["data"].forEach((key) => {
|
|
|
|
if (values[key] == null) {
|
2019-08-25 09:39:20 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
tx[key] = Promise.resolve(values[key]).then((v) => (v ? hexlify(v) : null));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
2019-11-20 12:57:38 +03:00
|
|
|
return this.formatter.transactionRequest(yield resolveProperties(tx));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
_getFilter(filter) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
filter = yield filter;
|
2019-11-20 12:57:38 +03:00
|
|
|
const result = {};
|
|
|
|
if (filter.address != null) {
|
|
|
|
result.address = this._getAddress(filter.address);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
["blockHash", "topics"].forEach((key) => {
|
|
|
|
if (filter[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result[key] = filter[key];
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
["fromBlock", "toBlock"].forEach((key) => {
|
2019-11-20 12:57:38 +03:00
|
|
|
if (filter[key] == null) {
|
2019-08-25 09:39:20 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
result[key] = this._getBlockTag(filter[key]);
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
2020-04-24 06:35:39 +03:00
|
|
|
return this.formatter.filter(yield resolveProperties(result));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
call(transaction, blockTag) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
const params = yield resolveProperties({
|
|
|
|
transaction: this._getTransactionRequest(transaction),
|
|
|
|
blockTag: this._getBlockTag(blockTag)
|
|
|
|
});
|
|
|
|
return hexlify(yield this.perform("call", params));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
estimateGas(transaction) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
const params = yield resolveProperties({
|
|
|
|
transaction: this._getTransactionRequest(transaction)
|
|
|
|
});
|
|
|
|
return BigNumber.from(yield this.perform("estimateGas", params));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
_getAddress(addressOrName) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const address = yield this.resolveName(addressOrName);
|
2019-08-25 09:39:20 +03:00
|
|
|
if (address == null) {
|
|
|
|
logger.throwError("ENS name not configured", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: `resolveName(${JSON.stringify(addressOrName)})`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_getBlock(blockHashOrBlockTag, includeTransactions) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
|
|
|
blockHashOrBlockTag = yield blockHashOrBlockTag;
|
2019-11-20 12:57:38 +03:00
|
|
|
// If blockTag is a number (not "latest", etc), this is the block number
|
|
|
|
let blockNumber = -128;
|
|
|
|
const params = {
|
|
|
|
includeTransactions: !!includeTransactions
|
|
|
|
};
|
2019-10-16 19:12:03 +03:00
|
|
|
if (isHexString(blockHashOrBlockTag, 32)) {
|
2019-11-20 12:57:38 +03:00
|
|
|
params.blockHash = blockHashOrBlockTag;
|
2019-10-16 19:12:03 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-11-20 12:57:38 +03:00
|
|
|
try {
|
|
|
|
params.blockTag = this.formatter.blockTag(yield this._getBlockTag(blockHashOrBlockTag));
|
|
|
|
if (isHexString(params.blockTag)) {
|
|
|
|
blockNumber = parseInt(params.blockTag.substring(2), 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
logger.throwArgumentError("invalid block hash or block tag", "blockHashOrBlockTag", blockHashOrBlockTag);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
}
|
|
|
|
return poll(() => __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const block = yield this.perform("getBlock", params);
|
|
|
|
// 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 null;
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
// For block tags, if we are asking for a future block, we return null
|
|
|
|
if (params.blockTag != null) {
|
|
|
|
if (blockNumber > this._emitted.block) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
// Retry on the next block
|
|
|
|
return undefined;
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
// Add transactions
|
|
|
|
if (includeTransactions) {
|
2020-01-19 05:48:12 +03:00
|
|
|
let blockNumber = null;
|
|
|
|
for (let i = 0; i < block.transactions.length; i++) {
|
|
|
|
const tx = block.transactions[i];
|
|
|
|
if (tx.blockNumber == null) {
|
|
|
|
tx.confirmations = 0;
|
|
|
|
}
|
|
|
|
else if (tx.confirmations == null) {
|
|
|
|
if (blockNumber == null) {
|
|
|
|
blockNumber = yield this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
|
|
|
|
}
|
|
|
|
// Add the confirmations using the fast block number (pessimistic)
|
|
|
|
let confirmations = (blockNumber - tx.blockNumber) + 1;
|
|
|
|
if (confirmations <= 0) {
|
|
|
|
confirmations = 1;
|
|
|
|
}
|
|
|
|
tx.confirmations = confirmations;
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
return this.formatter.blockWithTransactions(block);
|
|
|
|
}
|
|
|
|
return this.formatter.block(block);
|
2020-05-30 04:27:59 +03:00
|
|
|
}), { oncePoll: this });
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
getBlock(blockHashOrBlockTag) {
|
|
|
|
return (this._getBlock(blockHashOrBlockTag, false));
|
|
|
|
}
|
|
|
|
getBlockWithTransactions(blockHashOrBlockTag) {
|
|
|
|
return (this._getBlock(blockHashOrBlockTag, true));
|
|
|
|
}
|
|
|
|
getTransaction(transactionHash) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
|
|
|
transactionHash = yield transactionHash;
|
2019-11-20 12:57:38 +03:00
|
|
|
const params = { transactionHash: this.formatter.hash(transactionHash, true) };
|
|
|
|
return poll(() => __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const result = yield this.perform("getTransaction", params);
|
|
|
|
if (result == null) {
|
|
|
|
if (this._emitted["t:" + transactionHash] == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const tx = this.formatter.transactionResponse(result);
|
|
|
|
if (tx.blockNumber == null) {
|
|
|
|
tx.confirmations = 0;
|
|
|
|
}
|
|
|
|
else if (tx.confirmations == null) {
|
|
|
|
const blockNumber = yield this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
|
|
|
|
// Add the confirmations using the fast block number (pessimistic)
|
|
|
|
let confirmations = (blockNumber - tx.blockNumber) + 1;
|
|
|
|
if (confirmations <= 0) {
|
|
|
|
confirmations = 1;
|
|
|
|
}
|
|
|
|
tx.confirmations = confirmations;
|
|
|
|
}
|
|
|
|
return this._wrapTransaction(tx);
|
2020-05-30 04:27:59 +03:00
|
|
|
}), { oncePoll: this });
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
getTransactionReceipt(transactionHash) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
|
|
|
transactionHash = yield transactionHash;
|
2019-11-20 12:57:38 +03:00
|
|
|
const params = { transactionHash: this.formatter.hash(transactionHash, true) };
|
|
|
|
return poll(() => __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const result = yield this.perform("getTransactionReceipt", params);
|
|
|
|
if (result == null) {
|
|
|
|
if (this._emitted["t:" + transactionHash] == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// "geth-etc" returns receipts before they are ready
|
|
|
|
if (result.blockHash == null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const receipt = this.formatter.receipt(result);
|
|
|
|
if (receipt.blockNumber == null) {
|
|
|
|
receipt.confirmations = 0;
|
|
|
|
}
|
|
|
|
else if (receipt.confirmations == null) {
|
|
|
|
const blockNumber = yield this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
|
|
|
|
// Add the confirmations using the fast block number (pessimistic)
|
|
|
|
let confirmations = (blockNumber - receipt.blockNumber) + 1;
|
|
|
|
if (confirmations <= 0) {
|
|
|
|
confirmations = 1;
|
|
|
|
}
|
|
|
|
receipt.confirmations = confirmations;
|
|
|
|
}
|
|
|
|
return receipt;
|
2020-05-30 04:27:59 +03:00
|
|
|
}), { oncePoll: this });
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
getLogs(filter) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
const params = yield resolveProperties({ filter: this._getFilter(filter) });
|
|
|
|
const logs = yield this.perform("getLogs", params);
|
2020-01-19 05:48:12 +03:00
|
|
|
logs.forEach((log) => {
|
|
|
|
if (log.removed == null) {
|
|
|
|
log.removed = false;
|
|
|
|
}
|
|
|
|
});
|
2019-11-20 12:57:38 +03:00
|
|
|
return Formatter.arrayOf(this.formatter.filterLog.bind(this.formatter))(logs);
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
getEtherPrice() {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
yield this.getNetwork();
|
2019-11-20 12:57:38 +03:00
|
|
|
return this.perform("getEtherPrice", {});
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
_getBlockTag(blockTag) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
blockTag = yield blockTag;
|
2019-11-20 12:57:38 +03:00
|
|
|
if (typeof (blockTag) === "number" && blockTag < 0) {
|
|
|
|
if (blockTag % 1) {
|
|
|
|
logger.throwArgumentError("invalid BlockTag", "blockTag", blockTag);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
let blockNumber = yield this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
|
|
|
|
blockNumber += blockTag;
|
|
|
|
if (blockNumber < 0) {
|
|
|
|
blockNumber = 0;
|
|
|
|
}
|
|
|
|
return this.formatter.blockTag(blockNumber);
|
|
|
|
}
|
|
|
|
return this.formatter.blockTag(blockTag);
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2020-09-04 08:37:14 +03:00
|
|
|
getResolver(name) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const address = yield this._getResolver(name);
|
|
|
|
if (address == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new Resolver(this, address, name);
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
_getResolver(name) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
// Get the resolver from the blockchain
|
|
|
|
const network = yield this.getNetwork();
|
2019-08-25 09:39:20 +03:00
|
|
|
// No ENS...
|
|
|
|
if (!network.ensAddress) {
|
2019-09-08 09:46:53 +03:00
|
|
|
logger.throwError("network does not support ENS", Logger.errors.UNSUPPORTED_OPERATION, { operation: "ENS", network: network.name });
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
// keccak256("resolver(bytes32)")
|
2019-11-20 12:57:38 +03:00
|
|
|
const transaction = {
|
|
|
|
to: network.ensAddress,
|
|
|
|
data: ("0x0178b8bf" + namehash(name).substring(2))
|
|
|
|
};
|
|
|
|
return this.formatter.callAddress(yield this.call(transaction));
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
resolveName(name) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
name = yield name;
|
2019-11-20 12:57:38 +03:00
|
|
|
// If it is already an address, nothing to resolve
|
|
|
|
try {
|
|
|
|
return Promise.resolve(this.formatter.address(name));
|
|
|
|
}
|
2020-01-08 03:58:04 +03:00
|
|
|
catch (error) {
|
|
|
|
// If is is a hexstring, the address is bad (See #694)
|
|
|
|
if (isHexString(name)) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2020-04-04 05:13:06 +03:00
|
|
|
if (typeof (name) !== "string") {
|
|
|
|
logger.throwArgumentError("invalid ENS name", "name", name);
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
// Get the addr from the resovler
|
2020-09-04 08:37:14 +03:00
|
|
|
const resolver = yield this.getResolver(name);
|
|
|
|
if (!resolver) {
|
2019-08-25 09:39:20 +03:00
|
|
|
return null;
|
|
|
|
}
|
2020-09-04 08:37:14 +03:00
|
|
|
return yield resolver.getAddress();
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
lookupAddress(address) {
|
2019-11-20 12:57:38 +03:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-03 10:47:17 +03:00
|
|
|
address = yield address;
|
2019-11-20 12:57:38 +03:00
|
|
|
address = this.formatter.address(address);
|
|
|
|
const reverseName = address.substring(2).toLowerCase() + ".addr.reverse";
|
|
|
|
const resolverAddress = yield this._getResolver(reverseName);
|
2019-08-25 09:39:20 +03:00
|
|
|
if (!resolverAddress) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// keccak("name(bytes32)")
|
2019-11-20 12:57:38 +03:00
|
|
|
let bytes = arrayify(yield this.call({
|
|
|
|
to: resolverAddress,
|
|
|
|
data: ("0x691f3431" + namehash(reverseName).substring(2))
|
|
|
|
}));
|
|
|
|
// Strip off the dynamic string pointer (0x20)
|
|
|
|
if (bytes.length < 32 || !BigNumber.from(bytes.slice(0, 32)).eq(32)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
bytes = bytes.slice(32);
|
|
|
|
// Not a length-prefixed string
|
|
|
|
if (bytes.length < 32) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Get the length of the string (from the length-prefix)
|
|
|
|
const length = BigNumber.from(bytes.slice(0, 32)).toNumber();
|
|
|
|
bytes = bytes.slice(32);
|
|
|
|
// Length longer than available data
|
|
|
|
if (length > bytes.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const name = toUtf8String(bytes.slice(0, length));
|
|
|
|
// Make sure the reverse record matches the foward record
|
|
|
|
const addr = yield this.resolveName(name);
|
|
|
|
if (addr != address) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return name;
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
perform(method, params) {
|
|
|
|
return logger.throwError(method + " not implemented", Logger.errors.NOT_IMPLEMENTED, { operation: method });
|
|
|
|
}
|
2020-03-12 21:14:50 +03:00
|
|
|
_startEvent(event) {
|
|
|
|
this.polling = (this._events.filter((e) => e.pollable()).length > 0);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2020-03-12 21:14:50 +03:00
|
|
|
_stopEvent(event) {
|
2019-08-25 09:39:20 +03:00
|
|
|
this.polling = (this._events.filter((e) => e.pollable()).length > 0);
|
|
|
|
}
|
|
|
|
_addEventListener(eventName, listener, once) {
|
2020-03-12 21:14:50 +03:00
|
|
|
const event = new Event(getEventTag(eventName), listener, once);
|
|
|
|
this._events.push(event);
|
|
|
|
this._startEvent(event);
|
2019-08-25 09:39:20 +03:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
on(eventName, listener) {
|
|
|
|
return this._addEventListener(eventName, listener, false);
|
|
|
|
}
|
|
|
|
once(eventName, listener) {
|
|
|
|
return this._addEventListener(eventName, listener, true);
|
|
|
|
}
|
|
|
|
emit(eventName, ...args) {
|
|
|
|
let result = false;
|
2020-03-12 21:14:50 +03:00
|
|
|
let stopped = [];
|
2019-08-25 09:39:20 +03:00
|
|
|
let eventTag = getEventTag(eventName);
|
|
|
|
this._events = this._events.filter((event) => {
|
|
|
|
if (event.tag !== eventTag) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
event.listener.apply(this, args);
|
|
|
|
}, 0);
|
|
|
|
result = true;
|
2020-03-12 21:14:50 +03:00
|
|
|
if (event.once) {
|
|
|
|
stopped.push(event);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
2020-03-12 21:14:50 +03:00
|
|
|
stopped.forEach((event) => { this._stopEvent(event); });
|
2019-08-25 09:39:20 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
listenerCount(eventName) {
|
|
|
|
if (!eventName) {
|
|
|
|
return this._events.length;
|
|
|
|
}
|
|
|
|
let eventTag = getEventTag(eventName);
|
|
|
|
return this._events.filter((event) => {
|
|
|
|
return (event.tag === eventTag);
|
|
|
|
}).length;
|
|
|
|
}
|
|
|
|
listeners(eventName) {
|
|
|
|
if (eventName == null) {
|
|
|
|
return this._events.map((event) => event.listener);
|
|
|
|
}
|
|
|
|
let eventTag = getEventTag(eventName);
|
|
|
|
return this._events
|
|
|
|
.filter((event) => (event.tag === eventTag))
|
|
|
|
.map((event) => event.listener);
|
|
|
|
}
|
|
|
|
off(eventName, listener) {
|
|
|
|
if (listener == null) {
|
|
|
|
return this.removeAllListeners(eventName);
|
|
|
|
}
|
2020-03-12 21:14:50 +03:00
|
|
|
const stopped = [];
|
2019-08-25 09:39:20 +03:00
|
|
|
let found = false;
|
|
|
|
let eventTag = getEventTag(eventName);
|
|
|
|
this._events = this._events.filter((event) => {
|
|
|
|
if (event.tag !== eventTag || event.listener != listener) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
found = true;
|
2020-03-12 21:14:50 +03:00
|
|
|
stopped.push(event);
|
2019-08-25 09:39:20 +03:00
|
|
|
return false;
|
|
|
|
});
|
2020-03-12 21:14:50 +03:00
|
|
|
stopped.forEach((event) => { this._stopEvent(event); });
|
2019-08-25 09:39:20 +03:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
removeAllListeners(eventName) {
|
2020-03-12 21:14:50 +03:00
|
|
|
let stopped = [];
|
2019-08-25 09:39:20 +03:00
|
|
|
if (eventName == null) {
|
2020-03-12 21:14:50 +03:00
|
|
|
stopped = this._events;
|
2019-08-25 09:39:20 +03:00
|
|
|
this._events = [];
|
|
|
|
}
|
|
|
|
else {
|
2020-03-12 21:14:50 +03:00
|
|
|
const eventTag = getEventTag(eventName);
|
2019-08-25 09:39:20 +03:00
|
|
|
this._events = this._events.filter((event) => {
|
2020-03-12 21:14:50 +03:00
|
|
|
if (event.tag !== eventTag) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
stopped.push(event);
|
|
|
|
return false;
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
}
|
2020-03-12 21:14:50 +03:00
|
|
|
stopped.forEach((event) => { this._stopEvent(event); });
|
2019-08-25 09:39:20 +03:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 15:03:56 +03:00
|
|
|
//# sourceMappingURL=base-provider.js.map
|