From a568be9df3ebb3819c880ef5d9c0c374d097e767 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Fri, 29 Dec 2017 21:01:43 -0500 Subject: [PATCH] Added getEtherPrice and getHistory calls for Etherscan Provider. --- providers/etherscan-provider.js | 40 +++++++++++++++++++++++++++++++++ tests/test-providers.js | 23 +++++++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/providers/etherscan-provider.js b/providers/etherscan-provider.js index 99b0474bb..b7a50a369 100644 --- a/providers/etherscan-provider.js +++ b/providers/etherscan-provider.js @@ -218,6 +218,14 @@ utils.defineProperty(EtherscanProvider.prototype, 'perform', function(method, pa url += apiKey; return Provider.fetchJSON(url, null, getResult); + case 'getEtherPrice': + if (this.name !== 'homestead') { return Promise.resolve(0.0); } + url += '/api?module=stats&action=ethprice'; + url += apiKey; + return Provider.fetchJSON(url, null, getResult).then(function(result) { + return parseFloat(result.ethusd); + }); + default: break; } @@ -225,4 +233,36 @@ utils.defineProperty(EtherscanProvider.prototype, 'perform', function(method, pa return Promise.reject(new Error('not implemented - ' + method)); }); +utils.defineProperty(EtherscanProvider.prototype, 'getHistory', function(addressOrName, startBlock, endBlock) { + + var url = this.baseUrl; + + var 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) { + url += '/api?module=account&action=txlist&address=' + address; + url += '&fromBlock=' + startBlock; + url += '&endBlock=' + endBlock; + url += '&sort=asc'; + + return Provider.fetchJSON(url, null, getResult).then(function(result) { + var output = []; + result.forEach(function(tx) { + ['contractAddress', 'to'].forEach(function(key) { + if (tx[key] == '') { delete tx[key]; } + }); + if (tx.creates == null && tx.contractAddress != null) { + tx.creates = tx.contractAddress; + } + output.push(Provider._formatters.checkTransactionResponse(tx)); + }); + return output; + }); + }); +}); + module.exports = EtherscanProvider;; diff --git a/tests/test-providers.js b/tests/test-providers.js index 6f03da3b2..94b3fcfcf 100644 --- a/tests/test-providers.js +++ b/tests/test-providers.js @@ -362,8 +362,8 @@ function testProvider(providerName, networkName) { //if (providerName === 'EtherscanProvider' && networkName !== 'homestead') { return; } // HACK! INFURA is flakey on homestead right now and the test cases are failing - console.log('WARNING: Test cases being skipped! Temporary. Please turn backon soon.'); - if (providerName === 'InfuraProvider' && networkName === 'homestead') { return; } + //console.log('WARNING: Test cases being skipped! Temporary. Please turn backon soon.'); + //if (providerName === 'InfuraProvider' && networkName === 'homestead') { return; } testProvider(providerName, networkName); }); @@ -660,3 +660,22 @@ describe('Test legacy provider arguments', function() { }); }); }); + +describe('Test extra Etherscan operations', function() { + var providers = require('../providers'); + var provider = new providers.EtherscanProvider(); + it('fethces the current price of ether', function() { + this.timeout(20000); + return provider.getEtherPrice().then(function(price) { + assert.ok(typeof(price) === 'number', 'Etherscan price returns a number'); + assert.ok(price > 0.0, 'Etherscan price returns non-zero'); + }); + }); + it('fetches the history', function() { + this.timeout(100000); + return provider.getHistory('ricmoo.firefly.eth').then(function(history) { + assert.ok(history.length > 40, 'Etherscan history returns results'); + assert.equal(history[0].hash, '0xd25f550cfdff90c086a6496a84dbb2c4577df15b1416e5b3319a3e4ebb5b25d8', 'Etherscan history returns correct transaction'); + }); + }); +});