Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions dist/ethers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11039,6 +11039,15 @@ var BaseProvider = /** @class */ (function (_super) {
});
});
};
BaseProvider.prototype.getEtherPriceBtc = function () {
var _this = this;
return this.ready.then(function () {
return _this.perform('getEtherPriceBtc', {}).then(function (result) {
// @TODO: Check valid float
return result;
});
});
};
// @TODO: Could probably use resolveProperties instead?
BaseProvider.prototype._resolveNames = function (object, keys) {
var promises = [];
Expand Down Expand Up @@ -11567,6 +11576,15 @@ var EtherscanProvider = /** @class */ (function (_super) {
return get(url, getResult).then(function (result) {
return parseFloat(result.ethusd);
});
case 'getEtherPriceBtc':
if (this.network.name !== 'homestead') {
return Promise.resolve(0.0);
}
url += '/api?module=stats&action=ethprice';
url += apiKey;
return get(url, getResult).then(function (result) {
return parseFloat(result.ethbtc);
});
default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/ethers.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ethers.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/ethers.types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ declare module 'ethers/providers/base-provider' {
getTransactionReceipt(transactionHash: string): Promise<TransactionReceipt>;
getLogs(filter: Filter | FilterByBlock): Promise<Array<Log>>;
getEtherPrice(): Promise<number>;
getEtherPriceBtc(): Promise<number>;
resolveName(name: string | Promise<string>): Promise<string>;
lookupAddress(address: string | Promise<string>): Promise<string>;
static checkTransactionResponse(transaction: any): TransactionResponse;
Expand Down
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions providers/base-provider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export declare class BaseProvider extends Provider {
getTransactionReceipt(transactionHash: string): Promise<TransactionReceipt>;
getLogs(filter: Filter | FilterByBlock): Promise<Array<Log>>;
getEtherPrice(): Promise<number>;
getEtherPriceBtc(): Promise<number>;
private _resolveNames;
private _getResolver;
resolveName(name: string | Promise<string>): Promise<string>;
Expand Down
9 changes: 9 additions & 0 deletions providers/base-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,15 @@ var BaseProvider = /** @class */ (function (_super) {
});
});
};
BaseProvider.prototype.getEtherPriceBtc = function () {
var _this = this;
return this.ready.then(function () {
return _this.perform('getEtherPriceBtc', {}).then(function (result) {
// @TODO: Check valid float
return result;
});
});
};
// @TODO: Could probably use resolveProperties instead?
BaseProvider.prototype._resolveNames = function (object, keys) {
var promises = [];
Expand Down
9 changes: 9 additions & 0 deletions providers/etherscan-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ var EtherscanProvider = /** @class */ (function (_super) {
return get(url, getResult).then(function (result) {
return parseFloat(result.ethusd);
});
case 'getEtherPriceBtc':
if (this.network.name !== 'homestead') {
return Promise.resolve(0.0);
}
url += '/api?module=stats&action=ethprice';
url += apiKey;
return get(url, getResult).then(function (result) {
return parseFloat(result.ethbtc);
});
default:
break;
}
Expand Down
9 changes: 9 additions & 0 deletions src.ts/providers/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,15 @@ export class BaseProvider extends Provider {
});
}

getEtherPriceBtc(): Promise<number> {
return this.ready.then(() => {
return this.perform('getEtherPriceBtc', {}).then((result) => {
// @TODO: Check valid float
return result;
});
});
}

// @TODO: Could probably use resolveProperties instead?
private _resolveNames(object: any, keys: Array<string>): Promise<{ [key: string]: string }> {
let promises: Array<Promise<void>> = [];
Expand Down
38 changes: 23 additions & 15 deletions src.ts/providers/etherscan-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function getResult(result: { status?: number, message?: string, result?: any }):
return result.result;
}

function getJsonResult(result: { jsonrpc: string, result?: any, error?: { code?: number, data?: any, message?: string} } ): any {
function getJsonResult(result: { jsonrpc: string, result?: any, error?: { code?: number, data?: any, message?: string } }): any {
if (result.jsonrpc != '2.0') {
// @TODO: not any
let error: any = new Error('invalid response');
Expand Down Expand Up @@ -74,7 +74,7 @@ function checkLogTag(blockTag: string): number | "latest" {
}


export class EtherscanProvider extends BaseProvider{
export class EtherscanProvider extends BaseProvider {
readonly baseUrl: string;
readonly apiKey: string;
constructor(network?: Networkish, apiKey?: string) {
Expand All @@ -85,7 +85,7 @@ export class EtherscanProvider extends BaseProvider{
if (this.network) { name = this.network.name; }

let baseUrl = null;
switch(name) {
switch (name) {
case 'homestead':
baseUrl = 'https://api.etherscan.io';
break;
Expand Down Expand Up @@ -168,15 +168,15 @@ export class EtherscanProvider extends BaseProvider{
if (error.responseText) {
// "Insufficient funds. The account you tried to send transaction from does not have enough funds. Required 21464000000000 and got: 0"
if (error.responseText.toLowerCase().indexOf('insufficient funds') >= 0) {
errors.throwError('insufficient funds', errors.INSUFFICIENT_FUNDS, { });
errors.throwError('insufficient funds', errors.INSUFFICIENT_FUNDS, {});
}
// "Transaction with the same hash was already imported."
if (error.responseText.indexOf('same hash was already imported') >= 0) {
errors.throwError('nonce has already been used', errors.NONCE_EXPIRED, { });
errors.throwError('nonce has already been used', errors.NONCE_EXPIRED, {});
}
// "Transaction gas price is too low. There is another transaction with same nonce in the queue. Try increasing the gas price or incrementing the nonce."
if (error.responseText.indexOf('another transaction with same nonce') >= 0) {
errors.throwError('replacement fee too low', errors.REPLACEMENT_UNDERPRICED, { });
errors.throwError('replacement fee too low', errors.REPLACEMENT_UNDERPRICED, {});
}
}
throw error;
Expand Down Expand Up @@ -257,7 +257,7 @@ export class EtherscanProvider extends BaseProvider{
throw new Error('unsupported topic format');
}
let topic0 = params.filter.topics[0];
if (typeof(topic0) !== 'string' || topic0.length !== 66) {
if (typeof (topic0) !== 'string' || topic0.length !== 66) {
throw new Error('unsupported topic0 format');
}
url += '&topic0=' + topic0;
Expand All @@ -269,16 +269,16 @@ export class EtherscanProvider extends BaseProvider{
url += apiKey;

var self = this;
return get(url, getResult).then(function(logs: Array<any>) {
return get(url, getResult).then(function (logs: Array<any>) {
var txs: { [hash: string]: string } = {};

var seq = Promise.resolve();
logs.forEach(function(log) {
seq = seq.then(function() {
logs.forEach(function (log) {
seq = seq.then(function () {
if (log.blockHash != null) { return null; }
log.blockHash = txs[log.transactionHash];
if (log.blockHash == null) {
return self.getTransaction(log.transactionHash).then(function(tx) {
return self.getTransaction(log.transactionHash).then(function (tx) {
txs[log.transactionHash] = tx.blockHash;
log.blockHash = tx.blockHash;
return null;
Expand All @@ -288,7 +288,7 @@ export class EtherscanProvider extends BaseProvider{
});
});

return seq.then(function() {
return seq.then(function () {
return logs;
});
});
Expand All @@ -297,13 +297,21 @@ export class EtherscanProvider extends BaseProvider{
if (this.network.name !== 'homestead') { return Promise.resolve(0.0); }
url += '/api?module=stats&action=ethprice';
url += apiKey;
return get(url, getResult).then(function(result) {
return get(url, getResult).then(function (result) {
return parseFloat(result.ethusd);
});

case 'getEtherPriceBtc':
if (this.network.name !== 'homestead') { return Promise.resolve(0.0); }
url += '/api?module=stats&action=ethprice';
url += apiKey;
return get(url, getResult).then(function (result) {
return parseFloat(result.ethbtc);
});

default:
break;
}
}

return super.perform(method, params);
}
Expand Down Expand Up @@ -334,7 +342,7 @@ export class EtherscanProvider extends BaseProvider{
});
var output: Array<TransactionResponse> = [];
result.forEach((tx) => {
['contractAddress', 'to'].forEach(function(key) {
['contractAddress', 'to'].forEach(function (key) {
if (tx[key] == '') { delete tx[key]; }
});
if (tx.creates == null && tx.contractAddress != null) {
Expand Down
Loading