2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
var __extends = (this && this.__extends) || (function () {
|
|
|
|
var extendStatics = Object.setPrototypeOf ||
|
|
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
|
|
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
|
|
return function (d, b) {
|
|
|
|
extendStatics(d, b);
|
|
|
|
function __() { this.constructor = d; }
|
|
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC
|
2018-08-03 03:43:09 +03:00
|
|
|
var base_provider_1 = require("./base-provider");
|
2018-09-24 23:07:14 +03:00
|
|
|
var abstract_signer_1 = require("../abstract-signer");
|
2018-06-13 22:39:39 +03:00
|
|
|
var address_1 = require("../utils/address");
|
2018-06-17 23:47:28 +03:00
|
|
|
var bytes_1 = require("../utils/bytes");
|
2018-07-16 07:55:34 +03:00
|
|
|
var networks_1 = require("../utils/networks");
|
2018-06-18 12:42:41 +03:00
|
|
|
var properties_1 = require("../utils/properties");
|
2018-06-13 22:39:39 +03:00
|
|
|
var utf8_1 = require("../utils/utf8");
|
|
|
|
var web_1 = require("../utils/web");
|
2018-09-24 23:07:14 +03:00
|
|
|
var errors = __importStar(require("../errors"));
|
2018-06-13 22:39:39 +03:00
|
|
|
function timer(timeout) {
|
|
|
|
return new Promise(function (resolve) {
|
|
|
|
setTimeout(function () {
|
|
|
|
resolve();
|
|
|
|
}, timeout);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function getResult(payload) {
|
|
|
|
if (payload.error) {
|
|
|
|
// @TODO: not any
|
|
|
|
var error = new Error(payload.error.message);
|
|
|
|
error.code = payload.error.code;
|
|
|
|
error.data = payload.error.data;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return payload.result;
|
|
|
|
}
|
|
|
|
function getLowerCase(value) {
|
|
|
|
if (value) {
|
|
|
|
return value.toLowerCase();
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2018-07-31 01:59:52 +03:00
|
|
|
var _constructorGuard = {};
|
2018-06-18 12:42:41 +03:00
|
|
|
var JsonRpcSigner = /** @class */ (function (_super) {
|
|
|
|
__extends(JsonRpcSigner, _super);
|
2018-08-03 22:26:01 +03:00
|
|
|
function JsonRpcSigner(constructorGuard, provider, addressOrIndex) {
|
2018-06-18 12:42:41 +03:00
|
|
|
var _this = _super.call(this) || this;
|
|
|
|
errors.checkNew(_this, JsonRpcSigner);
|
2018-07-31 01:59:52 +03:00
|
|
|
if (constructorGuard !== _constructorGuard) {
|
|
|
|
throw new Error('do not call the JsonRpcSigner constructor directly; use provider.getSigner');
|
|
|
|
}
|
2018-06-18 12:42:41 +03:00
|
|
|
properties_1.defineReadOnly(_this, 'provider', provider);
|
2018-06-13 22:39:39 +03:00
|
|
|
// Statically attach to a given address
|
2018-08-03 22:26:01 +03:00
|
|
|
if (addressOrIndex) {
|
|
|
|
if (typeof (addressOrIndex) === 'string') {
|
|
|
|
properties_1.defineReadOnly(_this, '_address', address_1.getAddress(addressOrIndex));
|
|
|
|
}
|
|
|
|
else if (typeof (addressOrIndex) === 'number') {
|
|
|
|
properties_1.defineReadOnly(_this, '_index', addressOrIndex);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
errors.throwError('invalid address or index', errors.INVALID_ARGUMENT, { argument: 'addressOrIndex', value: addressOrIndex });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
properties_1.defineReadOnly(_this, '_index', 0);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-18 12:42:41 +03:00
|
|
|
return _this;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-08-27 14:42:26 +03:00
|
|
|
/* May add back in the future; for now it is considered confusing. :)
|
|
|
|
get address(): string {
|
|
|
|
if (!this._address) {
|
|
|
|
errors.throwError('no sync sync address available; use getAddress', errors.UNSUPPORTED_OPERATION, { operation: 'address' });
|
|
|
|
}
|
|
|
|
return this._address
|
|
|
|
}
|
|
|
|
*/
|
2018-06-13 22:39:39 +03:00
|
|
|
JsonRpcSigner.prototype.getAddress = function () {
|
2018-08-03 22:26:01 +03:00
|
|
|
var _this = this;
|
2018-06-13 22:39:39 +03:00
|
|
|
if (this._address) {
|
|
|
|
return Promise.resolve(this._address);
|
|
|
|
}
|
|
|
|
return this.provider.send('eth_accounts', []).then(function (accounts) {
|
2018-08-03 22:26:01 +03:00
|
|
|
if (accounts.length <= _this._index) {
|
|
|
|
errors.throwError('unknown account #' + _this._index, errors.UNSUPPORTED_OPERATION, { operation: 'getAddress' });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-08-27 14:42:26 +03:00
|
|
|
_this._address = address_1.getAddress(accounts[_this._index]);
|
|
|
|
return _this._address;
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
JsonRpcSigner.prototype.getBalance = function (blockTag) {
|
2018-06-18 12:42:41 +03:00
|
|
|
return this.provider.getBalance(this.getAddress(), blockTag);
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
|
|
|
JsonRpcSigner.prototype.getTransactionCount = function (blockTag) {
|
2018-06-18 12:42:41 +03:00
|
|
|
return this.provider.getTransactionCount(this.getAddress(), blockTag);
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
|
|
|
JsonRpcSigner.prototype.sendTransaction = function (transaction) {
|
|
|
|
var _this = this;
|
2018-06-27 01:37:21 +03:00
|
|
|
var tx = properties_1.shallowCopy(transaction);
|
2018-06-18 12:42:41 +03:00
|
|
|
if (tx.from == null) {
|
|
|
|
tx.from = this.getAddress().then(function (address) {
|
|
|
|
if (!address) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return address.toLowerCase();
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
2018-06-18 12:42:41 +03:00
|
|
|
}
|
2018-08-02 01:05:19 +03:00
|
|
|
if (transaction.gasLimit == null) {
|
2018-08-03 22:26:01 +03:00
|
|
|
tx.gasLimit = this.provider.estimateGas(tx);
|
2018-08-02 01:05:19 +03:00
|
|
|
}
|
2018-06-18 12:42:41 +03:00
|
|
|
return properties_1.resolveProperties(tx).then(function (tx) {
|
2018-08-02 01:05:19 +03:00
|
|
|
return _this.provider.send('eth_sendTransaction', [JsonRpcProvider.hexlifyTransaction(tx)]).then(function (hash) {
|
2018-07-03 22:48:37 +03:00
|
|
|
return web_1.poll(function () {
|
|
|
|
return _this.provider.getTransaction(hash).then(function (tx) {
|
|
|
|
if (tx === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return _this.provider._wrapTransaction(tx, hash);
|
|
|
|
});
|
|
|
|
}, { onceBlock: _this.provider }).catch(function (error) {
|
|
|
|
error.transactionHash = hash;
|
|
|
|
throw error;
|
2018-06-27 01:37:21 +03:00
|
|
|
});
|
2018-08-02 01:05:19 +03:00
|
|
|
}, function (error) {
|
2018-10-05 03:25:07 +03:00
|
|
|
if (error.responseText) {
|
|
|
|
// See: JsonRpcProvider.sendTransaction (@TODO: Expose a ._throwError??)
|
|
|
|
if (error.responseText.indexOf('insufficient funds') >= 0) {
|
|
|
|
errors.throwError('insufficient funds', errors.INSUFFICIENT_FUNDS, {
|
|
|
|
transaction: tx
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (error.responseText.indexOf('nonce too low') >= 0) {
|
|
|
|
errors.throwError('nonce has already been used', errors.NONCE_EXPIRED, {
|
|
|
|
transaction: tx
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (error.responseText.indexOf('replacement transaction underpriced') >= 0) {
|
|
|
|
errors.throwError('replacement fee too low', errors.REPLACEMENT_UNDERPRICED, {
|
|
|
|
transaction: tx
|
|
|
|
});
|
|
|
|
}
|
2018-08-02 01:05:19 +03:00
|
|
|
}
|
|
|
|
throw error;
|
2018-06-27 01:37:21 +03:00
|
|
|
});
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
JsonRpcSigner.prototype.signMessage = function (message) {
|
|
|
|
var _this = this;
|
|
|
|
var data = ((typeof (message) === 'string') ? utf8_1.toUtf8Bytes(message) : message);
|
|
|
|
return this.getAddress().then(function (address) {
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
|
2018-06-17 23:47:28 +03:00
|
|
|
return _this.provider.send('eth_sign', [address.toLowerCase(), bytes_1.hexlify(data)]);
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
JsonRpcSigner.prototype.unlock = function (password) {
|
|
|
|
var provider = this.provider;
|
|
|
|
return this.getAddress().then(function (address) {
|
|
|
|
return provider.send('personal_unlockAccount', [address.toLowerCase(), password, null]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return JsonRpcSigner;
|
2018-07-31 01:59:52 +03:00
|
|
|
}(abstract_signer_1.Signer));
|
2018-06-13 22:39:39 +03:00
|
|
|
exports.JsonRpcSigner = JsonRpcSigner;
|
2018-10-11 23:50:52 +03:00
|
|
|
var allowedTransactionKeys = {
|
|
|
|
chainId: true, data: true, gasLimit: true, gasPrice: true, nonce: true, to: true, value: true
|
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
var JsonRpcProvider = /** @class */ (function (_super) {
|
|
|
|
__extends(JsonRpcProvider, _super);
|
|
|
|
function JsonRpcProvider(url, network) {
|
|
|
|
var _this = this;
|
|
|
|
// One parameter, but it is a network name, so swap it with the URL
|
|
|
|
if (typeof (url) === 'string') {
|
|
|
|
if (network === null && networks_1.getNetwork(url)) {
|
|
|
|
network = url;
|
|
|
|
url = null;
|
|
|
|
}
|
|
|
|
}
|
2018-06-18 12:42:41 +03:00
|
|
|
if (network) {
|
|
|
|
// The network has been specified explicitly, we can use it
|
|
|
|
_this = _super.call(this, network) || this;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// The network is unknown, query the JSON-RPC for it
|
|
|
|
var ready = new Promise(function (resolve, reject) {
|
|
|
|
setTimeout(function () {
|
|
|
|
_this.send('net_version', []).then(function (result) {
|
2018-07-03 23:44:05 +03:00
|
|
|
return resolve(networks_1.getNetwork(parseInt(result)));
|
|
|
|
}).catch(function (error) {
|
|
|
|
reject(error);
|
2018-06-18 12:42:41 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
_this = _super.call(this, ready) || this;
|
|
|
|
}
|
2018-06-14 04:10:41 +03:00
|
|
|
errors.checkNew(_this, JsonRpcProvider);
|
2018-06-13 22:39:39 +03:00
|
|
|
// Default URL
|
|
|
|
if (!url) {
|
|
|
|
url = 'http://localhost:8545';
|
|
|
|
}
|
|
|
|
if (typeof (url) === 'string') {
|
|
|
|
_this.connection = {
|
|
|
|
url: url
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_this.connection = url;
|
|
|
|
}
|
|
|
|
return _this;
|
|
|
|
}
|
2018-08-03 22:26:01 +03:00
|
|
|
JsonRpcProvider.prototype.getSigner = function (addressOrIndex) {
|
|
|
|
return new JsonRpcSigner(_constructorGuard, this, addressOrIndex);
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
|
|
|
JsonRpcProvider.prototype.listAccounts = function () {
|
|
|
|
return this.send('eth_accounts', []).then(function (accounts) {
|
|
|
|
return accounts.map(function (a) { return address_1.getAddress(a); });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
JsonRpcProvider.prototype.send = function (method, params) {
|
|
|
|
var request = {
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: 42,
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
|
|
|
return web_1.fetchJson(this.connection, JSON.stringify(request), getResult);
|
|
|
|
};
|
|
|
|
JsonRpcProvider.prototype.perform = function (method, params) {
|
|
|
|
switch (method) {
|
|
|
|
case 'getBlockNumber':
|
|
|
|
return this.send('eth_blockNumber', []);
|
|
|
|
case 'getGasPrice':
|
|
|
|
return this.send('eth_gasPrice', []);
|
|
|
|
case 'getBalance':
|
|
|
|
return this.send('eth_getBalance', [getLowerCase(params.address), params.blockTag]);
|
|
|
|
case 'getTransactionCount':
|
|
|
|
return this.send('eth_getTransactionCount', [getLowerCase(params.address), params.blockTag]);
|
|
|
|
case 'getCode':
|
|
|
|
return this.send('eth_getCode', [getLowerCase(params.address), params.blockTag]);
|
|
|
|
case 'getStorageAt':
|
|
|
|
return this.send('eth_getStorageAt', [getLowerCase(params.address), params.position, params.blockTag]);
|
|
|
|
case 'sendTransaction':
|
2018-08-02 01:05:19 +03:00
|
|
|
return this.send('eth_sendRawTransaction', [params.signedTransaction]).catch(function (error) {
|
2018-10-05 03:25:07 +03:00
|
|
|
if (error.responseText) {
|
|
|
|
// "insufficient funds for gas * price + value"
|
|
|
|
if (error.responseText.indexOf('insufficient funds') > 0) {
|
|
|
|
errors.throwError('insufficient funds', errors.INSUFFICIENT_FUNDS, {});
|
|
|
|
}
|
|
|
|
// "nonce too low"
|
|
|
|
if (error.responseText.indexOf('nonce too low') > 0) {
|
|
|
|
errors.throwError('nonce has already been used', errors.NONCE_EXPIRED, {});
|
|
|
|
}
|
|
|
|
// "replacement transaction underpriced"
|
|
|
|
if (error.responseText.indexOf('replacement transaction underpriced') > 0) {
|
|
|
|
errors.throwError('replacement fee too low', errors.REPLACEMENT_UNDERPRICED, {});
|
|
|
|
}
|
2018-08-02 01:05:19 +03:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
2018-06-13 22:39:39 +03:00
|
|
|
case 'getBlock':
|
|
|
|
if (params.blockTag) {
|
2018-09-06 23:41:02 +03:00
|
|
|
return this.send('eth_getBlockByNumber', [params.blockTag, !!params.includeTransactions]);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (params.blockHash) {
|
2018-09-06 23:41:02 +03:00
|
|
|
return this.send('eth_getBlockByHash', [params.blockHash, !!params.includeTransactions]);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
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':
|
2018-10-11 23:50:52 +03:00
|
|
|
return this.send('eth_call', [JsonRpcProvider.hexlifyTransaction(params.transaction, { from: true }), params.blockTag]);
|
2018-06-13 22:39:39 +03:00
|
|
|
case 'estimateGas':
|
2018-10-11 23:50:52 +03:00
|
|
|
return this.send('eth_estimateGas', [JsonRpcProvider.hexlifyTransaction(params.transaction, { from: true })]);
|
2018-06-13 22:39:39 +03:00
|
|
|
case 'getLogs':
|
|
|
|
if (params.filter && params.filter.address != null) {
|
|
|
|
params.filter.address = getLowerCase(params.filter.address);
|
|
|
|
}
|
|
|
|
return this.send('eth_getLogs', [params.filter]);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
errors.throwError(method + ' not implemented', errors.NOT_IMPLEMENTED, { operation: method });
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
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 null;
|
|
|
|
}
|
|
|
|
var seq = Promise.resolve();
|
|
|
|
hashes.forEach(function (hash) {
|
|
|
|
self._emitted['t:' + hash.toLowerCase()] = 'pending';
|
|
|
|
seq = seq.then(function () {
|
|
|
|
return self.getTransaction(hash).then(function (tx) {
|
|
|
|
self.emit('pending', tx);
|
2018-07-03 23:44:05 +03:00
|
|
|
return null;
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return seq.then(function () {
|
|
|
|
return timer(1000);
|
|
|
|
});
|
|
|
|
}).then(function () {
|
|
|
|
if (self._pendingFilter != pendingFilter) {
|
|
|
|
self.send('eth_uninstallFilter', [filterId]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(function () { poll(); }, 0);
|
2018-07-03 23:44:05 +03:00
|
|
|
return null;
|
|
|
|
}).catch(function (error) { });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
poll();
|
|
|
|
return filterId;
|
2018-07-03 23:44:05 +03:00
|
|
|
}).catch(function (error) { });
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
|
|
|
JsonRpcProvider.prototype._stopPending = function () {
|
|
|
|
this._pendingFilter = null;
|
|
|
|
};
|
2018-07-15 00:21:32 +03:00
|
|
|
// Convert an ethers.js transaction into a JSON-RPC transaction
|
|
|
|
// - gasLimit => gas
|
|
|
|
// - All values hexlified
|
|
|
|
// - All numeric values zero-striped
|
2018-10-11 23:50:52 +03:00
|
|
|
// NOTE: This allows a TransactionRequest, but all values should be resolved
|
|
|
|
// before this is called
|
|
|
|
JsonRpcProvider.hexlifyTransaction = function (transaction, allowExtra) {
|
|
|
|
if (!allowExtra) {
|
|
|
|
allowExtra = {};
|
|
|
|
}
|
|
|
|
for (var key in transaction) {
|
|
|
|
if (!allowedTransactionKeys[key] && !allowExtra[key]) {
|
|
|
|
errors.throwError('invalid key - ' + key, errors.INVALID_ARGUMENT, {
|
|
|
|
argument: 'transaction',
|
|
|
|
value: transaction,
|
|
|
|
key: key
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-07-15 00:21:32 +03:00
|
|
|
var result = {};
|
|
|
|
// Some nodes (INFURA ropsten; INFURA mainnet is fine) don't like extra zeros.
|
|
|
|
['gasLimit', 'gasPrice', 'nonce', 'value'].forEach(function (key) {
|
|
|
|
if (transaction[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var value = bytes_1.hexStripZeros(bytes_1.hexlify(transaction[key]));
|
|
|
|
if (key === 'gasLimit') {
|
|
|
|
key = 'gas';
|
|
|
|
}
|
|
|
|
result[key] = value;
|
|
|
|
});
|
|
|
|
['from', 'to', 'data'].forEach(function (key) {
|
|
|
|
if (transaction[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result[key] = bytes_1.hexlify(transaction[key]);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
return JsonRpcProvider;
|
2018-08-03 03:43:09 +03:00
|
|
|
}(base_provider_1.BaseProvider));
|
2018-06-13 22:39:39 +03:00
|
|
|
exports.JsonRpcProvider = JsonRpcProvider;
|