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() {
|
2018-03-05 03:31:09 +03:00
|
|
|
var convert = require('../utils/convert');
|
2017-02-25 09:23:48 +03:00
|
|
|
return {
|
2018-03-05 03:31:09 +03:00
|
|
|
defineProperty: require('../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,
|
2018-03-05 03:31:09 +03:00
|
|
|
hexStripZeros: convert.hexStripZeros,
|
2017-02-25 09:23:48 +03:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2017-12-30 04:41:16 +03:00
|
|
|
function timer(timeout) {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
setTimeout(function() {
|
|
|
|
resolve();
|
|
|
|
}, timeout);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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; }
|
2018-03-05 03:31:09 +03:00
|
|
|
result[key] = utils.hexStripZeros(result[key]);
|
2017-07-06 03:26:03 +03:00
|
|
|
});
|
|
|
|
|
2018-01-17 02:55:56 +03:00
|
|
|
// Transform "gasLimit" to "gas"
|
|
|
|
if (result.gasLimit != null && result.gas == null) {
|
|
|
|
result.gas = result.gasLimit;
|
|
|
|
delete result.gasLimit;
|
|
|
|
}
|
|
|
|
|
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'); }
|
|
|
|
|
2018-03-06 04:16:53 +03:00
|
|
|
if (arguments.length == 1) {
|
2018-03-05 03:31:09 +03:00
|
|
|
if (typeof(url) === 'string') {
|
|
|
|
try {
|
|
|
|
network = Provider.getNetwork(url);
|
|
|
|
url = null;
|
|
|
|
} catch (error) { }
|
|
|
|
} else {
|
|
|
|
network = url;
|
|
|
|
url = null;
|
|
|
|
}
|
2017-11-13 00:02:07 +03:00
|
|
|
}
|
|
|
|
|
2018-03-05 03:31:09 +03:00
|
|
|
Provider.call(this, network);
|
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':
|
2018-03-05 10:52:53 +03:00
|
|
|
return this.send('eth_getBalance', [params.address, params.blockTag]);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
case 'getTransactionCount':
|
2018-03-05 10:52:53 +03:00
|
|
|
return this.send('eth_getTransactionCount', [params.address, params.blockTag]);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
case 'getCode':
|
2018-03-05 10:52:53 +03:00
|
|
|
return this.send('eth_getCode', [params.address, params.blockTag]);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
case 'getStorageAt':
|
2018-03-05 10:52:53 +03:00
|
|
|
return this.send('eth_getStorageAt', [params.address, params.position, params.blockTag]);
|
2017-02-25 09:23:48 +03:00
|
|
|
|
|
|
|
case 'sendTransaction':
|
|
|
|
return this.send('eth_sendRawTransaction', [params.signedTransaction]);
|
|
|
|
|
|
|
|
case 'getBlock':
|
|
|
|
if (params.blockTag) {
|
2018-03-05 10:52:53 +03:00
|
|
|
return this.send('eth_getBlockByNumber', [params.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));
|
|
|
|
});
|
|
|
|
|
2017-12-30 04:41:16 +03:00
|
|
|
utils.defineProperty(JsonRpcProvider.prototype, '_startPending', function() {
|
|
|
|
if (this._pendingFilter != null) { return; }
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var pendingFilter = this.send('eth_newPendingTransactionFilter', []);
|
|
|
|
this._pendingFilter = pendingFilter;
|
|
|
|
|
|
|
|
pendingFilter.then(function(filterId) {
|
|
|
|
function poll() {
|
|
|
|
self.send('eth_getFilterChanges', [ filterId ]).then(function(hashes) {
|
|
|
|
if (self._pendingFilter != pendingFilter) { return; }
|
|
|
|
|
|
|
|
var seq = Promise.resolve();
|
|
|
|
hashes.forEach(function(hash) {
|
|
|
|
seq = seq.then(function() {
|
|
|
|
return self.getTransaction(hash).then(function(tx) {
|
|
|
|
self.emit('pending', tx);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return seq.then(function() {
|
|
|
|
return timer(1000);
|
|
|
|
});
|
|
|
|
}).then(function() {
|
|
|
|
if (self._pendingFilter != pendingFilter) {
|
|
|
|
self.send('eth_uninstallFilter', [ filterIf ]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(function() { poll(); }, 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
poll();
|
|
|
|
|
|
|
|
return filterId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(JsonRpcProvider.prototype, '_stopPending', function() {
|
|
|
|
this._pendingFilter = null;
|
|
|
|
});
|
|
|
|
|
2018-01-17 02:55:56 +03:00
|
|
|
utils.defineProperty(JsonRpcProvider, '_hexlifyTransaction', function(transaction) {
|
|
|
|
return getTransaction(transaction);
|
|
|
|
});
|
|
|
|
|
2017-02-25 09:23:48 +03:00
|
|
|
module.exports = JsonRpcProvider;
|