2020-03-12 18:53:34 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
2020-10-03 02:54:03 -04:00
|
|
|
import { Network, Networkish } from "@ethersproject/networks";
|
2020-03-12 18:53:34 +01:00
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
|
|
|
|
|
|
|
import { Event } from "./base-provider";
|
|
|
|
import { JsonRpcProvider } from "./json-rpc-provider";
|
2020-11-14 17:42:36 -05:00
|
|
|
import { WebSocket } from "./ws";
|
2020-03-12 18:53:34 +01:00
|
|
|
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notes:
|
|
|
|
*
|
|
|
|
* This provider differs a bit from the polling providers. One main
|
|
|
|
* difference is how it handles consistency. The polling providers
|
|
|
|
* will stall responses to ensure a consistent state, while this
|
|
|
|
* WebSocket provider assumes the connected backend will manage this.
|
|
|
|
*
|
2021-10-04 11:46:24 -04:00
|
|
|
* For example, if a polling provider emits an event which indicates
|
2020-03-12 18:53:34 +01:00
|
|
|
* the event occurred in blockhash XXX, a call to fetch that block by
|
|
|
|
* its hash XXX, if not present will retry until it is present. This
|
|
|
|
* can occur when querying a pool of nodes that are mildly out of sync
|
|
|
|
* with each other.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let NextId = 1;
|
|
|
|
|
|
|
|
export type InflightRequest = {
|
|
|
|
callback: (error: Error, result: any) => void;
|
|
|
|
payload: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Subscription = {
|
|
|
|
tag: string;
|
|
|
|
processFunc: (payload: any) => void;
|
|
|
|
};
|
2020-04-15 16:46:15 -04:00
|
|
|
|
2022-02-19 01:57:45 -05:00
|
|
|
export interface WebSocketLike {
|
2022-08-15 17:56:04 -04:00
|
|
|
onopen: ((...args: Array<any>) => any) | null;
|
|
|
|
onmessage: ((...args: Array<any>) => any) | null;
|
|
|
|
onerror: ((...args: Array<any>) => any) | null;
|
2022-02-19 01:57:45 -05:00
|
|
|
|
|
|
|
readyState: number;
|
|
|
|
|
|
|
|
send(payload: any): void;
|
|
|
|
close(code?: number, reason?: string): void;
|
|
|
|
}
|
2020-04-15 16:46:15 -04:00
|
|
|
|
|
|
|
// For more info about the Real-time Event API see:
|
|
|
|
// https://geth.ethereum.org/docs/rpc/pubsub
|
|
|
|
|
2020-03-12 18:53:34 +01:00
|
|
|
export class WebSocketProvider extends JsonRpcProvider {
|
|
|
|
readonly _websocket: any;
|
|
|
|
readonly _requests: { [ name: string ]: InflightRequest };
|
2020-10-03 02:54:03 -04:00
|
|
|
readonly _detectNetwork: Promise<Network>;
|
2020-03-12 18:53:34 +01:00
|
|
|
|
|
|
|
// Maps event tag to subscription ID (we dedupe identical events)
|
|
|
|
readonly _subIds: { [ tag: string ]: Promise<string> };
|
|
|
|
|
|
|
|
// Maps Subscription ID to Subscription
|
|
|
|
readonly _subs: { [ name: string ]: Subscription };
|
|
|
|
|
|
|
|
_wsReady: boolean;
|
|
|
|
|
2022-02-19 01:57:45 -05:00
|
|
|
constructor(url: string | WebSocketLike, network?: Networkish) {
|
|
|
|
|
2020-06-03 02:37:59 -04:00
|
|
|
// This will be added in the future; please open an issue to expedite
|
|
|
|
if (network === "any") {
|
|
|
|
logger.throwError("WebSocketProvider does not support 'any' network yet", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "network:any"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-19 01:57:45 -05:00
|
|
|
if (typeof(url) === "string") {
|
|
|
|
super(url, network);
|
|
|
|
} else {
|
|
|
|
super("_websocket", network);
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:53:34 +01:00
|
|
|
this._pollingInterval = -1;
|
|
|
|
|
2020-10-03 02:54:03 -04:00
|
|
|
this._wsReady = false;
|
|
|
|
|
2022-02-19 01:57:45 -05:00
|
|
|
if (typeof(url) === "string") {
|
|
|
|
defineReadOnly(this, "_websocket", new WebSocket(this.connection.url));
|
|
|
|
} else {
|
|
|
|
defineReadOnly(this, "_websocket", url);
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:53:34 +01:00
|
|
|
defineReadOnly(this, "_requests", { });
|
|
|
|
defineReadOnly(this, "_subs", { });
|
|
|
|
defineReadOnly(this, "_subIds", { });
|
2020-10-03 02:54:03 -04:00
|
|
|
defineReadOnly(this, "_detectNetwork", super.detectNetwork());
|
2020-03-12 18:53:34 +01:00
|
|
|
|
|
|
|
// Stall sending requests until the socket is open...
|
2022-02-19 01:57:45 -05:00
|
|
|
this.websocket.onopen = () => {
|
2020-03-12 18:53:34 +01:00
|
|
|
this._wsReady = true;
|
|
|
|
Object.keys(this._requests).forEach((id) => {
|
2022-02-19 01:57:45 -05:00
|
|
|
this.websocket.send(this._requests[id].payload);
|
2020-03-12 18:53:34 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-19 01:57:45 -05:00
|
|
|
this.websocket.onmessage = (messageEvent: { data: string }) => {
|
2020-03-12 18:53:34 +01:00
|
|
|
const data = messageEvent.data;
|
|
|
|
const result = JSON.parse(data);
|
2020-05-03 14:18:12 -04:00
|
|
|
if (result.id != null) {
|
2020-03-12 18:53:34 +01:00
|
|
|
const id = String(result.id);
|
|
|
|
const request = this._requests[id];
|
|
|
|
delete this._requests[id];
|
|
|
|
|
2020-05-03 14:18:12 -04:00
|
|
|
if (result.result !== undefined) {
|
2020-03-12 18:53:34 +01:00
|
|
|
request.callback(null, result.result);
|
|
|
|
|
2020-10-06 21:12:49 -04:00
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
request: JSON.parse(request.payload),
|
|
|
|
response: result.result,
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
|
2020-03-12 18:53:34 +01:00
|
|
|
} else {
|
2020-10-06 21:12:49 -04:00
|
|
|
let error: Error = null;
|
2020-03-12 18:53:34 +01:00
|
|
|
if (result.error) {
|
2020-10-06 21:12:49 -04:00
|
|
|
error = new Error(result.error.message || "unknown error");
|
|
|
|
defineReadOnly(<any>error, "code", result.error.code || null);
|
|
|
|
defineReadOnly(<any>error, "response", data);
|
2020-03-12 18:53:34 +01:00
|
|
|
} else {
|
2020-10-06 21:12:49 -04:00
|
|
|
error = new Error("unknown error");
|
2020-03-12 18:53:34 +01:00
|
|
|
}
|
2020-10-06 21:12:49 -04:00
|
|
|
|
|
|
|
request.callback(error, undefined);
|
|
|
|
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
error: error,
|
|
|
|
request: JSON.parse(request.payload),
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
|
2020-03-12 18:53:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (result.method === "eth_subscription") {
|
|
|
|
// Subscription...
|
|
|
|
const sub = this._subs[result.params.subscription];
|
|
|
|
if (sub) {
|
|
|
|
//this.emit.apply(this, );
|
|
|
|
sub.processFunc(result.params.result)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.warn("this should not happen");
|
|
|
|
}
|
|
|
|
};
|
2020-05-29 20:41:20 -04:00
|
|
|
|
|
|
|
// This Provider does not actually poll, but we want to trigger
|
|
|
|
// poll events for things that depend on them (like stalling for
|
|
|
|
// block and transaction lookups)
|
|
|
|
const fauxPoll = setInterval(() => {
|
|
|
|
this.emit("poll");
|
|
|
|
}, 1000);
|
|
|
|
if (fauxPoll.unref) { fauxPoll.unref(); }
|
2020-03-12 18:53:34 +01:00
|
|
|
}
|
|
|
|
|
2022-02-19 01:57:45 -05:00
|
|
|
// Cannot narrow the type of _websocket, as that is not backwards compatible
|
|
|
|
// so we add a getter and let the WebSocket be a public API.
|
|
|
|
get websocket(): WebSocketLike { return this._websocket; }
|
|
|
|
|
2020-10-03 02:54:03 -04:00
|
|
|
detectNetwork(): Promise<Network> {
|
|
|
|
return this._detectNetwork;
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:53:34 +01:00
|
|
|
get pollingInterval(): number {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
resetEventsBlock(blockNumber: number): void {
|
|
|
|
logger.throwError("cannot reset events block on WebSocketProvider", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "resetEventBlock"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
set pollingInterval(value: number) {
|
|
|
|
logger.throwError("cannot set polling interval on WebSocketProvider", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "setPollingInterval"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async poll(): Promise<void> {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
set polling(value: boolean) {
|
|
|
|
if (!value) { return; }
|
|
|
|
|
|
|
|
logger.throwError("cannot set polling on WebSocketProvider", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "setPolling"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
send(method: string, params?: Array<any>): Promise<any> {
|
|
|
|
const rid = NextId++;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
function callback(error: Error, result: any) {
|
|
|
|
if (error) { return reject(error); }
|
|
|
|
return resolve(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
const payload = JSON.stringify({
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: rid,
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
});
|
|
|
|
|
2020-10-06 21:12:49 -04:00
|
|
|
this.emit("debug", {
|
|
|
|
action: "request",
|
|
|
|
request: JSON.parse(payload),
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
|
2020-03-12 18:53:34 +01:00
|
|
|
this._requests[String(rid)] = { callback, payload };
|
|
|
|
|
2022-02-19 01:57:45 -05:00
|
|
|
if (this._wsReady) { this.websocket.send(payload); }
|
2020-03-12 18:53:34 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultUrl(): string {
|
2020-04-22 02:42:25 -04:00
|
|
|
return "ws:/\/localhost:8546";
|
2020-03-12 18:53:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async _subscribe(tag: string, param: Array<any>, processFunc: (result: any) => void): Promise<void> {
|
|
|
|
let subIdPromise = this._subIds[tag];
|
|
|
|
if (subIdPromise == null) {
|
2020-04-23 23:10:21 -04:00
|
|
|
subIdPromise = Promise.all(param).then((param) => {
|
|
|
|
return this.send("eth_subscribe", param);
|
|
|
|
});
|
2020-03-12 18:53:34 +01:00
|
|
|
this._subIds[tag] = subIdPromise;
|
|
|
|
}
|
|
|
|
const subId = await subIdPromise;
|
|
|
|
this._subs[subId] = { tag, processFunc };
|
|
|
|
}
|
|
|
|
|
|
|
|
_startEvent(event: Event): void {
|
|
|
|
switch (event.type) {
|
|
|
|
case "block":
|
2020-05-26 06:42:19 -04:00
|
|
|
this._subscribe("block", [ "newHeads" ], (result: any) => {
|
2020-05-29 20:41:20 -04:00
|
|
|
const blockNumber = BigNumber.from(result.number).toNumber();
|
|
|
|
this._emitted.block = blockNumber;
|
|
|
|
this.emit("block", blockNumber);
|
2020-03-12 18:53:34 +01:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "pending":
|
|
|
|
this._subscribe("pending", [ "newPendingTransactions" ], (result: any) => {
|
|
|
|
this.emit("pending", result);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2020-04-23 23:17:55 -04:00
|
|
|
case "filter":
|
2020-04-23 23:10:21 -04:00
|
|
|
this._subscribe(event.tag, [ "logs", this._getFilter(event.filter) ], (result: any) => {
|
2020-04-23 23:17:55 -04:00
|
|
|
if (result.removed == null) { result.removed = false; }
|
|
|
|
this.emit(event.filter, this.formatter.filterLog(result));
|
2020-03-12 18:53:34 +01:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "tx": {
|
|
|
|
const emitReceipt = (event: Event) => {
|
|
|
|
const hash = event.hash;
|
|
|
|
this.getTransactionReceipt(hash).then((receipt) => {
|
|
|
|
if (!receipt) { return; }
|
|
|
|
this.emit(hash, receipt);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// In case it is already mined
|
|
|
|
emitReceipt(event);
|
|
|
|
|
|
|
|
// To keep things simple, we start up a single newHeads subscription
|
|
|
|
// to keep an eye out for transactions we are watching for.
|
|
|
|
// Starting a subscription for an event (i.e. "tx") that is already
|
|
|
|
// running is (basically) a nop.
|
2020-05-26 06:42:19 -04:00
|
|
|
this._subscribe("tx", [ "newHeads" ], (result: any) => {
|
2020-03-12 18:53:34 +01:00
|
|
|
this._events.filter((e) => (e.type === "tx")).forEach(emitReceipt);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing is needed
|
|
|
|
case "debug":
|
2020-05-29 20:41:20 -04:00
|
|
|
case "poll":
|
|
|
|
case "willPoll":
|
|
|
|
case "didPoll":
|
2020-03-12 18:53:34 +01:00
|
|
|
case "error":
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
console.log("unhandled:", event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_stopEvent(event: Event): void {
|
|
|
|
let tag = event.tag;
|
|
|
|
|
|
|
|
if (event.type === "tx") {
|
|
|
|
// There are remaining transaction event listeners
|
|
|
|
if (this._events.filter((e) => (e.type === "tx")).length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tag = "tx";
|
2020-04-15 16:46:15 -04:00
|
|
|
} else if (this.listenerCount(event.event)) {
|
2020-03-12 18:53:34 +01:00
|
|
|
// There are remaining event listeners
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const subId = this._subIds[tag];
|
|
|
|
if (!subId) { return; }
|
|
|
|
|
|
|
|
delete this._subIds[tag];
|
|
|
|
subId.then((subId) => {
|
|
|
|
if (!this._subs[subId]) { return; }
|
|
|
|
delete this._subs[subId];
|
|
|
|
this.send("eth_unsubscribe", [ subId ]);
|
|
|
|
});
|
|
|
|
}
|
2020-07-12 05:02:08 -04:00
|
|
|
|
|
|
|
async destroy(): Promise<void> {
|
|
|
|
// Wait until we have connected before trying to disconnect
|
2022-02-19 01:57:45 -05:00
|
|
|
if (this.websocket.readyState === WebSocket.CONNECTING) {
|
2020-07-13 08:41:54 -04:00
|
|
|
await (new Promise((resolve) => {
|
2022-02-19 01:57:45 -05:00
|
|
|
this.websocket.onopen = function() {
|
2020-07-12 05:02:08 -04:00
|
|
|
resolve(true);
|
2020-07-13 08:41:54 -04:00
|
|
|
};
|
2020-07-12 05:02:08 -04:00
|
|
|
|
2022-02-19 01:57:45 -05:00
|
|
|
this.websocket.onerror = function() {
|
2020-07-12 05:02:08 -04:00
|
|
|
resolve(false);
|
2020-07-13 08:41:54 -04:00
|
|
|
};
|
|
|
|
}));
|
2020-07-12 05:02:08 -04:00
|
|
|
}
|
|
|
|
|
2020-07-13 09:27:08 -04:00
|
|
|
// Hangup
|
|
|
|
// See: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
2022-02-19 01:57:45 -05:00
|
|
|
this.websocket.close(1000);
|
2020-07-12 05:02:08 -04:00
|
|
|
}
|
2020-03-12 18:53:34 +01:00
|
|
|
}
|