Refactor Provider.

This commit is contained in:
Richard Moore 2018-06-18 18:49:00 -04:00
parent cf79190175
commit 336df72e04
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
8 changed files with 15 additions and 59 deletions

@ -1,5 +1,5 @@
import { checkTransactionResponse, Provider, TransactionRequest } from './provider'; import { BlockTag, checkTransactionResponse, Provider, TransactionRequest, TransactionResponse } from './provider';
import { Networkish } from './networks'; import { Networkish } from './networks';
import { hexlify, hexStripZeros } from '../utils/bytes'; import { hexlify, hexStripZeros } from '../utils/bytes';
@ -251,32 +251,33 @@ export class EtherscanProvider extends Provider{
return super.perform(method, params); return super.perform(method, params);
} }
getHistory(addressOrName, startBlock, endBlock) { // @TODO: Allow startBlock and endBlock to be Promises
getHistory(addressOrName: string | Promise<string>, startBlock?: BlockTag, endBlock?: BlockTag): Promise<Array<TransactionResponse>> {
var url = this.baseUrl; let url = this.baseUrl;
var apiKey = ''; let apiKey = '';
if (this.apiKey) { apiKey += '&apikey=' + this.apiKey; } if (this.apiKey) { apiKey += '&apikey=' + this.apiKey; }
if (startBlock == null) { startBlock = 0; } if (startBlock == null) { startBlock = 0; }
if (endBlock == null) { endBlock = 99999999; } 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 += '/api?module=account&action=txlist&address=' + address;
url += '&startblock=' + startBlock; url += '&startblock=' + startBlock;
url += '&endblock=' + endBlock; url += '&endblock=' + endBlock;
url += '&sort=asc' + apiKey; url += '&sort=asc' + apiKey;
return fetchJson(url, null, getResult).then(function(result) { return fetchJson(url, null, getResult).then((result) => {
var output = []; var output: Array<TransactionResponse> = [];
result.forEach(function(tx) { result.forEach((tx) => {
['contractAddress', 'to'].forEach(function(key) { ['contractAddress', 'to'].forEach(function(key) {
if (tx[key] == '') { delete tx[key]; } if (tx[key] == '') { delete tx[key]; }
}); });
if (tx.creates == null && tx.contractAddress != null) { if (tx.creates == null && tx.contractAddress != null) {
tx.creates = tx.contractAddress; tx.creates = tx.contractAddress;
} }
var item = checkTransactionResponse(tx); let item = checkTransactionResponse(tx);
if (tx.timeStamp) { item.timestamp = parseInt(tx.timeStamp); } if (tx.timeStamp) { item.timestamp = parseInt(tx.timeStamp); }
output.push(item); output.push(item);
}); });

@ -65,7 +65,7 @@ function getLowerCase(value: string): string {
export class JsonRpcSigner extends Signer { export class JsonRpcSigner extends Signer {
readonly provider: JsonRpcProvider; readonly provider: JsonRpcProvider;
readonly _address: string; private _address: string;
constructor(provider: JsonRpcProvider, address?: string) { constructor(provider: JsonRpcProvider, address?: string) {
super(); super();

@ -630,26 +630,6 @@ export class ProviderSigner extends Signer {
}); });
}); });
} }
estimateGas(transaction: TransactionRequest): Promise<BigNumber> {
transaction = shallowCopy(transaction);
if (transaction.from == null) {
transaction.from = this.getAddress();
}
return this.provider.estimateGas(transaction);
}
call(transaction: TransactionRequest): Promise<string> {
transaction = shallowCopy(transaction);
if (transaction.from == null) {
transaction.from = this.getAddress();
}
return this.provider.call(transaction);
}
} }
export class Provider { export class Provider {
@ -1349,13 +1329,3 @@ function(child) {
} }
}); });
*/ */
//defineProperty(Provider, 'networks', networks);
/*
defineProperty(Provider, '_formatters', {
checkTransactionResponse: checkTransaction
});
module.exports = Provider;
*/

@ -1,9 +1,9 @@
import { Provider } from './provider'; import { BlockTag, Provider, TransactionResponse } from './provider';
import { Networkish } from './networks'; import { Networkish } from './networks';
export declare class EtherscanProvider extends Provider { export declare class EtherscanProvider extends Provider {
readonly baseUrl: string; readonly baseUrl: string;
readonly apiKey: string; readonly apiKey: string;
constructor(network?: Networkish, apiKey?: string); constructor(network?: Networkish, apiKey?: string);
perform(method: string, params: any): Promise<any>; perform(method: string, params: any): Promise<any>;
getHistory(addressOrName: any, startBlock: any, endBlock: any): Promise<any[]>; getHistory(addressOrName: string | Promise<string>, startBlock?: BlockTag, endBlock?: BlockTag): Promise<Array<TransactionResponse>>;
} }

@ -243,6 +243,7 @@ var EtherscanProvider = /** @class */ (function (_super) {
} }
return _super.prototype.perform.call(this, method, params); return _super.prototype.perform.call(this, method, params);
}; };
// @TODO: Allow startBlock and endBlock to be Promises
EtherscanProvider.prototype.getHistory = function (addressOrName, startBlock, endBlock) { EtherscanProvider.prototype.getHistory = function (addressOrName, startBlock, endBlock) {
var url = this.baseUrl; var url = this.baseUrl;
var apiKey = ''; var apiKey = '';

@ -7,7 +7,7 @@ import { ConnectionInfo } from '../utils/web';
export declare function hexlifyTransaction(transaction: TransactionRequest): any; export declare function hexlifyTransaction(transaction: TransactionRequest): any;
export declare class JsonRpcSigner extends Signer { export declare class JsonRpcSigner extends Signer {
readonly provider: JsonRpcProvider; readonly provider: JsonRpcProvider;
readonly _address: string; private _address;
constructor(provider: JsonRpcProvider, address?: string); constructor(provider: JsonRpcProvider, address?: string);
readonly address: string; readonly address: string;
getAddress(): Promise<string>; getAddress(): Promise<string>;

@ -73,8 +73,6 @@ export declare class ProviderSigner extends Signer {
getAddress(): Promise<string>; getAddress(): Promise<string>;
signMessage(message: Arrayish | string): Promise<string>; signMessage(message: Arrayish | string): Promise<string>;
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse>; sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse>;
estimateGas(transaction: TransactionRequest): Promise<BigNumber>;
call(transaction: TransactionRequest): Promise<string>;
} }
export declare class Provider { export declare class Provider {
private _network; private _network;

@ -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; return ProviderSigner;
}(wallet_1.Signer)); }(wallet_1.Signer));
exports.ProviderSigner = ProviderSigner; exports.ProviderSigner = ProviderSigner;