Etherscan now supports estiateGas and getGasPrice (thanks Matt!).

This commit is contained in:
ricmoo 2016-08-05 02:56:24 -04:00
parent 0a38d14930
commit a4e1f531b7
3 changed files with 91 additions and 66 deletions

@ -197,7 +197,6 @@ function getGasPrice(value) {
if (!value || !value.transactions || value.transactions.length === 0) {
throw new Error('invalid response');
}
console.log(value.transactions[0].gasPrice)
return hexToBN(value.transactions[0].gasPrice);
}
@ -275,11 +274,8 @@ utils.defineProperty(EtherscanProvider.prototype, 'getTransactionCount', functio
});
utils.defineProperty(EtherscanProvider.prototype, 'getGasPrice', function() {
/* This doesn't work, over-estimates gas if current block was anxious
var query = ('module=proxy&action=eth_getBlockByNumber&tag=latest&boolean=true');
return this._send(query, getGasPrice);
*/
throw new Error('etherscan does not support gasPrice');
var query = ('module=proxy&action=eth_gasPrice');
return this._send(query, hexToBN);
});
utils.defineProperty(EtherscanProvider.prototype, 'sendTransaction', function(signedTransaction) {
@ -297,7 +293,25 @@ utils.defineProperty(EtherscanProvider.prototype, 'call', function(transaction)
});
utils.defineProperty(EtherscanProvider.prototype, 'estimateGas', function(transaction) {
throw new Error('etherscan does not support estimation');
var address = SigningKey.getAddress(transaction.to);
var query = 'module=proxy&action=eth_estimateGas&to=' + address;
if (transaction.gasPrice) {
query += '&gasPrice=' + utils.hexlify(transaction.gasPrice);
}
if (transaction.gasLimit) {
query += '&gas=' + utils.hexlify(transaction.gasLimit);
}
if (transaction.from) {
query += '&from=' + SigningKey.getAddress(transaction.from);
}
if (transaction.data) {
query += '&data=' + ensureHex(transaction.data);
}
if (transaction.value) {
query += '&value=' + utils.hexlify(transaction.value);
}
return this._send(query, hexToBN);
});

@ -42,7 +42,7 @@ module.exports.testSolidityCoder = require('./test-solidity-coder.js');
// Test contract address helper
module.exports.testSolidityCoder = require('./test-contract-address.js');
// Test the providers API (this test case needs a little work as it
// needs to modify the blockchain for a full test)
//module.exports.testSolidityCoder = require('./test-providers.js');
// Test the providers API (we still need to add a lot ore test cases here)
module.exports.testSolidityCoder = require('./test-providers.js');

@ -3,68 +3,79 @@ var Wallet = require('../index.js');
var Web3 = require('web3');
// @TODO: We need to do a lot more test cases here:
// - homestead
// - sendTransaction
// - estimateGas with various parameters set and not set
// - estimateGas on a contract with from/value conditionals
// - Metamask-style injected Web3
module.exports = function(test) {
var pending = [];
var otherGasPrice = null;
function checkGasPrice(value) {
if (otherGasPrice === null) {
otherGasPrice = value;
return true;
}
var same = otherGasPrice.eq(value);
if (!same) {
console.log('NOTE: This test case may have false positives; try again');
}
return same;
}
function checkMethod(provider, method, params, expectedValue) {
pending.push(new Promise(function(resolve, reject) {
provider[method].apply(provider, params).then(function(value) {
//console.log(provider, method, expectedValue, value);
if (expectedValue instanceof Wallet.utils.BN) {
test.ok(expectedValue.eq(value), 'Failed ' + method);
} else if (typeof(expectedValue) === 'function') {
test.ok(expectedValue(value), 'Failed ' + method);
} else {
test.equal(value, expectedValue, 'Failed ' + method);
}
resolve();
}, function(error) {
test.ok(false, 'Error - ' + error.message);
reject(error);
});
}));
}
function check(provider) {
checkMethod(
provider,
'getBalance', ['0x7357589f8e367c2C31F51242fB77B350A11830F3'],
new Wallet.utils.BN('436095820614148588744')
);
checkMethod(
provider,
'getTransactionCount', ['0x7357589f8e367c2C31F51242fB77B350A11830F3'],
1048598
);
// checkMethod(provider, 'getGasPrice', [], checkGasPrice);
checkMethod(
provider,
'call', [{to: '0xdfaf84077cF4bCECA4F79d167F47041Ed3006D5b', data: '0x20965255'}],
'0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000'
);
}
var url = 'http://localhost:8545';
var web3Provider = new Web3.providers.HttpProvider(url)
var web3 = new Web3(web3Provider)
check(new Wallet.providers.Web3Provider(web3Provider));
check(new Wallet.providers.Web3Provider(web3));
check(new Wallet.providers.HttpProvider(url));
check(new Wallet.providers.EtherscanProvider({testnet: true}));
var providers = [
(new Wallet.providers.Web3Provider(web3Provider)),
(new Wallet.providers.Web3Provider(web3)),
(new Wallet.providers.HttpProvider(url)),
(new Wallet.providers.EtherscanProvider({testnet: true})),
]
var pending = [];
function checkMethod(method, params, expectedValue) {
var checks = [];
providers.forEach(function(provider) {
checks.push(new Promise(function(resolve, reject) {
provider[method].apply(provider, params).then(function(value) {
resolve(value);
}, function(error) {
reject(error);
});
}));
});
pending.push(new Promise(function(resolve, reject) {
Promise.all(checks).then(function(results) {
if (!expectedValue) { expectedValue = results[0]; }
results.forEach(function(value) {
if (expectedValue instanceof Wallet.utils.BN) {
test.ok(expectedValue.eq(value), 'Failed ' + method);
} else if (typeof(expectedValue) === 'function') {
test.ok(expectedValue(value), 'Failed ' + method);
} else {
test.equal(value, expectedValue, 'Failed ' + method);
}
});
resolve();
}, function(error) {
console.log(error);
test.ok(false, 'Error - ' + error.message)
});
}));
}
checkMethod(
'getBalance', ['0x7357589f8e367c2C31F51242fB77B350A11830F3']
);
checkMethod(
'getTransactionCount', ['0x7357589f8e367c2C31F51242fB77B350A11830F3']
);
checkMethod('getGasPrice', []);
checkMethod(
'call', [{to: '0xdfaf84077cF4bCECA4F79d167F47041Ed3006D5b', data: '0x20965255'}],
'0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000'
);
checkMethod(
'estimateGas', [{
to: '0xdfaf84077cF4bCECA4F79d167F47041Ed3006D5b',
from: '0x7357589f8e367c2C31F51242fB77B350A11830F3',
data: '0x93a0935200000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003666f6f0000000000000000000000000000000000000000000000000000000000'
}],
new Wallet.utils.BN('35588')
);
Promise.all(pending).then(function(results) {
test.done();