From 68f55cfab59e394f2bc3f11959f1bc1075a52df4 Mon Sep 17 00:00:00 2001 From: Kevin King Date: Wed, 10 Feb 2016 13:35:53 -0800 Subject: [PATCH] fixed: ContractFactory prototype inheritance #384 --- lib/web3/contract.js | 125 ++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 62 deletions(-) diff --git a/lib/web3/contract.js b/lib/web3/contract.js index af4a573..aa65cc6 100644 --- a/lib/web3/contract.js +++ b/lib/web3/contract.js @@ -14,7 +14,7 @@ You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ -/** +/** * @file contract.js * @author Marek Kotewicz * @date 2014 @@ -76,7 +76,7 @@ var addEventsToContract = function (contract) { var All = new AllEvents(contract._eth._requestManager, events, contract.address); All.attachToContract(contract); - + events.map(function (json) { return new SolidityEvent(contract._eth._requestManager, json, contract.address); }).forEach(function (e) { @@ -104,7 +104,7 @@ var checkForContractAddress = function(contract, callback){ // stop watching after 50 blocks (timeout) if (count > 50) { - + filter.stopWatching(); callbackFired = true; @@ -124,7 +124,7 @@ var checkForContractAddress = function(contract, callback){ if(callbackFired || !code) return; - + filter.stopWatching(); callbackFired = true; @@ -166,6 +166,62 @@ var ContractFactory = function (eth, abi) { this.eth = eth; this.abi = abi; + /** + * Should be called to create new contract on a blockchain + * + * @method new + * @param {Any} contract constructor param1 (optional) + * @param {Any} contract constructor param2 (optional) + * @param {Object} contract transaction object (required) + * @param {Function} callback + * @returns {Contract} returns contract instance + */ + this.new = function () { + var contract = new Contract(this.eth, this.abi); + + // parse arguments + var options = {}; // required! + var callback; + + var args = Array.prototype.slice.call(arguments); + if (utils.isFunction(args[args.length - 1])) { + callback = args.pop(); + } + + var last = args[args.length - 1]; + if (utils.isObject(last) && !utils.isArray(last)) { + options = args.pop(); + } + + var bytes = encodeConstructorParams(this.abi, args); + options.data += bytes; + + if (callback) { + + // wait for the contract address adn check if the code was deployed + this.eth.sendTransaction(options, function (err, hash) { + if (err) { + callback(err); + } else { + // add the transaction hash + contract.transactionHash = hash; + + // call callback for the first time + callback(null, contract); + + checkForContractAddress(contract, callback); + } + }); + } else { + var hash = this.eth.sendTransaction(options); + // add the transaction hash + contract.transactionHash = hash; + checkForContractAddress(contract); + } + + return contract; + }; + this.new.getData = this.getData.bind(this); }; @@ -180,61 +236,7 @@ var ContractFactory = function (eth, abi) { //return new ContractFactory(abi); //}; -/** - * Should be called to create new contract on a blockchain - * - * @method new - * @param {Any} contract constructor param1 (optional) - * @param {Any} contract constructor param2 (optional) - * @param {Object} contract transaction object (required) - * @param {Function} callback - * @returns {Contract} returns contract instance - */ -ContractFactory.prototype.new = function () { - var contract = new Contract(this.eth, this.abi); - // parse arguments - var options = {}; // required! - var callback; - - var args = Array.prototype.slice.call(arguments); - if (utils.isFunction(args[args.length - 1])) { - callback = args.pop(); - } - - var last = args[args.length - 1]; - if (utils.isObject(last) && !utils.isArray(last)) { - options = args.pop(); - } - - var bytes = encodeConstructorParams(this.abi, args); - options.data += bytes; - - if (callback) { - - // wait for the contract address adn check if the code was deployed - this.eth.sendTransaction(options, function (err, hash) { - if (err) { - callback(err); - } else { - // add the transaction hash - contract.transactionHash = hash; - - // call callback for the first time - callback(null, contract); - - checkForContractAddress(contract, callback); - } - }); - } else { - var hash = this.eth.sendTransaction(options); - // add the transaction hash - contract.transactionHash = hash; - checkForContractAddress(contract); - } - - return contract; -}; /** * Should be called to get access to existing contract on a blockchain @@ -248,14 +250,14 @@ ContractFactory.prototype.new = function () { ContractFactory.prototype.at = function (address, callback) { var contract = new Contract(this.eth, this.abi, address); - // this functions are not part of prototype, + // this functions are not part of prototype, // because we dont want to spoil the interface addFunctionsToContract(contract); addEventsToContract(contract); - + if (callback) { callback(null, contract); - } + } return contract; }; @@ -294,4 +296,3 @@ var Contract = function (eth, abi, address) { }; module.exports = ContractFactory; -