2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import {
|
|
|
|
Block, BlockTag, BlockWithTransactions, EventType, Filter, FilterByBlockHash, ForkEvent,
|
|
|
|
Listener, Log, Provider, TransactionReceipt, TransactionRequest, TransactionResponse
|
|
|
|
} from "@ethersproject/abstract-provider";
|
|
|
|
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
|
|
|
|
import { arrayify, hexDataLength, hexlify, hexValue, isHexString } from "@ethersproject/bytes";
|
|
|
|
import { namehash } from "@ethersproject/hash";
|
|
|
|
import { getNetwork, Network, Networkish } from "@ethersproject/networks";
|
2019-07-02 23:06:47 +03:00
|
|
|
import { defineReadOnly, getStatic, resolveProperties } from "@ethersproject/properties";
|
2019-05-15 01:25:46 +03:00
|
|
|
import { Transaction } from "@ethersproject/transactions";
|
|
|
|
import { toUtf8String } from "@ethersproject/strings";
|
|
|
|
import { poll } from "@ethersproject/web";
|
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
import { Formatter } from "./formatter";
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////
|
|
|
|
// Event Serializeing
|
|
|
|
|
|
|
|
function checkTopic(topic: string): string {
|
|
|
|
if (topic == null) { return "null"; }
|
|
|
|
if (hexDataLength(topic) !== 32) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid topic", "topic", topic);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
return topic.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeTopics(topics: Array<string | Array<string>>): string {
|
|
|
|
// Remove trailing null AND-topics; they are redundant
|
|
|
|
topics = topics.slice();
|
2020-04-17 04:30:35 +03:00
|
|
|
while (topics.length > 0 && topics[topics.length - 1] == null) { topics.pop(); }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
return topics.map((topic) => {
|
|
|
|
if (Array.isArray(topic)) {
|
|
|
|
|
|
|
|
// Only track unique OR-topics
|
2019-11-01 17:51:08 +03:00
|
|
|
const unique: { [ topic: string ]: boolean } = { }
|
2019-05-15 01:25:46 +03:00
|
|
|
topic.forEach((topic) => {
|
|
|
|
unique[checkTopic(topic)] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// The order of OR-topics does not matter
|
2019-11-01 17:51:08 +03:00
|
|
|
const sorted = Object.keys(unique);
|
2019-05-15 01:25:46 +03:00
|
|
|
sorted.sort();
|
|
|
|
|
|
|
|
return sorted.join("|");
|
2020-04-28 11:52:29 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
} else {
|
|
|
|
return checkTopic(topic);
|
|
|
|
}
|
|
|
|
}).join("&");
|
|
|
|
}
|
|
|
|
|
|
|
|
function deserializeTopics(data: string): Array<string | Array<string>> {
|
2020-04-17 04:30:35 +03:00
|
|
|
if (data === "") { return [ ]; }
|
2020-04-28 11:52:29 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
return data.split(/&/g).map((topic) => {
|
2020-04-28 11:52:29 +03:00
|
|
|
if (topic === "") { return [ ]; }
|
|
|
|
|
|
|
|
const comps = topic.split("|").map((topic) => {
|
2019-05-15 01:25:46 +03:00
|
|
|
return ((topic === "null") ? null: topic);
|
|
|
|
});
|
2020-04-28 11:52:29 +03:00
|
|
|
|
|
|
|
return ((comps.length === 1) ? comps[0]: comps);
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEventTag(eventName: EventType): string {
|
|
|
|
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);
|
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
} else if (ForkEvent.isForkEvent(eventName)) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.warn("not implemented");
|
2019-05-15 01:25:46 +03: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();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////
|
|
|
|
// Provider Object
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EventType
|
|
|
|
* - "block"
|
2020-05-05 05:43:44 +03:00
|
|
|
* - "poll"
|
2019-05-15 01:25:46 +03:00
|
|
|
* - "pending"
|
|
|
|
* - "error"
|
|
|
|
* - filter
|
|
|
|
* - topics array
|
|
|
|
* - transaction hash
|
|
|
|
*/
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
export class Event {
|
2019-05-15 01:25:46 +03:00
|
|
|
readonly listener: Listener;
|
|
|
|
readonly once: boolean;
|
|
|
|
readonly tag: string;
|
|
|
|
|
|
|
|
constructor(tag: string, listener: Listener, once: boolean) {
|
|
|
|
defineReadOnly(this, "tag", tag);
|
|
|
|
defineReadOnly(this, "listener", listener);
|
|
|
|
defineReadOnly(this, "once", once);
|
|
|
|
}
|
|
|
|
|
2020-04-15 23:46:15 +03:00
|
|
|
get event(): EventType {
|
|
|
|
switch (this.type) {
|
|
|
|
case "tx":
|
|
|
|
return this.hash;
|
|
|
|
case "filter":
|
|
|
|
return this.filter;
|
|
|
|
}
|
|
|
|
return this.tag;
|
|
|
|
}
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
get type(): string {
|
|
|
|
return this.tag.split(":")[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
get hash(): string {
|
|
|
|
const comps = this.tag.split(":");
|
|
|
|
if (comps[0] !== "tx") { return null; }
|
|
|
|
return comps[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
get filter(): Filter {
|
|
|
|
const comps = this.tag.split(":");
|
|
|
|
if (comps[0] !== "filter") { return null; }
|
2020-04-17 04:30:35 +03:00
|
|
|
const address = comps[1];
|
|
|
|
|
|
|
|
const topics = deserializeTopics(comps[2]);
|
|
|
|
const filter: Filter = { };
|
|
|
|
|
|
|
|
if (topics.length > 0) { filter.topics = topics; }
|
|
|
|
if (address && address !== "*") { filter.address = address; }
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
pollable(): boolean {
|
2020-05-05 05:43:44 +03:00
|
|
|
return (this.tag.indexOf(":") >= 0 || this.tag === "block" || this.tag === "pending" || this.tag === "poll");
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let defaultFormatter: Formatter = null;
|
|
|
|
|
|
|
|
let nextPollId = 1;
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
export class BaseProvider extends Provider {
|
2020-05-04 00:32:16 +03:00
|
|
|
_networkPromise: Promise<Network>;
|
2019-05-15 01:25:46 +03:00
|
|
|
_network: Network;
|
|
|
|
|
|
|
|
_events: Array<Event>;
|
|
|
|
|
|
|
|
formatter: Formatter;
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// To help mitigate the eventually consistent nature of the blockchain
|
2019-05-15 01:25:46 +03:00
|
|
|
// we keep a mapping of events we emit. If we emit an event X, we expect
|
|
|
|
// that a user should be able to query for that event in the callback,
|
|
|
|
// if the node returns null, we stall the response until we get back a
|
|
|
|
// meaningful value, since we may be hitting a re-org, or a node that
|
|
|
|
// has not indexed the event yet.
|
|
|
|
// Events:
|
|
|
|
// - t:{hash} - Transaction hash
|
|
|
|
// - b:{hash} - BlockHash
|
|
|
|
// - block - The most recent emitted block
|
|
|
|
_emitted: { [ eventName: string ]: number | "pending" };
|
|
|
|
|
|
|
|
_pollingInterval: number;
|
2020-03-12 20:53:34 +03:00
|
|
|
_poller: NodeJS.Timer;
|
2020-05-05 05:43:44 +03:00
|
|
|
_bootstrapPoll: NodeJS.Timer;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
_lastBlockNumber: number;
|
|
|
|
|
|
|
|
_fastBlockNumber: number;
|
|
|
|
_fastBlockNumberPromise: Promise<number>;
|
|
|
|
_fastQueryDate: number;
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
_maxInternalBlockNumber: number;
|
|
|
|
_internalBlockNumber: Promise<{ blockNumber: number, reqTime: number, respTime: number }>;
|
|
|
|
|
2019-05-15 01:25:46 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
constructor(network: Networkish | Promise<Network>) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.checkNew(new.target, Provider);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.formatter = new.target.getFormatter();
|
|
|
|
|
|
|
|
if (network instanceof Promise) {
|
2020-05-04 00:32:16 +03:00
|
|
|
this._networkPromise = network;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
// Squash any "unhandled promise" errors; that do not need to be handled
|
2020-05-04 00:32:16 +03:00
|
|
|
network.catch((error) => { });
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
} else {
|
2019-11-01 17:51:08 +03:00
|
|
|
const knownNetwork = getStatic<(network: Networkish) => Network>(new.target, "getNetwork")(network);
|
2019-05-15 01:25:46 +03:00
|
|
|
if (knownNetwork) {
|
|
|
|
defineReadOnly(this, "_network", knownNetwork);
|
|
|
|
|
|
|
|
} else {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid network", "network", network);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
this._maxInternalBlockNumber = -1024;
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
this._lastBlockNumber = -2;
|
|
|
|
|
|
|
|
// Events being listened to
|
|
|
|
this._events = [];
|
|
|
|
|
|
|
|
this._pollingInterval = 4000;
|
|
|
|
|
|
|
|
this._emitted = { block: -2 };
|
|
|
|
|
|
|
|
this._fastQueryDate = 0;
|
|
|
|
}
|
|
|
|
|
2020-05-04 00:32:16 +03:00
|
|
|
async _ready(): Promise<Network> {
|
|
|
|
if (this._network == null) {
|
|
|
|
let network: Network = null;
|
|
|
|
if (this._networkPromise) {
|
|
|
|
try {
|
|
|
|
network = await this._networkPromise;
|
|
|
|
} catch (error) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the Provider's network detection (this MUST throw if it cannot)
|
|
|
|
if (network == null) {
|
|
|
|
network = await this.detectNetwork();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should never happen; every Provider sub-class should have
|
2020-05-29 11:03:46 +03:00
|
|
|
// suggested a network by here (or have thrown).
|
2020-05-04 00:32:16 +03:00
|
|
|
if (!network) {
|
|
|
|
logger.throwError("no network detected", Logger.errors.UNKNOWN_ERROR, { });
|
|
|
|
}
|
|
|
|
|
2020-05-29 11:03:46 +03:00
|
|
|
// Possible this call stacked so do not call defineReadOnly again
|
|
|
|
if (this._network == null) {
|
|
|
|
defineReadOnly(this, "_network", network);
|
|
|
|
}
|
2020-05-04 00:32:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._network;
|
|
|
|
}
|
|
|
|
|
|
|
|
get ready(): Promise<Network> {
|
|
|
|
return this._ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
async detectNetwork(): Promise<Network> {
|
|
|
|
return logger.throwError("provider does not support network detection", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "provider.detectNetwork"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
static getFormatter(): Formatter {
|
|
|
|
if (defaultFormatter == null) {
|
|
|
|
defaultFormatter = new Formatter();
|
|
|
|
}
|
|
|
|
return defaultFormatter;
|
2019-07-02 23:06:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static getNetwork(network: Networkish): Network {
|
|
|
|
return getNetwork((network == null) ? "homestead": network);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async _getInternalBlockNumber(maxAge: number): Promise<number> {
|
|
|
|
await this.ready;
|
|
|
|
|
|
|
|
const internalBlockNumber = this._internalBlockNumber;
|
|
|
|
|
|
|
|
if (maxAge > 0 && this._internalBlockNumber) {
|
|
|
|
const result = await internalBlockNumber;
|
|
|
|
if ((getTime() - result.respTime) <= maxAge) {
|
|
|
|
return result.blockNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const reqTime = getTime();
|
|
|
|
this._internalBlockNumber = this.perform("getBlockNumber", { }).then((blockNumber) => {
|
|
|
|
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 };
|
|
|
|
});
|
|
|
|
|
|
|
|
return (await this._internalBlockNumber).blockNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
async poll(): Promise<void> {
|
|
|
|
const pollId = nextPollId++;
|
2020-05-05 05:43:44 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
this.emit("willPoll", pollId);
|
|
|
|
|
|
|
|
// Track all running promises, so we can trigger a post-poll once they are complete
|
2019-11-01 17:51:08 +03:00
|
|
|
const runners: Array<Promise<void>> = [];
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const blockNumber = await this._getInternalBlockNumber(100 + this.pollingInterval / 2);
|
|
|
|
this._setFastBlockNumber(blockNumber);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2020-05-05 05:43:44 +03:00
|
|
|
// Emit a poll event after we have the latest (fast) block number
|
|
|
|
this.emit("poll", pollId, blockNumber);
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// If the block has not changed, meh.
|
2020-05-05 05:43:44 +03:00
|
|
|
if (blockNumber === this._lastBlockNumber) {
|
|
|
|
this.emit("didPoll", pollId);
|
|
|
|
return;
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// First polling cycle, trigger a "block" events
|
|
|
|
if (this._emitted.block === -2) {
|
|
|
|
this._emitted.block = blockNumber - 1;
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// Notify all listener for each block that has passed
|
|
|
|
for (let i = (<number>this._emitted.block) + 1; i <= blockNumber; i++) {
|
|
|
|
this.emit("block", i);
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// The emitted block was updated, check for obsolete events
|
|
|
|
if ((<number>this._emitted.block) !== blockNumber) {
|
|
|
|
this._emitted.block = blockNumber;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
Object.keys(this._emitted).forEach((key) => {
|
|
|
|
// The block event does not expire
|
|
|
|
if (key === "block") { return; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// The block we were at when we emitted this event
|
|
|
|
const eventBlockNumber = this._emitted[key];
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +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; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// 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-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// First polling cycle
|
|
|
|
if (this._lastBlockNumber === -2) {
|
|
|
|
this._lastBlockNumber = blockNumber - 1;
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// Find all transaction hashes we are waiting on
|
|
|
|
this._events.forEach((event) => {
|
2020-03-12 20:53:34 +03:00
|
|
|
switch (event.type) {
|
2019-11-01 17:51:08 +03:00
|
|
|
case "tx": {
|
2020-03-12 20:53:34 +03:00
|
|
|
const hash = event.hash;
|
2019-11-01 17:51:08 +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: Error) => { this.emit("error", error); });
|
|
|
|
|
|
|
|
runners.push(runner);
|
|
|
|
|
|
|
|
break;
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
case "filter": {
|
2020-03-12 20:53:34 +03:00
|
|
|
const filter = event.filter;
|
|
|
|
filter.fromBlock = this._lastBlockNumber + 1;
|
|
|
|
filter.toBlock = blockNumber;
|
2020-02-27 22:29:02 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const runner = this.getLogs(filter).then((logs) => {
|
|
|
|
if (logs.length === 0) { return; }
|
|
|
|
logs.forEach((log: Log) => {
|
|
|
|
this._emitted["b:" + log.blockHash] = log.blockNumber;
|
|
|
|
this._emitted["t:" + log.transactionHash] = log.blockNumber;
|
|
|
|
this.emit(filter, log);
|
|
|
|
});
|
|
|
|
}).catch((error: Error) => { this.emit("error", error); });
|
|
|
|
runners.push(runner);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
this._lastBlockNumber = blockNumber;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
Promise.all(runners).then(() => {
|
|
|
|
this.emit("didPoll", pollId);
|
|
|
|
});
|
2019-11-01 17:51:08 +03:00
|
|
|
|
|
|
|
return null;
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resetEventsBlock(blockNumber: number): void {
|
|
|
|
this._lastBlockNumber = blockNumber - 1;
|
|
|
|
if (this.polling) { this.poll(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
get network(): Network {
|
|
|
|
return this._network;
|
|
|
|
}
|
|
|
|
|
|
|
|
getNetwork(): Promise<Network> {
|
|
|
|
return this.ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
get blockNumber(): number {
|
2020-05-05 05:43:44 +03:00
|
|
|
this._getInternalBlockNumber(100 + this.pollingInterval / 2).then((blockNumber) => {
|
|
|
|
this._setFastBlockNumber(blockNumber);
|
|
|
|
});
|
|
|
|
|
|
|
|
return (this._fastBlockNumber != null) ? this._fastBlockNumber: -1;
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get polling(): boolean {
|
|
|
|
return (this._poller != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
set polling(value: boolean) {
|
2020-05-05 05:43:44 +03:00
|
|
|
if (value && !this._poller) {
|
|
|
|
this._poller = setInterval(this.poll.bind(this), this.pollingInterval);
|
|
|
|
|
|
|
|
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-05-15 01:25:46 +03:00
|
|
|
}
|
2020-05-05 05:43:44 +03:00
|
|
|
|
|
|
|
} else if (!value && this._poller) {
|
|
|
|
clearInterval(this._poller);
|
|
|
|
this._poller = null;
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get pollingInterval(): number {
|
|
|
|
return this._pollingInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
set pollingInterval(value: number) {
|
|
|
|
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(): Promise<number> {
|
2019-11-01 17:51:08 +03:00
|
|
|
const now = getTime();
|
2019-05-15 01:25:46 +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: number): void {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: Add .poller which must be an event emitter with a 'start', 'stop' and 'block' event;
|
|
|
|
// this will be used once we move to the WebSocket or other alternatives to polling
|
|
|
|
|
2020-02-04 07:05:04 +03:00
|
|
|
async waitForTransaction(transactionHash: string, confirmations?: number, timeout?: number): Promise<TransactionReceipt> {
|
2019-05-15 01:25:46 +03:00
|
|
|
if (confirmations == null) { confirmations = 1; }
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const receipt = await this.getTransactionReceipt(transactionHash);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// Receipt is already good
|
2020-02-04 07:05:04 +03:00
|
|
|
if ((receipt ? receipt.confirmations: 0) >= confirmations) { return receipt; }
|
2019-11-01 17:51:08 +03:00
|
|
|
|
|
|
|
// Poll until the receipt is good...
|
2020-02-04 07:05:04 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let timer: NodeJS.Timer = null;
|
|
|
|
let done = false;
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const handler = (receipt: TransactionReceipt) => {
|
2019-05-15 01:25:46 +03:00
|
|
|
if (receipt.confirmations < confirmations) { return; }
|
2020-02-04 07:05:04 +03:00
|
|
|
|
|
|
|
if (timer) { clearTimeout(timer); }
|
|
|
|
if (done) { return; }
|
|
|
|
done = true;
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
this.removeListener(transactionHash, handler);
|
|
|
|
resolve(receipt);
|
|
|
|
}
|
|
|
|
this.on(transactionHash, handler);
|
2020-02-04 07:05:04 +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-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlockNumber(): Promise<number> {
|
2019-11-01 17:51:08 +03:00
|
|
|
return this._getInternalBlockNumber(0);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async getGasPrice(): Promise<BigNumber> {
|
|
|
|
await this.ready;
|
|
|
|
return BigNumber.from(await this.perform("getGasPrice", { }));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<BigNumber> {
|
|
|
|
await this.ready;
|
|
|
|
const params = await resolveProperties({
|
|
|
|
address: this._getAddress(addressOrName),
|
|
|
|
blockTag: this._getBlockTag(blockTag)
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
2019-11-01 17:51:08 +03:00
|
|
|
return BigNumber.from(await this.perform("getBalance", params));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<number> {
|
|
|
|
await this.ready;
|
|
|
|
const params = await resolveProperties({
|
|
|
|
address: this._getAddress(addressOrName),
|
|
|
|
blockTag: this._getBlockTag(blockTag)
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
2019-11-01 17:51:08 +03:00
|
|
|
return BigNumber.from(await this.perform("getTransactionCount", params)).toNumber();
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
|
|
|
|
await this.ready;
|
|
|
|
const params = await resolveProperties({
|
|
|
|
address: this._getAddress(addressOrName),
|
|
|
|
blockTag: this._getBlockTag(blockTag)
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
2019-11-01 17:51:08 +03:00
|
|
|
return hexlify(await this.perform("getCode", params));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
|
|
|
|
await this.ready;
|
|
|
|
const params = await resolveProperties({
|
|
|
|
address: this._getAddress(addressOrName),
|
|
|
|
blockTag: this._getBlockTag(blockTag),
|
|
|
|
position: Promise.resolve(position).then((p) => hexValue(p))
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
2019-11-01 17:51:08 +03:00
|
|
|
return hexlify(await this.perform("getStorageAt", params));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This should be called by any subclass wrapping a TransactionResponse
|
|
|
|
_wrapTransaction(tx: Transaction, hash?: string): TransactionResponse {
|
|
|
|
if (hash != null && hexDataLength(hash) !== 32) { throw new Error("invalid response - sendTransaction"); }
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const result = <TransactionResponse>tx;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
// Check the hash we expect is the same as the hash the server reported
|
|
|
|
if (hash != null && tx.hash !== hash) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwError("Transaction hash mismatch from Provider.sendTransaction.", Logger.errors.UNKNOWN_ERROR, { expectedHash: tx.hash, returnedHash: hash });
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: (confirmations? number, timeout? number)
|
2019-11-01 17:51:08 +03:00
|
|
|
result.wait = async (confirmations?: number) => {
|
2019-05-15 01:25:46 +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-01 17:51:08 +03:00
|
|
|
const receipt = await this.waitForTransaction(tx.hash, confirmations)
|
|
|
|
if (receipt == null && confirmations === 0) { return null; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// No longer pending, allow the polling loop to garbage collect this
|
|
|
|
this._emitted["t:" + tx.hash] = receipt.blockNumber;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
if (receipt.status === 0) {
|
|
|
|
logger.throwError("transaction failed", Logger.errors.CALL_EXCEPTION, {
|
|
|
|
transactionHash: tx.hash,
|
|
|
|
transaction: tx,
|
|
|
|
receipt: receipt
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return receipt;
|
2019-05-15 01:25:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse> {
|
|
|
|
await this.ready;
|
|
|
|
const hexTx = await Promise.resolve(signedTransaction).then(t => hexlify(t));
|
|
|
|
const tx = this.formatter.transaction(signedTransaction);
|
|
|
|
try {
|
|
|
|
const hash = await this.perform("sendTransaction", { signedTransaction: hexTx });
|
|
|
|
return this._wrapTransaction(tx, hash);
|
|
|
|
} catch (error) {
|
|
|
|
(<any>error).transaction = tx;
|
|
|
|
(<any>error).transactionHash = tx.hash;
|
2019-05-15 01:25:46 +03:00
|
|
|
throw error;
|
2019-11-01 17:51:08 +03:00
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async _getTransactionRequest(transaction: TransactionRequest | Promise<TransactionRequest>): Promise<Transaction> {
|
|
|
|
const values: any = await transaction;
|
|
|
|
|
|
|
|
const tx: any = { };
|
|
|
|
|
|
|
|
["from", "to"].forEach((key) => {
|
|
|
|
if (values[key] == null) { return; }
|
|
|
|
tx[key] = Promise.resolve(values[key]).then((v) => (v ? this._getAddress(v): null))
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
["gasLimit", "gasPrice", "value"].forEach((key) => {
|
|
|
|
if (values[key] == null) { return; }
|
|
|
|
tx[key] = Promise.resolve(values[key]).then((v) => (v ? BigNumber.from(v): null));
|
|
|
|
});
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
["data"].forEach((key) => {
|
|
|
|
if (values[key] == null) { return; }
|
|
|
|
tx[key] = Promise.resolve(values[key]).then((v) => (v ? hexlify(v): null));
|
|
|
|
});
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
return this.formatter.transactionRequest(await resolveProperties(tx));
|
|
|
|
}
|
2019-05-24 01:24:31 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async _getFilter(filter: Filter | FilterByBlockHash | Promise<Filter | FilterByBlockHash>): Promise<Filter | FilterByBlockHash> {
|
|
|
|
if (filter instanceof Promise) { filter = await filter; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const result: any = { };
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
if (filter.address != null) {
|
|
|
|
result.address = this._getAddress(filter.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
["blockHash", "topics"].forEach((key) => {
|
|
|
|
if ((<any>filter)[key] == null) { return; }
|
|
|
|
result[key] = (<any>filter)[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
["fromBlock", "toBlock"].forEach((key) => {
|
|
|
|
if ((<any>filter)[key] == null) { return; }
|
|
|
|
result[key] = this._getBlockTag((<any>filter)[key]);
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
2019-11-01 17:51:08 +03:00
|
|
|
|
2020-04-24 05:54:25 +03:00
|
|
|
return this.formatter.filter(await resolveProperties(result));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async call(transaction: TransactionRequest | Promise<TransactionRequest>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
|
|
|
|
await this.ready;
|
|
|
|
const params = await resolveProperties({
|
|
|
|
transaction: this._getTransactionRequest(transaction),
|
|
|
|
blockTag: this._getBlockTag(blockTag)
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
2019-11-01 17:51:08 +03:00
|
|
|
return hexlify(await this.perform("call", params));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async estimateGas(transaction: TransactionRequest | Promise<TransactionRequest>): Promise<BigNumber> {
|
|
|
|
await this.ready;
|
|
|
|
const params = await resolveProperties({
|
|
|
|
transaction: this._getTransactionRequest(transaction)
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
2019-11-01 17:51:08 +03:00
|
|
|
return BigNumber.from(await this.perform("estimateGas", params));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async _getAddress(addressOrName: string | Promise<string>): Promise<string> {
|
|
|
|
const address = await this.resolveName(addressOrName);
|
|
|
|
if (address == null) {
|
|
|
|
logger.throwError("ENS name not configured", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: `resolveName(${ JSON.stringify(addressOrName) })`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return address;
|
2019-05-24 02:10:15 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async _getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>, includeTransactions?: boolean): Promise<Block | BlockWithTransactions> {
|
|
|
|
await this.ready;
|
|
|
|
|
2019-10-16 18:53:25 +03:00
|
|
|
if (blockHashOrBlockTag instanceof Promise) {
|
2019-11-01 17:51:08 +03:00
|
|
|
blockHashOrBlockTag = await blockHashOrBlockTag;
|
2019-10-16 18:53:25 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// If blockTag is a number (not "latest", etc), this is the block number
|
|
|
|
let blockNumber = -128;
|
2019-10-16 18:53:25 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const params: { [key: string]: any } = {
|
|
|
|
includeTransactions: !!includeTransactions
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isHexString(blockHashOrBlockTag, 32)) {
|
|
|
|
params.blockHash = blockHashOrBlockTag;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
params.blockTag = this.formatter.blockTag(await this._getBlockTag(blockHashOrBlockTag));
|
|
|
|
if (isHexString(params.blockTag)) {
|
|
|
|
blockNumber = parseInt(params.blockTag.substring(2), 16);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
2019-11-01 17:51:08 +03:00
|
|
|
} catch (error) {
|
|
|
|
logger.throwArgumentError("invalid block hash or block tag", "blockHashOrBlockTag", blockHashOrBlockTag);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
return poll(async () => {
|
|
|
|
const block = await this.perform("getBlock", params);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// Block was not found
|
|
|
|
if (block == null) {
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// 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-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +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-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// Retry on the next block
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// Add transactions
|
|
|
|
if (includeTransactions) {
|
2020-01-19 05:24:28 +03:00
|
|
|
let blockNumber: number = 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 = await 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-01 17:51:08 +03:00
|
|
|
return this.formatter.blockWithTransactions(block);
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
return this.formatter.block(block);
|
2020-05-30 03:41:20 +03:00
|
|
|
}, { oncePoll: this });
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<Block> {
|
|
|
|
return <Promise<Block>>(this._getBlock(blockHashOrBlockTag, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlockWithTransactions(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<BlockWithTransactions> {
|
|
|
|
return <Promise<BlockWithTransactions>>(this._getBlock(blockHashOrBlockTag, true));
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async getTransaction(transactionHash: string | Promise<string>): Promise<TransactionResponse> {
|
|
|
|
await this.ready;
|
|
|
|
if (transactionHash instanceof Promise) { transactionHash = await transactionHash; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const params = { transactionHash: this.formatter.hash(transactionHash, true) };
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
return poll(async () => {
|
|
|
|
const result = await this.perform("getTransaction", params);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
if (result == null) {
|
|
|
|
if (this._emitted["t:" + transactionHash] == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const tx = this.formatter.transactionResponse(result);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
if (tx.blockNumber == null) {
|
|
|
|
tx.confirmations = 0;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
} else if (tx.confirmations == null) {
|
|
|
|
const blockNumber = await 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 03:41:20 +03:00
|
|
|
}, { oncePoll: this });
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async getTransactionReceipt(transactionHash: string | Promise<string>): Promise<TransactionReceipt> {
|
|
|
|
await this.ready;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
if (transactionHash instanceof Promise) { transactionHash = await transactionHash; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const params = { transactionHash: this.formatter.hash(transactionHash, true) };
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
return poll(async () => {
|
|
|
|
const result = await this.perform("getTransactionReceipt", params);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
if (result == null) {
|
|
|
|
if (this._emitted["t:" + transactionHash] == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// "geth-etc" returns receipts before they are ready
|
|
|
|
if (result.blockHash == null) { return undefined; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const receipt = this.formatter.receipt(result);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
if (receipt.blockNumber == null) {
|
|
|
|
receipt.confirmations = 0;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
} else if (receipt.confirmations == null) {
|
|
|
|
const blockNumber = await this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// 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 03:41:20 +03:00
|
|
|
}, { oncePoll: this });
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async getLogs(filter: Filter | FilterByBlockHash | Promise<Filter | FilterByBlockHash>): Promise<Array<Log>> {
|
|
|
|
await this.ready;
|
|
|
|
const params = await resolveProperties({ filter: this._getFilter(filter) });
|
2020-01-19 05:24:28 +03:00
|
|
|
const logs: Array<Log> = await this.perform("getLogs", params);
|
|
|
|
logs.forEach((log) => {
|
|
|
|
if (log.removed == null) { log.removed = false; }
|
|
|
|
});
|
2019-11-01 17:51:08 +03:00
|
|
|
return Formatter.arrayOf(this.formatter.filterLog.bind(this.formatter))(logs);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getEtherPrice(): Promise<number> {
|
|
|
|
await this.ready;
|
|
|
|
return this.perform("getEtherPrice", { });
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async _getBlockTag(blockTag: BlockTag | Promise<BlockTag>): Promise<BlockTag> {
|
2019-05-15 01:25:46 +03:00
|
|
|
if (blockTag instanceof Promise) {
|
2019-11-01 17:51:08 +03:00
|
|
|
blockTag = await blockTag;
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(blockTag) === "number" && blockTag < 0) {
|
|
|
|
if (blockTag % 1) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid BlockTag", "blockTag", blockTag);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
let blockNumber = await this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
|
|
|
|
blockNumber += blockTag;
|
|
|
|
if (blockNumber < 0) { blockNumber = 0; }
|
|
|
|
return this.formatter.blockTag(blockNumber)
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
return this.formatter.blockTag(blockTag);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async _getResolver(name: string): Promise<string> {
|
2019-05-15 01:25:46 +03:00
|
|
|
// Get the resolver from the blockchain
|
2019-11-01 17:51:08 +03:00
|
|
|
const network = await this.getNetwork();
|
|
|
|
|
|
|
|
// No ENS...
|
|
|
|
if (!network.ensAddress) {
|
|
|
|
logger.throwError(
|
|
|
|
"network does not support ENS",
|
|
|
|
Logger.errors.UNSUPPORTED_OPERATION,
|
|
|
|
{ operation: "ENS", network: network.name }
|
|
|
|
);
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// keccak256("resolver(bytes32)")
|
|
|
|
const transaction = {
|
|
|
|
to: network.ensAddress,
|
|
|
|
data: ("0x0178b8bf" + namehash(name).substring(2))
|
|
|
|
};
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
return this.formatter.callAddress(await this.call(transaction));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async resolveName(name: string | Promise<string>): Promise<string> {
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
if (name instanceof Promise) { name = await name; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
// If it is already an address, nothing to resolve
|
|
|
|
try {
|
|
|
|
return Promise.resolve(this.formatter.address(name));
|
2020-01-08 03:41:43 +03:00
|
|
|
} catch (error) {
|
|
|
|
// If is is a hexstring, the address is bad (See #694)
|
|
|
|
if (isHexString(name)) { throw error; }
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2020-04-04 04:32:44 +03:00
|
|
|
if (typeof(name) !== "string") {
|
|
|
|
logger.throwArgumentError("invalid ENS name", "name", name);
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
// Get the addr from the resovler
|
2019-11-01 17:51:08 +03:00
|
|
|
const resolverAddress = await this._getResolver(name);
|
|
|
|
if (!resolverAddress) { return null; }
|
|
|
|
|
|
|
|
// keccak256("addr(bytes32)")
|
|
|
|
const transaction = {
|
|
|
|
to: resolverAddress,
|
|
|
|
data: ("0x3b3b57de" + namehash(name).substring(2))
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.formatter.callAddress(await this.call(transaction));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
async lookupAddress(address: string | Promise<string>): Promise<string> {
|
|
|
|
if (address instanceof Promise) { address = await address; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
address = this.formatter.address(address);
|
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const reverseName = address.substring(2).toLowerCase() + ".addr.reverse";
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const resolverAddress = await this._getResolver(reverseName);
|
|
|
|
if (!resolverAddress) { return null; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// keccak("name(bytes32)")
|
|
|
|
let bytes = arrayify(await this.call({
|
|
|
|
to: resolverAddress,
|
|
|
|
data: ("0x691f3431" + namehash(reverseName).substring(2))
|
|
|
|
}));
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// 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);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// Not a length-prefixed string
|
|
|
|
if (bytes.length < 32) { return null; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// Get the length of the string (from the length-prefix)
|
|
|
|
const length = BigNumber.from(bytes.slice(0, 32)).toNumber();
|
|
|
|
bytes = bytes.slice(32);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
// Length longer than available data
|
|
|
|
if (length > bytes.length) { return null; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:51:08 +03:00
|
|
|
const name = toUtf8String(bytes.slice(0, length));
|
|
|
|
|
|
|
|
// Make sure the reverse record matches the foward record
|
|
|
|
const addr = await this.resolveName(name);
|
|
|
|
if (addr != address) { return null; }
|
|
|
|
|
|
|
|
return name;
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
perform(method: string, params: any): Promise<any> {
|
2019-08-02 01:04:06 +03:00
|
|
|
return logger.throwError(method + " not implemented", Logger.errors.NOT_IMPLEMENTED, { operation: method });
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
_startEvent(event: Event): void {
|
|
|
|
this.polling = (this._events.filter((e) => e.pollable()).length > 0);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
_stopEvent(event: Event): void {
|
2019-05-15 01:25:46 +03:00
|
|
|
this.polling = (this._events.filter((e) => e.pollable()).length > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
_addEventListener(eventName: EventType, listener: Listener, once: boolean): this {
|
2020-03-12 20:53:34 +03:00
|
|
|
const event = new Event(getEventTag(eventName), listener, once)
|
|
|
|
this._events.push(event);
|
|
|
|
this._startEvent(event);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
on(eventName: EventType, listener: Listener): this {
|
|
|
|
return this._addEventListener(eventName, listener, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
once(eventName: EventType, listener: Listener): this {
|
|
|
|
return this._addEventListener(eventName, listener, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
emit(eventName: EventType, ...args: Array<any>): boolean {
|
|
|
|
let result = false;
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
let stopped: Array<Event> = [ ];
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
let eventTag = getEventTag(eventName);
|
|
|
|
this._events = this._events.filter((event) => {
|
|
|
|
if (event.tag !== eventTag) { return true; }
|
2020-03-12 20:53:34 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
event.listener.apply(this, args);
|
|
|
|
}, 0);
|
2020-03-12 20:53:34 +03:00
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
result = true;
|
2020-03-12 20:53:34 +03:00
|
|
|
|
|
|
|
if (event.once) {
|
|
|
|
stopped.push(event);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
stopped.forEach((event) => { this._stopEvent(event); });
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerCount(eventName?: EventType): number {
|
|
|
|
if (!eventName) { return this._events.length; }
|
|
|
|
|
|
|
|
let eventTag = getEventTag(eventName);
|
|
|
|
return this._events.filter((event) => {
|
|
|
|
return (event.tag === eventTag);
|
|
|
|
}).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners(eventName?: EventType): Array<Listener> {
|
|
|
|
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: EventType, listener?: Listener): this {
|
|
|
|
if (listener == null) {
|
|
|
|
return this.removeAllListeners(eventName);
|
|
|
|
}
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
const stopped: Array<Event> = [ ];
|
|
|
|
|
2019-05-15 01:25:46 +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 20:53:34 +03:00
|
|
|
stopped.push(event);
|
2019-05-15 01:25:46 +03:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
stopped.forEach((event) => { this._stopEvent(event); });
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAllListeners(eventName?: EventType): this {
|
2020-03-12 20:53:34 +03:00
|
|
|
let stopped: Array<Event> = [ ];
|
2019-05-15 01:25:46 +03:00
|
|
|
if (eventName == null) {
|
2020-03-12 20:53:34 +03:00
|
|
|
stopped = this._events;
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
this._events = [ ];
|
|
|
|
} else {
|
2020-03-12 20:53:34 +03:00
|
|
|
const eventTag = getEventTag(eventName);
|
2019-05-15 01:25:46 +03:00
|
|
|
this._events = this._events.filter((event) => {
|
2020-03-12 20:53:34 +03:00
|
|
|
if (event.tag !== eventTag) { return true; }
|
|
|
|
stopped.push(event);
|
|
|
|
return false;
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-12 20:53:34 +03:00
|
|
|
stopped.forEach((event) => { this._stopEvent(event); });
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|