Skip to content

Commit 3a19f43

Browse files
committed
Added "debug" event for providers; do not depend on the format as it may change, but this should help debugging in most cases (ethers-io#320).
1 parent 4852e83 commit 3a19f43

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src.ts/providers/etherscan-provider.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,43 +113,55 @@ export class EtherscanProvider extends BaseProvider{
113113
let apiKey = '';
114114
if (this.apiKey) { apiKey += '&apikey=' + this.apiKey; }
115115

116+
let get = (url: string, procFunc?: (value: any) => any) => {
117+
return fetchJson(url, null, procFunc || getJsonResult).then((result) => {
118+
this.emit('debug', {
119+
action: 'perform',
120+
request: url,
121+
response: result,
122+
provider: this
123+
});
124+
return result;
125+
});
126+
};
127+
116128
switch (method) {
117129
case 'getBlockNumber':
118130
url += '/api?module=proxy&action=eth_blockNumber' + apiKey;
119-
return fetchJson(url, null, getJsonResult);
131+
return get(url);
120132

121133
case 'getGasPrice':
122134
url += '/api?module=proxy&action=eth_gasPrice' + apiKey;
123-
return fetchJson(url, null, getJsonResult);
135+
return get(url);
124136

125137
case 'getBalance':
126138
// Returns base-10 result
127139
url += '/api?module=account&action=balance&address=' + params.address;
128140
url += '&tag=' + params.blockTag + apiKey;
129-
return fetchJson(url, null, getResult);
141+
return get(url, getResult);
130142

131143
case 'getTransactionCount':
132144
url += '/api?module=proxy&action=eth_getTransactionCount&address=' + params.address;
133145
url += '&tag=' + params.blockTag + apiKey;
134-
return fetchJson(url, null, getJsonResult);
146+
return get(url);
135147

136148

137149
case 'getCode':
138150
url += '/api?module=proxy&action=eth_getCode&address=' + params.address;
139151
url += '&tag=' + params.blockTag + apiKey;
140-
return fetchJson(url, null, getJsonResult);
152+
return get(url, getJsonResult);
141153

142154
case 'getStorageAt':
143155
url += '/api?module=proxy&action=eth_getStorageAt&address=' + params.address;
144156
url += '&position=' + params.position;
145157
url += '&tag=' + params.blockTag + apiKey;
146-
return fetchJson(url, null, getJsonResult);
158+
return get(url, getJsonResult);
147159

148160

149161
case 'sendTransaction':
150162
url += '/api?module=proxy&action=eth_sendRawTransaction&hex=' + params.signedTransaction;
151163
url += apiKey;
152-
return fetchJson(url, null, getJsonResult).catch((error) => {
164+
return get(url).catch((error) => {
153165
if (error.responseText) {
154166
// "Insufficient funds. The account you tried to send transaction from does not have enough funds. Required 21464000000000 and got: 0"
155167
if (error.responseText.toLowerCase().indexOf('insufficient funds') >= 0) {
@@ -176,19 +188,19 @@ export class EtherscanProvider extends BaseProvider{
176188
url += '&boolean=false';
177189
}
178190
url += apiKey;
179-
return fetchJson(url, null, getJsonResult);
191+
return get(url);
180192
}
181193
throw new Error('getBlock by blockHash not implmeneted');
182194

183195
case 'getTransaction':
184196
url += '/api?module=proxy&action=eth_getTransactionByHash&txhash=' + params.transactionHash;
185197
url += apiKey;
186-
return fetchJson(url, null, getJsonResult);
198+
return get(url);
187199

188200
case 'getTransactionReceipt':
189201
url += '/api?module=proxy&action=eth_getTransactionReceipt&txhash=' + params.transactionHash;
190202
url += apiKey;
191-
return fetchJson(url, null, getJsonResult);
203+
return get(url);
192204

193205

194206
case 'call': {
@@ -200,15 +212,15 @@ export class EtherscanProvider extends BaseProvider{
200212
throw new Error('EtherscanProvider does not support blockTag for call');
201213
}
202214
url += apiKey;
203-
return fetchJson(url, null, getJsonResult);
215+
return get(url);
204216
}
205217

206218
case 'estimateGas': {
207219
let transaction = getTransactionString(params.transaction);
208220
if (transaction) { transaction = '&' + transaction; }
209221
url += '/api?module=proxy&action=eth_estimateGas&' + transaction;
210222
url += apiKey;
211-
return fetchJson(url, null, getJsonResult);
223+
return get(url);
212224
}
213225

214226
case 'getLogs':
@@ -244,7 +256,7 @@ export class EtherscanProvider extends BaseProvider{
244256
url += apiKey;
245257

246258
var self = this;
247-
return fetchJson(url, null, getResult).then(function(logs: Array<any>) {
259+
return get(url, getResult).then(function(logs: Array<any>) {
248260
var txs: { [hash: string]: string } = {};
249261

250262
var seq = Promise.resolve();
@@ -272,7 +284,7 @@ export class EtherscanProvider extends BaseProvider{
272284
if (this.network.name !== 'homestead') { return Promise.resolve(0.0); }
273285
url += '/api?module=stats&action=ethprice';
274286
url += apiKey;
275-
return fetchJson(url, null, getResult).then(function(result) {
287+
return get(url, getResult).then(function(result) {
276288
return parseFloat(result.ethusd);
277289
});
278290

@@ -301,6 +313,12 @@ export class EtherscanProvider extends BaseProvider{
301313
url += '&sort=asc' + apiKey;
302314

303315
return fetchJson(url, null, getResult).then((result: Array<any>) => {
316+
this.emit('debug', {
317+
action: 'getHistory',
318+
request: url,
319+
response: result,
320+
provider: this
321+
});
304322
var output: Array<TransactionResponse> = [];
305323
result.forEach((tx) => {
306324
['contractAddress', 'to'].forEach(function(key) {

src.ts/providers/json-rpc-provider.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,22 @@ export class JsonRpcProvider extends BaseProvider {
230230
}
231231

232232
send(method: string, params: any): Promise<any> {
233-
var request = {
233+
let request = {
234234
method: method,
235235
params: params,
236236
id: 42,
237237
jsonrpc: "2.0"
238238
};
239239

240-
return fetchJson(this.connection, JSON.stringify(request), getResult);
240+
return fetchJson(this.connection, JSON.stringify(request), getResult).then((result) => {
241+
this.emit('debug', {
242+
action: 'send',
243+
request: request,
244+
response: result,
245+
provider: this
246+
});
247+
return result;
248+
});
241249
}
242250

243251
perform(method: string, params: any): Promise<any> {

0 commit comments

Comments
 (0)