Skip to content

Commit

Permalink
More consistent debug events for Providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Aug 1, 2019
1 parent 638d1fe commit e8f28b5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
23 changes: 18 additions & 5 deletions packages/providers/src.ts/etherscan-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BlockTag, TransactionRequest, TransactionResponse } from "@ethersprojec
import { hexlify, hexValue } from "@ethersproject/bytes";
import * as errors from "@ethersproject/errors";
import { Networkish } from "@ethersproject/networks";
import { defineReadOnly } from "@ethersproject/properties";
import { deepCopy, defineReadOnly } from "@ethersproject/properties";
import { fetchJson } from "@ethersproject/web";

import { BaseProvider } from "./base-provider";
Expand Down Expand Up @@ -112,11 +112,17 @@ export class EtherscanProvider extends BaseProvider{
if (this.apiKey) { apiKey += "&apikey=" + this.apiKey; }

let get = (url: string, procFunc?: (value: any) => any) => {
this.emit("debug", {
action: "request",
request: url,
provider: this
});

return fetchJson(url, null, procFunc || getJsonResult).then((result) => {
this.emit("debug", {
action: "perform",
action: "response",
request: url,
response: result,
response: deepCopy(result),
provider: this
});
return result;
Expand Down Expand Up @@ -310,13 +316,20 @@ export class EtherscanProvider extends BaseProvider{
url += "&endblock=" + endBlock;
url += "&sort=asc" + apiKey;

this.emit("debug", {
action: "request",
request: url,
provider: this
});

return fetchJson(url, null, getResult).then((result: Array<any>) => {
this.emit("debug", {
action: "getHistory",
action: "response",
request: url,
response: result,
response: deepCopy(result),
provider: this
});

let output: Array<TransactionResponse> = [];
result.forEach((tx) => {
["contractAddress", "to"].forEach(function(key) {
Expand Down
26 changes: 22 additions & 4 deletions packages/providers/src.ts/fallback-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as errors from "@ethersproject/errors";
import { Network } from "@ethersproject/networks";
import { shuffled } from "@ethersproject/random";
import { defineReadOnly } from "@ethersproject/properties";
import { deepCopy, defineReadOnly } from "@ethersproject/properties";

import { BaseProvider } from "./base-provider";

Expand Down Expand Up @@ -148,14 +148,32 @@ export class FallbackProvider extends BaseProvider {
run: () => {
let t0 = now();
let start = t0 - T0;
this.emit("debug", "perform", rid, { weight, start, provider, method, params });
this.emit("debug", {
action: "request",
rid: rid,
backend: { weight, start, provider },
request: { method: method, params: deepCopy(params) },
provider: this
});
return provider.perform(method, params).then((result) => {
let duration = now() - t0;
this.emit("debug", "result", rid, { duration, result });
this.emit("debug", {
action: "response",
rid: rid,
backend: { weight, start, duration, provider },
request: { method: method, params: deepCopy(params) },
response: deepCopy(result)
});
return { weight: weight, result: result };
}, (error) => {
let duration = now() - t0;
this.emit("debug", "error", rid, { duration, error });
this.emit("debug", {
action: "response",
rid: rid,
backend: { weight, start, duration, provider },
request: { method: method, params: deepCopy(params) },
error: error
});
return { weight: weight, error: error };
});
},
Expand Down
10 changes: 8 additions & 2 deletions packages/providers/src.ts/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BigNumber } from "@ethersproject/bignumber";
import { Bytes, hexlify, hexValue } from "@ethersproject/bytes";
import * as errors from "@ethersproject/errors";
import { getNetwork, Network, Networkish } from "@ethersproject/networks";
import { checkProperties, defineReadOnly, resolveProperties, shallowCopy } from "@ethersproject/properties";
import { checkProperties, deepCopy, defineReadOnly, resolveProperties, shallowCopy } from "@ethersproject/properties";
import { toUtf8Bytes } from "@ethersproject/strings";
import { ConnectionInfo, fetchJson, poll } from "@ethersproject/web";

Expand Down Expand Up @@ -283,9 +283,15 @@ export class JsonRpcProvider extends BaseProvider {
jsonrpc: "2.0"
};

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

return fetchJson(this.connection, JSON.stringify(request), getResult).then((result) => {
this.emit("debug", {
action: "send",
action: "response",
request: request,
response: result,
provider: this
Expand Down

0 comments on commit e8f28b5

Please sign in to comment.