UNPKG

datxweb-core-method

Version:

Handles the JSON-RPC methods. This package is also internally used by datxweb.

1,283 lines (1,239 loc) 102 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck')); var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass')); var EventEmitter = _interopDefault(require('eventemitter3')); var datxwebCoreSubscriptions = require('datxweb-core-subscriptions'); var _possibleConstructorReturn = _interopDefault(require('@babel/runtime/helpers/possibleConstructorReturn')); var _getPrototypeOf = _interopDefault(require('@babel/runtime/helpers/getPrototypeOf')); var _inherits = _interopDefault(require('@babel/runtime/helpers/inherits')); var isFunction = _interopDefault(require('lodash/isFunction')); var _toConsumableArray = _interopDefault(require('@babel/runtime/helpers/toConsumableArray')); var _regeneratorRuntime = _interopDefault(require('@babel/runtime/regenerator')); var _asyncToGenerator = _interopDefault(require('@babel/runtime/helpers/asyncToGenerator')); var isString = _interopDefault(require('lodash/isString')); var cloneDeep = _interopDefault(require('lodash/cloneDeep')); var rxjs = require('rxjs'); var _get = _interopDefault(require('@babel/runtime/helpers/get')); var PromiEvent = function () { function PromiEvent() { var _this = this; _classCallCheck(this, PromiEvent); this.promise = new Promise(function (resolve, reject) { _this.resolve = resolve; _this.reject = reject; }); this.eventEmitter = new EventEmitter(); return new Proxy(this, { get: this.proxyHandler }); } _createClass(PromiEvent, [{ key: "proxyHandler", value: function proxyHandler(target, name) { if (name === 'resolve' || name === 'reject') { return target[name]; } if (name === 'then') { return target.promise.then.bind(target.promise); } if (name === 'catch') { return target.promise.catch.bind(target.promise); } if (target.eventEmitter[name]) { return target.eventEmitter[name]; } } }]); return PromiEvent; }(); var AbstractMethod = function () { function AbstractMethod(rpcMethod, parametersAmount, utils, formatters, moduleInstance) { _classCallCheck(this, AbstractMethod); this.utils = utils; this.formatters = formatters; this.moduleInstance = moduleInstance; this._arguments = { parameters: [] }; this._rpcMethod = rpcMethod; this._parametersAmount = parametersAmount; } _createClass(AbstractMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) {} }, { key: "afterExecution", value: function afterExecution(response) { return response; } }, { key: "execute", value: function () { var _execute = _asyncToGenerator( _regeneratorRuntime.mark(function _callee() { var error, response; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: this.beforeExecution(this.moduleInstance); if (!(this.parameters.length !== this.parametersAmount)) { _context.next = 7; break; } error = new Error("Invalid Arguments length: expected: ".concat(this.parametersAmount, ", given: ").concat(this.parameters.length)); if (!this.callback) { _context.next = 6; break; } this.callback(error, null); return _context.abrupt("return"); case 6: throw error; case 7: _context.prev = 7; _context.next = 10; return this.moduleInstance.currentProvider.send(this.rpcMethod, this.parameters); case 10: response = _context.sent; if (response) { response = this.afterExecution(response); } if (!this.callback) { _context.next = 15; break; } this.callback(false, response); return _context.abrupt("return"); case 15: return _context.abrupt("return", response); case 18: _context.prev = 18; _context.t0 = _context["catch"](7); if (!this.callback) { _context.next = 23; break; } this.callback(_context.t0, null); return _context.abrupt("return"); case 23: throw _context.t0; case 24: case "end": return _context.stop(); } } }, _callee, this, [[7, 18]]); })); function execute() { return _execute.apply(this, arguments); } return execute; }() }, { key: "setArguments", value: function setArguments(methodArguments) { var parameters = cloneDeep(_toConsumableArray(methodArguments)); var callback = null; if (parameters.length > this.parametersAmount) { if (!isFunction(parameters[parameters.length - 1])) { throw new TypeError("The latest parameter should be a function otherwise it can't be used as callback"); } callback = parameters.pop(); } this._arguments = { callback: callback, parameters: parameters }; } }, { key: "getArguments", value: function getArguments() { return this._arguments; } }, { key: "isHash", value: function isHash(parameter) { return isString(parameter) && parameter.startsWith('0x'); } }, { key: "rpcMethod", set: function set(value) { this._rpcMethod = value; } , get: function get() { return this._rpcMethod; } }, { key: "parametersAmount", set: function set(value) { this._parametersAmount = value; } , get: function get() { return this._parametersAmount; } }, { key: "parameters", get: function get() { return this._arguments.parameters; } , set: function set(value) { this._arguments.parameters = value; } }, { key: "callback", get: function get() { return this._arguments.callback; } , set: function set(value) { this._arguments.callback = value; } }]); return AbstractMethod; }(); var AbstractGetBlockMethod = function (_AbstractMethod) { _inherits(AbstractGetBlockMethod, _AbstractMethod); function AbstractGetBlockMethod(rpcMethod, utils, formatters, moduleInstance) { _classCallCheck(this, AbstractGetBlockMethod); return _possibleConstructorReturn(this, _getPrototypeOf(AbstractGetBlockMethod).call(this, rpcMethod, 2, utils, formatters, moduleInstance)); } _createClass(AbstractGetBlockMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { this.parameters[0] = this.formatters.inputBlockNumberFormatter(this.parameters[0]); if (isFunction(this.parameters[1])) { this.callback = this.parameters[1]; this.parameters[1] = false; } else { this.parameters[1] = !!this.parameters[1]; } } }, { key: "afterExecution", value: function afterExecution(response) { return this.formatters.outputBlockFormatter(response); } }]); return AbstractGetBlockMethod; }(AbstractMethod); var GetBlockByNumberMethod = function (_AbstractGetBlockMeth) { _inherits(GetBlockByNumberMethod, _AbstractGetBlockMeth); function GetBlockByNumberMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetBlockByNumberMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetBlockByNumberMethod).call(this, 'datx_getBlockByNumber', utils, formatters, moduleInstance)); } return GetBlockByNumberMethod; }(AbstractGetBlockMethod); var GetTransactionReceiptMethod = function (_AbstractMethod) { _inherits(GetTransactionReceiptMethod, _AbstractMethod); function GetTransactionReceiptMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetTransactionReceiptMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetTransactionReceiptMethod).call(this, 'datx_getTransactionReceipt', 1, utils, formatters, moduleInstance)); } _createClass(GetTransactionReceiptMethod, [{ key: "afterExecution", value: function afterExecution(response) { if (response !== null) { return this.formatters.outputTransactionReceiptFormatter(response); } return response; } }]); return GetTransactionReceiptMethod; }(AbstractMethod); var TransactionObserver = function () { function TransactionObserver(provider, timeout, blockConfirmations, getTransactionReceiptMethod, getBlockByNumberMethod, newHeadsSubscription) { _classCallCheck(this, TransactionObserver); this.provider = provider; this.timeout = timeout; this.blockConfirmations = blockConfirmations; this.getTransactionReceiptMethod = getTransactionReceiptMethod; this.getBlockByNumberMethod = getBlockByNumberMethod; this.newHeadsSubscription = newHeadsSubscription; this.blockNumbers = []; this.lastBlock = false; this.confirmations = 0; this.confirmationChecks = 0; this.interval = false; } _createClass(TransactionObserver, [{ key: "observe", value: function observe(transactionHash) { var _this = this; return rxjs.Observable.create(function (observer) { if (_this.provider.supportsSubscriptions()) { _this.startSocketObserver(transactionHash, observer); } else { _this.startHttpObserver(transactionHash, observer); } }); } }, { key: "startSocketObserver", value: function startSocketObserver(transactionHash, observer) { var _this2 = this; this.newHeadsSubscription.subscribe( function () { var _ref = _asyncToGenerator( _regeneratorRuntime.mark(function _callee(error, newHead) { var receipt; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; if (!observer.closed) { _context.next = 5; break; } _context.next = 4; return _this2.newHeadsSubscription.unsubscribe(); case 4: return _context.abrupt("return"); case 5: if (!error) { _context.next = 7; break; } throw error; case 7: _this2.getTransactionReceiptMethod.parameters = [transactionHash]; _context.next = 10; return _this2.getTransactionReceiptMethod.execute(); case 10: receipt = _context.sent; if (_this2.blockNumbers.includes(newHead.number)) { _context.next = 25; break; } if (!receipt) { _context.next = 19; break; } _this2.confirmations++; _this2.emitNext(receipt, observer); if (!_this2.isConfirmed()) { _context.next = 19; break; } _context.next = 18; return _this2.newHeadsSubscription.unsubscribe(); case 18: observer.complete(); case 19: _this2.blockNumbers.push(newHead.number); _this2.confirmationChecks++; if (!_this2.isTimeoutTimeExceeded()) { _context.next = 25; break; } _context.next = 24; return _this2.newHeadsSubscription.unsubscribe(); case 24: _this2.emitError(new Error('Timeout exceeded during the transaction confirmation process. Be aware the transaction could still get confirmed!'), receipt, observer); case 25: _context.next = 30; break; case 27: _context.prev = 27; _context.t0 = _context["catch"](0); _this2.emitError(_context.t0, false, observer); case 30: case "end": return _context.stop(); } } }, _callee, null, [[0, 27]]); })); return function (_x, _x2) { return _ref.apply(this, arguments); }; }()); } }, { key: "startHttpObserver", value: function startHttpObserver(transactionHash, observer) { var _this3 = this; var interval = setInterval( _asyncToGenerator( _regeneratorRuntime.mark(function _callee2() { var receipt, block; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.prev = 0; if (!observer.closed) { _context2.next = 4; break; } clearInterval(interval); return _context2.abrupt("return"); case 4: _this3.getTransactionReceiptMethod.parameters = [transactionHash]; _context2.next = 7; return _this3.getTransactionReceiptMethod.execute(); case 7: receipt = _context2.sent; if (!(receipt && receipt.blockNumber)) { _context2.next = 22; break; } if (!_this3.lastBlock) { _context2.next = 16; break; } _context2.next = 12; return _this3.getBlockByNumber(_this3.lastBlock.number + 1); case 12: block = _context2.sent; if (block && _this3.isValidConfirmation(block)) { _this3.lastBlock = block; _this3.confirmations++; _this3.emitNext(receipt, observer); } _context2.next = 21; break; case 16: _context2.next = 18; return _this3.getBlockByNumber(receipt.blockNumber); case 18: _this3.lastBlock = _context2.sent; _this3.confirmations++; _this3.emitNext(receipt, observer); case 21: if (_this3.isConfirmed()) { observer.complete(); clearInterval(interval); } case 22: _this3.confirmationChecks++; if (_this3.isTimeoutTimeExceeded()) { clearInterval(interval); _this3.emitError(new Error('Timeout exceeded during the transaction confirmation process. Be aware the transaction could still get confirmed!'), receipt, observer); } _context2.next = 30; break; case 26: _context2.prev = 26; _context2.t0 = _context2["catch"](0); clearInterval(interval); _this3.emitError(_context2.t0, false, observer); case 30: case "end": return _context2.stop(); } } }, _callee2, null, [[0, 26]]); })), 1000); } }, { key: "emitNext", value: function emitNext(receipt, observer) { observer.next({ receipt: receipt, confirmations: this.confirmations }); } }, { key: "emitError", value: function emitError(error, receipt, observer) { observer.error({ error: error, receipt: receipt, confirmations: this.confirmations, confirmationChecks: this.confirmationChecks }); } }, { key: "getBlockByNumber", value: function getBlockByNumber(blockNumber) { this.getBlockByNumberMethod.parameters = [blockNumber]; return this.getBlockByNumberMethod.execute(); } }, { key: "isConfirmed", value: function isConfirmed() { return this.confirmations === this.blockConfirmations; } }, { key: "isValidConfirmation", value: function isValidConfirmation(block) { return this.lastBlock.hash === block.parentHash && this.lastBlock.number !== block.number; } }, { key: "isTimeoutTimeExceeded", value: function isTimeoutTimeExceeded() { return this.confirmationChecks === this.timeout; } }]); return TransactionObserver; }(); var GetTransactionCountMethod = function (_AbstractMethod) { _inherits(GetTransactionCountMethod, _AbstractMethod); function GetTransactionCountMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetTransactionCountMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetTransactionCountMethod).call(this, 'datx_getTransactionCount', 2, utils, formatters, moduleInstance)); } _createClass(GetTransactionCountMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { this.parameters[0] = this.formatters.inputAddressFormatter(this.parameters[0]); if (isFunction(this.parameters[1])) { this.callback = this.parameters[1]; this.parameters[1] = moduleInstance.defaultBlock; } this.parameters[1] = this.formatters.inputDefaultBlockNumberFormatter(this.parameters[1], moduleInstance); } }, { key: "afterExecution", value: function afterExecution(response) { return this.utils.hexToNumber(response); } }]); return GetTransactionCountMethod; }(AbstractMethod); var ChainIdMethod = function (_AbstractMethod) { _inherits(ChainIdMethod, _AbstractMethod); function ChainIdMethod(utils, formatters, moduleInstance) { _classCallCheck(this, ChainIdMethod); return _possibleConstructorReturn(this, _getPrototypeOf(ChainIdMethod).call(this, 'datx_chainId', 0, utils, formatters, moduleInstance)); } _createClass(ChainIdMethod, [{ key: "afterExecution", value: function afterExecution(response) { return this.utils.hexToNumber(response); } }]); return ChainIdMethod; }(AbstractMethod); var AbstractMethodFactory = function () { function AbstractMethodFactory(utils, formatters) { _classCallCheck(this, AbstractMethodFactory); this.utils = utils; this.formatters = formatters; this._methods = null; } _createClass(AbstractMethodFactory, [{ key: "hasMethod", value: function hasMethod(name) { return typeof this.methods[name] !== 'undefined'; } }, { key: "createMethod", value: function createMethod(name, moduleInstance) { var method = this.methods[name]; if (method.Type === 'observed-transaction-method') { return new method(this.utils, this.formatters, moduleInstance, this.createTransactionObserver(moduleInstance)); } if (method.Type === 'eth-send-transaction-method') { return new method(this.utils, this.formatters, moduleInstance, this.createTransactionObserver(moduleInstance), new ChainIdMethod(this.utils, this.formatters, moduleInstance), new GetTransactionCountMethod(this.utils, this.formatters, moduleInstance)); } return new method(this.utils, this.formatters, moduleInstance); } }, { key: "getTimeout", value: function getTimeout(moduleInstance) { var timeout = moduleInstance.transactionBlockTimeout; if (!moduleInstance.currentProvider.supportsSubscriptions()) { timeout = moduleInstance.transactionPollingTimeout; } return timeout; } }, { key: "createTransactionObserver", value: function createTransactionObserver(moduleInstance) { return new TransactionObserver(moduleInstance.currentProvider, this.getTimeout(moduleInstance), moduleInstance.transactionConfirmationBlocks, new GetTransactionReceiptMethod(this.utils, this.formatters, moduleInstance), new GetBlockByNumberMethod(this.utils, this.formatters, moduleInstance), new datxwebCoreSubscriptions.NewHeadsSubscription(this.utils, this.formatters, moduleInstance)); } }, { key: "methods", get: function get() { if (this._methods) { return this._methods; } throw new Error('No methods defined for MethodFactory!'); } , set: function set(value) { this._methods = value; } }]); return AbstractMethodFactory; }(); var MethodProxy = function MethodProxy(target, methodFactory) { _classCallCheck(this, MethodProxy); return new Proxy(target, { get: function get(target, name) { if (methodFactory.hasMethod(name)) { var RpcMethod = function RpcMethod() { method.setArguments(arguments); return method.execute(); }; if (typeof target[name] !== 'undefined') { throw new TypeError("Duplicated method ".concat(name, ". This method is defined as RPC call and as Object method.")); } var method = methodFactory.createMethod(name, target); RpcMethod.method = method; RpcMethod.request = function () { method.setArguments(arguments); return method; }; return RpcMethod; } return Reflect.get(target, name); } }); }; var GetProtocolVersionMethod = function (_AbstractMethod) { _inherits(GetProtocolVersionMethod, _AbstractMethod); function GetProtocolVersionMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetProtocolVersionMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetProtocolVersionMethod).call(this, 'datx_protocolVersion', 0, utils, formatters, moduleInstance)); } _createClass(GetProtocolVersionMethod, [{ key: "afterExecution", value: function afterExecution(response) { return this.utils.hexToNumber(response); } }]); return GetProtocolVersionMethod; }(AbstractMethod); var VersionMethod = function (_AbstractMethod) { _inherits(VersionMethod, _AbstractMethod); function VersionMethod(utils, formatters, moduleInstance) { _classCallCheck(this, VersionMethod); return _possibleConstructorReturn(this, _getPrototypeOf(VersionMethod).call(this, 'net_version', 0, utils, formatters, moduleInstance)); } _createClass(VersionMethod, [{ key: "afterExecution", value: function afterExecution(response) { return this.utils.hexToNumber(response); } }]); return VersionMethod; }(AbstractMethod); var ListeningMethod = function (_AbstractMethod) { _inherits(ListeningMethod, _AbstractMethod); function ListeningMethod(utils, formatters, moduleInstance) { _classCallCheck(this, ListeningMethod); return _possibleConstructorReturn(this, _getPrototypeOf(ListeningMethod).call(this, 'net_listening', 0, utils, formatters, moduleInstance)); } return ListeningMethod; }(AbstractMethod); var PeerCountMethod = function (_AbstractMethod) { _inherits(PeerCountMethod, _AbstractMethod); function PeerCountMethod(utils, formatters, moduleInstance) { _classCallCheck(this, PeerCountMethod); return _possibleConstructorReturn(this, _getPrototypeOf(PeerCountMethod).call(this, 'net_peerCount', 0, utils, formatters, moduleInstance)); } _createClass(PeerCountMethod, [{ key: "afterExecution", value: function afterExecution(response) { return this.utils.hexToNumber(response); } }]); return PeerCountMethod; }(AbstractMethod); var GetNodeInfoMethod = function (_AbstractMethod) { _inherits(GetNodeInfoMethod, _AbstractMethod); function GetNodeInfoMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetNodeInfoMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetNodeInfoMethod).call(this, 'datxweb_clientVersion', 0, utils, formatters, moduleInstance)); } return GetNodeInfoMethod; }(AbstractMethod); var GetCoinbaseMethod = function (_AbstractMethod) { _inherits(GetCoinbaseMethod, _AbstractMethod); function GetCoinbaseMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetCoinbaseMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetCoinbaseMethod).call(this, 'datx_coinbase', 0, utils, formatters, moduleInstance)); } return GetCoinbaseMethod; }(AbstractMethod); var IsMiningMethod = function (_AbstractMethod) { _inherits(IsMiningMethod, _AbstractMethod); function IsMiningMethod(utils, formatters, moduleInstance) { _classCallCheck(this, IsMiningMethod); return _possibleConstructorReturn(this, _getPrototypeOf(IsMiningMethod).call(this, 'datx_mining', 0, utils, formatters, moduleInstance)); } return IsMiningMethod; }(AbstractMethod); var GetHashrateMethod = function (_AbstractMethod) { _inherits(GetHashrateMethod, _AbstractMethod); function GetHashrateMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetHashrateMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetHashrateMethod).call(this, 'datx_hashrate', 0, utils, formatters, moduleInstance)); } _createClass(GetHashrateMethod, [{ key: "afterExecution", value: function afterExecution(response) { return this.utils.hexToNumber(response); } }]); return GetHashrateMethod; }(AbstractMethod); var IsSyncingMethod = function (_AbstractMethod) { _inherits(IsSyncingMethod, _AbstractMethod); function IsSyncingMethod(utils, formatters, moduleInstance) { _classCallCheck(this, IsSyncingMethod); return _possibleConstructorReturn(this, _getPrototypeOf(IsSyncingMethod).call(this, 'datx_syncing', 0, utils, formatters, moduleInstance)); } _createClass(IsSyncingMethod, [{ key: "afterExecution", value: function afterExecution(response) { if (typeof response !== 'boolean') { return this.formatters.outputSyncingFormatter(response); } return response; } }]); return IsSyncingMethod; }(AbstractMethod); var GetGasPriceMethod = function (_AbstractMethod) { _inherits(GetGasPriceMethod, _AbstractMethod); function GetGasPriceMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetGasPriceMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetGasPriceMethod).call(this, 'datx_gasPrice', 0, utils, formatters, moduleInstance)); } _createClass(GetGasPriceMethod, [{ key: "afterExecution", value: function afterExecution(response) { return this.formatters.outputBigNumberFormatter(response); } }]); return GetGasPriceMethod; }(AbstractMethod); var SubmitWorkMethod = function (_AbstractMethod) { _inherits(SubmitWorkMethod, _AbstractMethod); function SubmitWorkMethod(utils, formatters, moduleInstance) { _classCallCheck(this, SubmitWorkMethod); return _possibleConstructorReturn(this, _getPrototypeOf(SubmitWorkMethod).call(this, 'datx_submitWork', 3, utils, formatters, moduleInstance)); } return SubmitWorkMethod; }(AbstractMethod); var GetWorkMethod = function (_AbstractMethod) { _inherits(GetWorkMethod, _AbstractMethod); function GetWorkMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetWorkMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetWorkMethod).call(this, 'datx_getWork', 0, utils, formatters, moduleInstance)); } return GetWorkMethod; }(AbstractMethod); var GetProofMethod = function (_AbstractMethod) { _inherits(GetProofMethod, _AbstractMethod); function GetProofMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetProofMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetProofMethod).call(this, 'datx_getProof', 3, utils, formatters, moduleInstance)); } _createClass(GetProofMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { this.parameters[0] = this.formatters.inputAddressFormatter(this.parameters[0]); this.parameters[2] = this.formatters.inputDefaultBlockNumberFormatter(this.parameters[2], moduleInstance); } }, { key: "afterExecution", value: function afterExecution(response) { response.nonce = this.utils.toBN(response.nonce).toString(10); response.balance = this.utils.toBN(response.balance).toString(10); for (var i = 0; i < response.storageProof.length; i++) { response.storageProof[i].value = this.utils.toBN(response.storageProof[i].value).toString(10); } return response; } }]); return GetProofMethod; }(AbstractMethod); var GetAccountsMethod = function (_AbstractMethod) { _inherits(GetAccountsMethod, _AbstractMethod); function GetAccountsMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetAccountsMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetAccountsMethod).call(this, 'datx_accounts', 0, utils, formatters, moduleInstance)); } _createClass(GetAccountsMethod, [{ key: "afterExecution", value: function afterExecution(response) { var _this = this; return response.map(function (responseItem) { return _this.utils.toChecksumAddress(responseItem); }); } }]); return GetAccountsMethod; }(AbstractMethod); var GetBalanceMethod = function (_AbstractMethod) { _inherits(GetBalanceMethod, _AbstractMethod); function GetBalanceMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetBalanceMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetBalanceMethod).call(this, 'datx_getBalance', 2, utils, formatters, moduleInstance)); } _createClass(GetBalanceMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { this.parameters[0] = this.formatters.inputAddressFormatter(this.parameters[0]); if (isFunction(this.parameters[1])) { this.callback = this.parameters[1]; this.parameters[1] = moduleInstance.defaultBlock; } this.parameters[1] = this.formatters.inputDefaultBlockNumberFormatter(this.parameters[1], moduleInstance); } }, { key: "afterExecution", value: function afterExecution(response) { return this.formatters.outputBigNumberFormatter(response); } }]); return GetBalanceMethod; }(AbstractMethod); var RequestAccountsMethod = function (_AbstractMethod) { _inherits(RequestAccountsMethod, _AbstractMethod); function RequestAccountsMethod(utils, formatters, moduleInstance) { _classCallCheck(this, RequestAccountsMethod); return _possibleConstructorReturn(this, _getPrototypeOf(RequestAccountsMethod).call(this, 'datx_requestAccounts', 0, utils, formatters, moduleInstance)); } return RequestAccountsMethod; }(AbstractMethod); var AbstractGetUncleMethod = function (_AbstractMethod) { _inherits(AbstractGetUncleMethod, _AbstractMethod); function AbstractGetUncleMethod(rpcMethod, utils, formatters, moduleInstance) { _classCallCheck(this, AbstractGetUncleMethod); return _possibleConstructorReturn(this, _getPrototypeOf(AbstractGetUncleMethod).call(this, rpcMethod, 2, utils, formatters, moduleInstance)); } _createClass(AbstractGetUncleMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { this.parameters[0] = this.formatters.inputBlockNumberFormatter(this.parameters[0]); this.parameters[1] = this.utils.numberToHex(this.parameters[1]); } }, { key: "afterExecution", value: function afterExecution(response) { return this.formatters.outputBlockFormatter(response); } }]); return AbstractGetUncleMethod; }(AbstractMethod); var AbstractGetBlockTransactionCountMethod = function (_AbstractMethod) { _inherits(AbstractGetBlockTransactionCountMethod, _AbstractMethod); function AbstractGetBlockTransactionCountMethod(rpcMethod, utils, formatters, moduleInstance) { _classCallCheck(this, AbstractGetBlockTransactionCountMethod); return _possibleConstructorReturn(this, _getPrototypeOf(AbstractGetBlockTransactionCountMethod).call(this, rpcMethod, 1, utils, formatters, moduleInstance)); } _createClass(AbstractGetBlockTransactionCountMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { this.parameters[0] = this.formatters.inputBlockNumberFormatter(this.parameters[0]); } }, { key: "afterExecution", value: function afterExecution(response) { return this.utils.hexToNumber(response); } }]); return AbstractGetBlockTransactionCountMethod; }(AbstractMethod); var AbstractGetBlockUncleCountMethod = function (_AbstractMethod) { _inherits(AbstractGetBlockUncleCountMethod, _AbstractMethod); function AbstractGetBlockUncleCountMethod(rpcMethod, utils, formatters, moduleInstance) { _classCallCheck(this, AbstractGetBlockUncleCountMethod); return _possibleConstructorReturn(this, _getPrototypeOf(AbstractGetBlockUncleCountMethod).call(this, rpcMethod, 1, utils, formatters, moduleInstance)); } _createClass(AbstractGetBlockUncleCountMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { this.parameters[0] = this.formatters.inputBlockNumberFormatter(this.parameters[0]); } }, { key: "afterExecution", value: function afterExecution(response) { return this.utils.hexToNumber(response); } }]); return AbstractGetBlockUncleCountMethod; }(AbstractMethod); var GetBlockByHashMethod = function (_AbstractGetBlockMeth) { _inherits(GetBlockByHashMethod, _AbstractGetBlockMeth); function GetBlockByHashMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetBlockByHashMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetBlockByHashMethod).call(this, 'datx_getBlockByHash', utils, formatters, moduleInstance)); } return GetBlockByHashMethod; }(AbstractGetBlockMethod); var GetBlockNumberMethod = function (_AbstractMethod) { _inherits(GetBlockNumberMethod, _AbstractMethod); function GetBlockNumberMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetBlockNumberMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetBlockNumberMethod).call(this, 'datx_blockNumber', 0, utils, formatters, moduleInstance)); } _createClass(GetBlockNumberMethod, [{ key: "afterExecution", value: function afterExecution(response) { return this.utils.hexToNumber(response); } }]); return GetBlockNumberMethod; }(AbstractMethod); var GetBlockTransactionCountByHashMethod = function (_AbstractGetBlockTran) { _inherits(GetBlockTransactionCountByHashMethod, _AbstractGetBlockTran); function GetBlockTransactionCountByHashMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetBlockTransactionCountByHashMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetBlockTransactionCountByHashMethod).call(this, 'datx_getBlockTransactionCountByHash', utils, formatters, moduleInstance)); } return GetBlockTransactionCountByHashMethod; }(AbstractGetBlockTransactionCountMethod); var GetBlockTransactionCountByNumberMethod = function (_AbstractGetBlockTran) { _inherits(GetBlockTransactionCountByNumberMethod, _AbstractGetBlockTran); function GetBlockTransactionCountByNumberMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetBlockTransactionCountByNumberMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetBlockTransactionCountByNumberMethod).call(this, 'datx_getBlockTransactionCountByNumber', utils, formatters, moduleInstance)); } return GetBlockTransactionCountByNumberMethod; }(AbstractGetBlockTransactionCountMethod); var GetBlockUncleCountByBlockHashMethod = function (_AbstractGetBlockUncl) { _inherits(GetBlockUncleCountByBlockHashMethod, _AbstractGetBlockUncl); function GetBlockUncleCountByBlockHashMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetBlockUncleCountByBlockHashMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetBlockUncleCountByBlockHashMethod).call(this, 'datx_getUncleCountByBlockHash', utils, formatters, moduleInstance)); } return GetBlockUncleCountByBlockHashMethod; }(AbstractGetBlockUncleCountMethod); var GetBlockUncleCountByBlockNumberMethod = function (_AbstractGetBlockUncl) { _inherits(GetBlockUncleCountByBlockNumberMethod, _AbstractGetBlockUncl); function GetBlockUncleCountByBlockNumberMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetBlockUncleCountByBlockNumberMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetBlockUncleCountByBlockNumberMethod).call(this, 'datx_getUncleCountByBlockNumber', utils, formatters, moduleInstance)); } return GetBlockUncleCountByBlockNumberMethod; }(AbstractGetBlockUncleCountMethod); var GetUncleByBlockHashAndIndexMethod = function (_AbstractGetUncleMeth) { _inherits(GetUncleByBlockHashAndIndexMethod, _AbstractGetUncleMeth); function GetUncleByBlockHashAndIndexMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetUncleByBlockHashAndIndexMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetUncleByBlockHashAndIndexMethod).call(this, 'datx_getUncleByBlockHashAndIndex', utils, formatters, moduleInstance)); } return GetUncleByBlockHashAndIndexMethod; }(AbstractGetUncleMethod); var GetUncleByBlockNumberAndIndexMethod = function (_AbstractGetUncleMeth) { _inherits(GetUncleByBlockNumberAndIndexMethod, _AbstractGetUncleMeth); function GetUncleByBlockNumberAndIndexMethod(utils, formatters, moduleInstance) { _classCallCheck(this, GetUncleByBlockNumberAndIndexMethod); return _possibleConstructorReturn(this, _getPrototypeOf(GetUncleByBlockNumberAndIndexMethod).call(this, 'datx_getUncleByBlockNumberAndIndex', utils, formatters, moduleInstance)); } return GetUncleByBlockNumberAndIndexMethod; }(AbstractGetUncleMethod); var AbstractGetTransactionFromBlockMethod = function (_AbstractMethod) { _inherits(AbstractGetTransactionFromBlockMethod, _AbstractMethod); function AbstractGetTransactionFromBlockMethod(rpcMethod, utils, formatters, moduleInstance) { _classCallCheck(this, AbstractGetTransactionFromBlockMethod); return _possibleConstructorReturn(this, _getPrototypeOf(AbstractGetTransactionFromBlockMethod).call(this, rpcMethod, 2, utils, formatters, moduleInstance)); } _createClass(AbstractGetTransactionFromBlockMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { this.parameters[0] = this.formatters.inputBlockNumberFormatter(this.parameters[0]); this.parameters[1] = this.utils.numberToHex(this.parameters[1]); } }, { key: "afterExecution", value: function afterExecution(response) { return this.formatters.outputTransactionFormatter(response); } }]); return AbstractGetTransactionFromBlockMethod; }(AbstractMethod); var AbstractObservedTransactionMethod = function (_AbstractMethod) { _inherits(AbstractObservedTransactionMethod, _AbstractMethod); function AbstractObservedTransactionMethod(rpcMethod, parametersAmount, utils, formatters, moduleInstance, transactionObserver) { var _this; _classCallCheck(this, AbstractObservedTransactionMethod); _this = _possibleConstructorReturn(this, _getPrototypeOf(AbstractObservedTransactionMethod).call(this, rpcMethod, parametersAmount, utils, formatters, moduleInstance)); _this.transactionObserver = transactionObserver; _this.promiEvent = new PromiEvent(); return _this; } _createClass(AbstractObservedTransactionMethod, [{ key: "execute", value: function execute() { var _this2 = this; this.beforeExecution(this.moduleInstance); this.moduleInstance.currentProvider.send(this.rpcMethod, this.parameters).then(function (transactionHash) { var confirmations, receipt; if (_this2.callback) { _this2.callback(false, transactionHash); return; } _this2.promiEvent.emit('transactionHash', transactionHash); var transactionConfirmationSubscription = _this2.transactionObserver.observe(transactionHash).subscribe(function (transactionConfirmation) { confirmations = transactionConfirmation.confirmations; receipt = transactionConfirmation.receipt; if (!receipt.status) { if (_this2.parameters[0].gas === receipt.gasUsed) { _this2.handleError(new Error("Transaction ran out of gas. Please provide more gas:\n".concat(JSON.stringify(receipt, null, 2))), receipt, confirmations); transactionConfirmationSubscription.unsubscribe(); return; } _this2.handleError(new Error("Transaction has been reverted by the EVM:\n".concat(JSON.stringify(receipt, null, 2))), receipt, confirmations); transactionConfirmationSubscription.unsubscribe(); return; } _this2.promiEvent.emit('confirmation', confirmations, _this2.formatters.outputTransactionFormatter(receipt)); }, function (error) { _this2.handleError(error, receipt, confirmations); }, function () { if (_this2.promiEvent.listenerCount('receipt') > 0) { _this2.promiEvent.emit('receipt', _this2.afterExecution(receipt)); _this2.promiEvent.removeAllListeners(); return; } _this2.promiEvent.resolve(_this2.afterExecution(receipt)); }); }).catch(function (error) { if (_this2.callback) { _this2.callback(error, null); return; } _this2.handleError(error, false, 0); }); return this.promiEvent; } }, { key: "handleError", value: function handleError(error, receipt, confirmations) { if (this.promiEvent.listenerCount('error') > 0) { this.promiEvent.emit('error', error, receipt, confirmations); this.promiEvent.removeAllListeners(); return; } this.promiEvent.reject(error); } }, { key: "Type", get: function get() { return 'observed-transaction-method'; } }], [{ key: "Type", get: function get() { return 'observed-transaction-method'; } }]); return AbstractObservedTransactionMethod; }(AbstractMethod); var SendTransactionMethod = function (_AbstractObservedTran) { _inherits(SendTransactionMethod, _AbstractObservedTran); function SendTransactionMethod(utils, formatters, moduleInstance, transactionObserver) { _classCallCheck(this, SendTransactionMethod); return _possibleConstructorReturn(this, _getPrototypeOf(SendTransactionMethod).call(this, 'datx_sendTransaction', 1, utils, formatters, moduleInstance, transactionObserver)); } _createClass(SendTransactionMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { this.parameters[0] = this.formatters.inputTransactionFormatter(this.parameters[0], moduleInstance); } }, { key: "afterExecution", value: function afterExecution(response) { return this.formatters.outputTransactionFormatter(response); } }]); return SendTransactionMethod; }(AbstractObservedTransactionMethod); var EthSendTransactionMethod = function (_SendTransactionMetho) { _inherits(EthSendTransactionMethod, _SendTransactionMetho); function EthSendTransactionMethod(utils, formatters, moduleInstance, transactionObserver, chainIdMethod, getTransactionCountMethod) { var _this; _classCallCheck(this, EthSendTransactionMethod); _this = _possibleConstructorReturn(this, _getPrototypeOf(EthSendTransactionMethod).call(this, utils, formatters, moduleInstance, transactionObserver)); _this.chainIdMethod = chainIdMethod; _this.getTransactionCountMethod = getTransactionCountMethod; return _this; } _createClass(EthSendTransactionMethod, [{ key: "beforeExecution", value: function beforeExecution(moduleInstance) { if (this.rpcMethod !== 'datx_sendRawTransaction') { _get(_getPrototypeOf(EthSendTransactionMethod.prototype), "beforeExecution", this).call(this, moduleInstance); } } }, { key: "execute", value: function execute() { var _this2 = this; if (!this.parameters[0].gas && this.moduleInstance.defaultGas) { this.parameters[0]['gas'] = this.moduleInstance.defaultGas; } if (!this.parameters[0].gasPrice && this.parameters[0].gasPrice !== 0) { if (!this.moduleInstance.defaultGasPrice) { this.moduleInstance.currentProvider.send('datx_gasPrice', []).then(function (gasPrice) { _this2.parameters[0].gasPrice = gasPrice; _this2.execute(); }).catch(function (error) { _this2.handleError(error, false, 0); }); return this.promiEvent; } this.parameters[0]['gasPrice'] = this.moduleInstance.defaultGasPrice; } if (this.hasAccounts() && this.isDefaultSigner()) { var account = this.moduleInstance.accounts.wallet[this.parameters[0].from]; if (account) { this.sendRawTransaction(account).catch(function (error) { _this2.handleError(error, false, 0); }); return this.promiEvent; } } if (this.hasCustomSigner()) { this.sendRawTransaction().catch(function (error) { _this2.handleError(error, false, 0); }); return this.promiEvent; } return _get(_getPrototypeOf(EthSendTransactionMethod.prototype), "execute", this).call(this); } }, { key: "sendRawTransaction", value: function () { var _sendRawTransaction = _asyncToGenerator( _regeneratorRuntime.mark(function _callee() { var account, response, _args = arguments; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: account = _args.length > 0 && _args[0] !== undefined ? _args[0] : {}; _context.next = 3; return this.signTransaction(account); case 3: response = _context.sent; this.parameters = [response.rawTransaction]; this.rpcMethod = 'datx_sendRawTransaction'; return _context.abrupt("return", _get(_getPrototypeOf(EthSendTransactionMethod.prototype), "execute", this).call(this)); case 7: case "end": return _context.stop(); } } }, _callee, this); })); function sendRawTransaction() { return _sendRawTransaction.apply(this, arguments); } return sendRawTransaction; }() }, { key: "signTransaction", value: function () { var _signTransaction = _asyncToGenerator( _regeneratorRuntime.mark(function _callee2() { var account, nonce, transaction, _args2 = arguments; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: account = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : {}; this.beforeExecution(this.moduleInstance); if (this.parameters[0].chainId) { _context2.next = 6; break; } _context2.next = 5; return this.chainIdMethod.execute(); case 5: this.parameters[0].chainId = _context2.sent; case 6: if (!(!this.parameters[0].nonce && this.parameters[0].nonce !== 0)) { _context2.next = 15; break; } if (account.nonce) { account.nonce = account.nonce + 1; nonce = account.nonce; } if (nonce) { _context2.next = 14; break; } this.getTransactionCountMethod.parameters = [this.parameters[0].from, 'latest']; _context2.next = 12; return this.getTransactionCountMethod.execute(); case 12: nonce = _context2.sent; account.nonce = nonce; case 14: this.parameters[0].nonce = nonce; case 15: transaction = this.parameters[0]; transaction.to = transaction.to || '0x'; transaction.data = transaction.data || '0x'; transaction.value = transaction.value || '0x'; transaction.chainId = this.utils.numberToHex(transaction.chainId); delete transaction.from; return _context2.abrupt("return", this.moduleInstance.transactionSigner.sign(transaction, account.privateKey)); case 22: case "end": return _context2.stop(); } } }, _callee2, this); })); function signTransaction() { return _signTransaction.apply(this, arguments); } return signTransaction; }() }, { key: "isDefaultSigner", va