Skip to content

Commit

Permalink
Added debug event to Web3Provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed May 14, 2021
1 parent 37a9c77 commit 26464c5
Showing 1 changed file with 67 additions and 9 deletions.
76 changes: 67 additions & 9 deletions packages/providers/src.ts/web3-provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import { Networkish } from "@ethersproject/networks";
import { defineReadOnly } from "@ethersproject/properties";
import { deepCopy, defineReadOnly } from "@ethersproject/properties";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
Expand All @@ -27,6 +27,8 @@ export type JsonRpcFetchFunc = (method: string, params?: Array<any>) => Promise<
type Web3LegacySend = (request: any, callback: (error: Error, response: any) => void) => void;

function buildWeb3LegacyFetcher(provider: ExternalProvider, sendFunc: Web3LegacySend) : JsonRpcFetchFunc {
const fetcher = "Web3LegacyFetcher";

return function(method: string, params: Array<any>): Promise<any> {

// Metamask complains about eth_sign (and on some versions hangs)
Expand All @@ -44,17 +46,43 @@ function buildWeb3LegacyFetcher(provider: ExternalProvider, sendFunc: Web3Legacy
};

return new Promise((resolve, reject) => {
sendFunc(request, function(error, result) {
if (error) { return reject(error); }
this.emit("debug", {
action: "request",
fetcher,
request: deepCopy(request),
provider: this
});

sendFunc(request, (error, response) => {

if (error) {
this.emit("debug", {
action: "response",
fetcher,
error,
request,
provider: this
});

if (result.error) {
const error = new Error(result.error.message);
(<any>error).code = result.error.code;
(<any>error).data = result.error.data;
return reject(error);
}

resolve(result.result);
this.emit("debug", {
action: "response",
fetcher,
request,
response,
provider: this
});

if (response.error) {
const error = new Error(response.error.message);
(<any>error).code = response.error.code;
(<any>error).data = response.error.data;
return reject(error);
}

resolve(response.result);
});
});
}
Expand All @@ -71,7 +99,37 @@ function buildEip1193Fetcher(provider: ExternalProvider): JsonRpcFetchFunc {
params = [ params[1], params[0] ];
}

return provider.request({ method, params });
const request = { method, params };

this.emit("debug", {
action: "request",
fetcher: "Eip1193Fetcher",
request: deepCopy(request),
provider: this
});

return provider.request(request).then((response) => {
this.emit("debug", {
action: "response",
fetcher: "Eip1193Fetcher",
request,
response,
provider: this
});

return response;

}, (error) => {
this.emit("debug", {
action: "response",
fetcher: "Eip1193Fetcher",
request,
error,
provider: this
});

throw error;
});
}
}

Expand Down

0 comments on commit 26464c5

Please sign in to comment.