From abb393b88ced90c61a03c535cf53ff09a0d19caa Mon Sep 17 00:00:00 2001 From: Fabian Vogelsteller Date: Fri, 4 Nov 2016 12:17:20 +0100 Subject: [PATCH] fixed linting --- lib/web3/contract.js | 142 ++++++++++++++++--------------- test/helpers/FakeHttpProvider.js | 6 +- 2 files changed, 77 insertions(+), 71 deletions(-) diff --git a/lib/web3/contract.js b/lib/web3/contract.js index 673a6d4..e4d7269 100644 --- a/lib/web3/contract.js +++ b/lib/web3/contract.js @@ -629,8 +629,8 @@ Contract.prototype.once = function(event, options, callback) { * @param {Function} callback * @return {Object} the event subscription */ -Contract.prototype.on = function(event, options, callback){ - var subOptions = this._generateEventOptions.apply(this, [event, options, callback]); +Contract.prototype.on = function(){ + var subOptions = this._generateEventOptions.apply(this, arguments); // prevent the event "newListener" and "removeListener" from being overwritten @@ -664,8 +664,8 @@ Contract.prototype.on = function(event, options, callback){ * @param {Function} callback * @return {Object} the promievent */ -Contract.prototype.getPastEvents = function(event, options, callback){ - var subOptions = this._generateEventOptions.apply(this, [event, options, callback]); +Contract.prototype.getPastEvents = function(){ + var subOptions = this._generateEventOptions.apply(this, arguments); var getPastLogs = new Method({ name: 'getPastLogs', @@ -712,6 +712,73 @@ Contract.prototype._createTxObject = function _createTxObject(){ return txObject; }; +/** + * The callback called when executing a method + * + * @method _methodReturnCallback + * @param {Object} err + * @param {Mixed} returnValue + */ +var _methodReturnCallback = function(defer, callback, type, err, returnValue) { + var _this = this; + + if(type === 'call') { + returnValue = _this._parent._decodeMethodReturn(_this._method.outputs, returnValue); + } + + + if (err) { + return utils._fireError(err, defer.promise, defer.reject, callback); + } else { + + if(callback) { + callback(null, returnValue); + } + + // send immediate returnValue + defer.promise.emit('data', returnValue); + + if(type === 'send') { + + // fire "receipt" event and resolve after + _this._parent._web3.eth.subscribe('newBlocks', {}, function (err, block, sub) { + if(!err) { + + _this._parent._web3.eth.getTransactionReceipt(returnValue, function (err, receipt) { + if(!err) { + if(receipt) { + sub.unsubscribe(); + + if(!receipt.outOfGas) { + defer.promise.emit('receipt', receipt); + defer.resolve(receipt); + defer.promise.removeAllListeners(); + + } else { + return utils._fireError(new Error('Transaction ran out of gas.'), defer.promise, defer.reject); + } + } + } else { + sub.unsubscribe(); + return utils._fireError(err, defer.promise, defer.reject); + } + }); + + + } else { + sub.unsubscribe(); + return utils._fireError(err, defer.promise, defer.reject); + } + }); + + } else { + // remove all listeners on the end, as no event will ever fire again + defer.resolve(returnValue); + defer.promise.removeAllListeners(); + } + } +}; + /** * Executes a call, transact or estimateGas on a contract function * @@ -752,9 +819,8 @@ Contract.prototype._executeMethod = function _executeMethod(type){ delete options.gasLimit; // add contract address - if(!utils.isAddress(this._parent.address)) { + if(!utils.isAddress(this._parent.address)) throw new Error('This contract object doesn\'t have address set yet, please set an address first.'); - } if(utils.isAddress(options.from)) options.from = options.from.toLowerCase(); @@ -762,9 +828,8 @@ Contract.prototype._executeMethod = function _executeMethod(type){ options.to = this._parent.address.toLowerCase(); // return error, if no "data" is specified - if(!options.data) { + if(!options.data) return utils._fireError(new Error('Couldn\'t find a matching contract method, or the number of parameters is wrong.'), defer.promise, defer.reject, callback); - } // simple return request if(makeRequest) { @@ -785,66 +850,7 @@ Contract.prototype._executeMethod = function _executeMethod(type){ } else { - // create the callback method - var methodReturnCallback = function(err, returnValue) { - - if(type === 'call') { - returnValue = _this._parent._decodeMethodReturn(_this._method.outputs, returnValue); - } - - - if (err) { - return utils._fireError(err, defer.promise, defer.reject, callback); - } else { - - if(callback) { - callback(null, returnValue); - } - - // send immediate returnValue - defer.promise.emit('data', returnValue); - - if(type === 'send') { - - // fire "receipt" event and resolve after - _this._parent._web3.eth.subscribe('newBlocks', {}, function (err, block, sub) { - if(!err) { - - _this._parent._web3.eth.getTransactionReceipt(returnValue, function (err, receipt) { - if(!err) { - if(receipt) { - sub.unsubscribe(); - - if(!receipt.outOfGas) { - defer.promise.emit('receipt', receipt); - defer.resolve(receipt); - defer.promise.removeAllListeners(); - - } else { - return utils._fireError(new Error('Transaction ran out of gas.'), defer.promise, defer.reject); - } - } - } else { - sub.unsubscribe(); - return utils._fireError(err, defer.promise, defer.reject); - } - }); - - - } else { - sub.unsubscribe(); - return utils._fireError(err, defer.promise, defer.reject); - } - }); - - } else { - // remove all listeners on the end, as no event will ever fire again - defer.resolve(returnValue); - defer.promise.removeAllListeners(); - } - } - }; - + var methodReturnCallback = _methodReturnCallback.bind(_this, defer, callback, type); switch (type) { case 'estimate': diff --git a/test/helpers/FakeHttpProvider.js b/test/helpers/FakeHttpProvider.js index 45e8d23..90a0dcd 100644 --- a/test/helpers/FakeHttpProvider.js +++ b/test/helpers/FakeHttpProvider.js @@ -115,9 +115,9 @@ FakeHttpProvider.prototype.injectNotification = function (notification) { }, 100); }; -FakeHttpProvider.prototype.injectResponse = function (response) { - this.response = response; -}; +// FakeHttpProvider.prototype.injectResponse = function (response) { +// this.response = response; +// }; FakeHttpProvider.prototype.injectResult = function (result) { var response = this.getResponseStub();