2017-02-25 09:23:48 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC
|
|
|
|
|
|
|
|
var Provider = require('./provider.js');
|
|
|
|
|
|
|
|
var utils = (function() {
|
2017-07-06 03:26:03 +03:00
|
|
|
var convert = require('ethers-utils/convert');
|
2017-02-25 09:23:48 +03:00
|
|
|
return {
|
2017-04-05 00:28:25 +03:00
|
|
|
defineProperty: require('ethers-utils/properties').defineProperty,
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-07-06 03:26:03 +03:00
|
|
|
hexlify: convert.hexlify,
|
|
|
|
isHexString: convert.isHexString,
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
function getResult(payload) {
|
|
|
|
if (payload.error) {
|
|
|
|
var error = new Error(payload.error.message);
|
|
|
|
error.code = payload.error.code;
|
|
|
|
error.data = payload.error.data;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload.result;
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:26:03 +03:00
|
|
|
function stripHexZeros(value) {
|
|
|
|
while (value.length > 3 && value.substring(0, 3) === '0x0') {
|
|
|
|
value = '0x' + value.substring(3);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
function getTransaction(transaction) {
|
2017-03-01 10:29:12 +03:00
|
|
|
var result = {};
|
2017-07-06 03:26:03 +03:00
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
for (var key in transaction) {
|
|
|
|
result[key] = utils.hexlify(transaction[key]);
|
|
|
|
}
|
2017-07-06 03:26:03 +03:00
|
|
|
|
|
|
|
// Some nodes (INFURA ropsten; INFURA mainnet is fine) don't like extra zeros.
|
|
|
|
['gasLimit', 'gasPrice', 'nonce', 'value'].forEach(function(key) {
|
|
|
|
if (!result[key]) { return; }
|
|
|
|
result[key] = stripHexZeros(result[key]);
|
|
|
|
});
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-26 02:58:21 +03:00
|
|
|
function JsonRpcProvider(url, network) {
|
2017-02-25 09:23:48 +03:00
|
|
|
if (!(this instanceof JsonRpcProvider)) { throw new Error('missing new'); }
|
|
|
|
|
2017-11-13 00:02:07 +03:00
|
|
|
// Legacy Contructor (url, [ testnet, [ chainId ] ])
|
|
|
|
// @TODO: Remove this in the next major version
|
2017-10-26 02:58:21 +03:00
|
|
|
|
2017-11-13 00:02:07 +03:00
|
|
|
var args = [];
|
|
|
|
|
|
|
|
// Legacy without a url
|
|
|
|
if (typeof(url) !== 'string' || Provider.networks[url] != null) {
|
|
|
|
// url => network
|
|
|
|
args.push(url);
|
|
|
|
|
|
|
|
// network => chainId
|
|
|
|
if (network != null) { args.push(network); }
|
|
|
|
|
|
|
|
url = null;
|
|
|
|
|
|
|
|
} else if (arguments.length === 2) {
|
|
|
|
args.push(arguments[1]);
|
|
|
|
|
|
|
|
} else if (arguments.length === 3) {
|
|
|
|
args.push(arguments[1]);
|
|
|
|
args.push(arguments[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Provider.apply(this, args);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
2017-02-27 08:06:32 +03:00
|
|
|
if (!url) { url = 'http://localhost:8545'; }
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
utils.defineProperty(this, 'url', url);
|
|
|
|
}
|
2017-03-01 10:29:12 +03:00
|
|
|
Provider.inherits(JsonRpcProvider);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
utils.defineProperty(JsonRpcProvider.prototype, 'send', function(method, params) {
|
|
|
|
var request = {
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: 42,
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
|
|
|
return Provider.fetchJSON(this.url, JSON.stringify(request), getResult);
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(JsonRpcProvider.prototype, 'perform', function(method, params) {
|
|
|
|
switch (method) {
|
|
|
|
case 'getBlockNumber':
|
|
|
|
return this.send('eth_blockNumber', []);
|
|
|
|
|
|
|
|
case 'getGasPrice':
|
|
|
|
return this.send('eth_gasPrice', []);
|
|
|
|
|
|
|
|
case 'getBalance':
|
2017-07-06 03:26:03 +03:00
|
|
|
var blockTag = params.blockTag;
|
|
|
|
if (utils.isHexString(blockTag)) { blockTag = stripHexZeros(blockTag); }
|
|
|
|
return this.send('eth_getBalance', [params.address, blockTag]);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
case 'getTransactionCount':
|
2017-07-06 03:26:03 +03:00
|
|
|
var blockTag = params.blockTag;
|
|
|
|
if (utils.isHexString(blockTag)) { blockTag = stripHexZeros(blockTag); }
|
|
|
|
return this.send('eth_getTransactionCount', [params.address, blockTag]);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
case 'getCode':
|
2017-07-06 03:26:03 +03:00
|
|
|
var blockTag = params.blockTag;
|
|
|
|
if (utils.isHexString(blockTag)) { blockTag = stripHexZeros(blockTag); }
|
|
|
|
return this.send('eth_getCode', [params.address, blockTag]);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
case 'getStorageAt':
|
2017-07-06 03:26:03 +03:00
|
|
|
var position = params.position;
|
|
|
|
if (utils.isHexString(position)) { position = stripHexZeros(position); }
|
|
|
|
var blockTag = params.blockTag;
|
|
|
|
if (utils.isHexString(blockTag)) { blockTag = stripHexZeros(blockTag); }
|
|
|
|
return this.send('eth_getStorageAt', [params.address, position, blockTag]);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
case 'sendTransaction':
|
|
|
|
return this.send('eth_sendRawTransaction', [params.signedTransaction]);
|
|
|
|
|
|
|
|
case 'getBlock':
|
|
|
|
if (params.blockTag) {
|
2017-07-06 03:26:03 +03:00
|
|
|
var blockTag = params.blockTag;
|
|
|
|
if (utils.isHexString(blockTag)) { blockTag = stripHexZeros(blockTag); }
|
|
|
|
return this.send('eth_getBlockByNumber', [blockTag, false]);
|
2017-02-25 09:23:48 +03:00
|
|
|
} else if (params.blockHash) {
|
|
|
|
return this.send('eth_getBlockByHash', [params.blockHash, false]);
|
|
|
|
}
|
|
|
|
return Promise.reject(new Error('invalid block tag or block hash'));
|
|
|
|
|
|
|
|
case 'getTransaction':
|
|
|
|
return this.send('eth_getTransactionByHash', [params.transactionHash]);
|
|
|
|
|
|
|
|
case 'getTransactionReceipt':
|
|
|
|
return this.send('eth_getTransactionReceipt', [params.transactionHash]);
|
|
|
|
|
|
|
|
case 'call':
|
2017-03-01 10:29:12 +03:00
|
|
|
return this.send('eth_call', [getTransaction(params.transaction), 'latest']);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
case 'estimateGas':
|
|
|
|
return this.send('eth_estimateGas', [getTransaction(params.transaction)]);
|
|
|
|
|
|
|
|
case 'getLogs':
|
|
|
|
return this.send('eth_getLogs', [params.filter]);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(new Error('not implemented - ' + method));
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = JsonRpcProvider;
|