From 336df72e0461f95ac72bfb5d69b92e69ebb631ad Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Mon, 18 Jun 2018 18:49:00 -0400 Subject: [PATCH] Refactor Provider. --- src.ts/providers/etherscan-provider.ts | 19 ++++++++-------- src.ts/providers/json-rpc-provider.ts | 2 +- src.ts/providers/provider.ts | 30 -------------------------- src/providers/etherscan-provider.d.ts | 4 ++-- src/providers/etherscan-provider.js | 1 + src/providers/json-rpc-provider.d.ts | 2 +- src/providers/provider.d.ts | 2 -- src/providers/provider.js | 14 ------------ 8 files changed, 15 insertions(+), 59 deletions(-) diff --git a/src.ts/providers/etherscan-provider.ts b/src.ts/providers/etherscan-provider.ts index ce3b83fff..9e27300da 100644 --- a/src.ts/providers/etherscan-provider.ts +++ b/src.ts/providers/etherscan-provider.ts @@ -1,5 +1,5 @@ -import { checkTransactionResponse, Provider, TransactionRequest } from './provider'; +import { BlockTag, checkTransactionResponse, Provider, TransactionRequest, TransactionResponse } from './provider'; import { Networkish } from './networks'; import { hexlify, hexStripZeros } from '../utils/bytes'; @@ -251,32 +251,33 @@ export class EtherscanProvider extends Provider{ return super.perform(method, params); } - getHistory(addressOrName, startBlock, endBlock) { + // @TODO: Allow startBlock and endBlock to be Promises + getHistory(addressOrName: string | Promise, startBlock?: BlockTag, endBlock?: BlockTag): Promise> { - var url = this.baseUrl; + let url = this.baseUrl; - var apiKey = ''; + let apiKey = ''; if (this.apiKey) { apiKey += '&apikey=' + this.apiKey; } if (startBlock == null) { startBlock = 0; } if (endBlock == null) { endBlock = 99999999; } - return this.resolveName(addressOrName).then(function(address) { + return this.resolveName(addressOrName).then((address) => { url += '/api?module=account&action=txlist&address=' + address; url += '&startblock=' + startBlock; url += '&endblock=' + endBlock; url += '&sort=asc' + apiKey; - return fetchJson(url, null, getResult).then(function(result) { - var output = []; - result.forEach(function(tx) { + return fetchJson(url, null, getResult).then((result) => { + var output: Array = []; + result.forEach((tx) => { ['contractAddress', 'to'].forEach(function(key) { if (tx[key] == '') { delete tx[key]; } }); if (tx.creates == null && tx.contractAddress != null) { tx.creates = tx.contractAddress; } - var item = checkTransactionResponse(tx); + let item = checkTransactionResponse(tx); if (tx.timeStamp) { item.timestamp = parseInt(tx.timeStamp); } output.push(item); }); diff --git a/src.ts/providers/json-rpc-provider.ts b/src.ts/providers/json-rpc-provider.ts index 0d8309b32..9b0be3cbc 100644 --- a/src.ts/providers/json-rpc-provider.ts +++ b/src.ts/providers/json-rpc-provider.ts @@ -65,7 +65,7 @@ function getLowerCase(value: string): string { export class JsonRpcSigner extends Signer { readonly provider: JsonRpcProvider; - readonly _address: string; + private _address: string; constructor(provider: JsonRpcProvider, address?: string) { super(); diff --git a/src.ts/providers/provider.ts b/src.ts/providers/provider.ts index 38bb83dd1..1d7d35ccd 100644 --- a/src.ts/providers/provider.ts +++ b/src.ts/providers/provider.ts @@ -630,26 +630,6 @@ export class ProviderSigner extends Signer { }); }); } - - estimateGas(transaction: TransactionRequest): Promise { - transaction = shallowCopy(transaction); - - if (transaction.from == null) { - transaction.from = this.getAddress(); - } - - return this.provider.estimateGas(transaction); - } - - call(transaction: TransactionRequest): Promise { - transaction = shallowCopy(transaction); - - if (transaction.from == null) { - transaction.from = this.getAddress(); - } - - return this.provider.call(transaction); - } } export class Provider { @@ -1349,13 +1329,3 @@ function(child) { } }); */ - -//defineProperty(Provider, 'networks', networks); - -/* -defineProperty(Provider, '_formatters', { - checkTransactionResponse: checkTransaction -}); - -module.exports = Provider; -*/ diff --git a/src/providers/etherscan-provider.d.ts b/src/providers/etherscan-provider.d.ts index 4e5f0db15..d3da1d728 100644 --- a/src/providers/etherscan-provider.d.ts +++ b/src/providers/etherscan-provider.d.ts @@ -1,9 +1,9 @@ -import { Provider } from './provider'; +import { BlockTag, Provider, TransactionResponse } from './provider'; import { Networkish } from './networks'; export declare class EtherscanProvider extends Provider { readonly baseUrl: string; readonly apiKey: string; constructor(network?: Networkish, apiKey?: string); perform(method: string, params: any): Promise; - getHistory(addressOrName: any, startBlock: any, endBlock: any): Promise; + getHistory(addressOrName: string | Promise, startBlock?: BlockTag, endBlock?: BlockTag): Promise>; } diff --git a/src/providers/etherscan-provider.js b/src/providers/etherscan-provider.js index f558262c9..697a5f896 100644 --- a/src/providers/etherscan-provider.js +++ b/src/providers/etherscan-provider.js @@ -243,6 +243,7 @@ var EtherscanProvider = /** @class */ (function (_super) { } return _super.prototype.perform.call(this, method, params); }; + // @TODO: Allow startBlock and endBlock to be Promises EtherscanProvider.prototype.getHistory = function (addressOrName, startBlock, endBlock) { var url = this.baseUrl; var apiKey = ''; diff --git a/src/providers/json-rpc-provider.d.ts b/src/providers/json-rpc-provider.d.ts index 558fd84da..4dea12beb 100644 --- a/src/providers/json-rpc-provider.d.ts +++ b/src/providers/json-rpc-provider.d.ts @@ -7,7 +7,7 @@ import { ConnectionInfo } from '../utils/web'; export declare function hexlifyTransaction(transaction: TransactionRequest): any; export declare class JsonRpcSigner extends Signer { readonly provider: JsonRpcProvider; - readonly _address: string; + private _address; constructor(provider: JsonRpcProvider, address?: string); readonly address: string; getAddress(): Promise; diff --git a/src/providers/provider.d.ts b/src/providers/provider.d.ts index ed6349e0e..db4979f8b 100644 --- a/src/providers/provider.d.ts +++ b/src/providers/provider.d.ts @@ -73,8 +73,6 @@ export declare class ProviderSigner extends Signer { getAddress(): Promise; signMessage(message: Arrayish | string): Promise; sendTransaction(transaction: TransactionRequest): Promise; - estimateGas(transaction: TransactionRequest): Promise; - call(transaction: TransactionRequest): Promise; } export declare class Provider { private _network; diff --git a/src/providers/provider.js b/src/providers/provider.js index c9afb16d4..fb7fbff83 100644 --- a/src/providers/provider.js +++ b/src/providers/provider.js @@ -492,20 +492,6 @@ var ProviderSigner = /** @class */ (function (_super) { }); }); }; - ProviderSigner.prototype.estimateGas = function (transaction) { - transaction = properties_1.shallowCopy(transaction); - if (transaction.from == null) { - transaction.from = this.getAddress(); - } - return this.provider.estimateGas(transaction); - }; - ProviderSigner.prototype.call = function (transaction) { - transaction = properties_1.shallowCopy(transaction); - if (transaction.from == null) { - transaction.from = this.getAddress(); - } - return this.provider.call(transaction); - }; return ProviderSigner; }(wallet_1.Signer)); exports.ProviderSigner = ProviderSigner;