Added getEtherPrice and getHistory calls for Etherscan Provider.

This commit is contained in:
Richard Moore 2017-12-29 21:01:43 -05:00
parent 43061c691b
commit a568be9df3
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
2 changed files with 61 additions and 2 deletions

@ -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;;

@ -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');
});
});
});