From 8b107355cbea80c0925e0d86b7c903d96be73b89 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 27 Feb 2017 20:32:26 -0500 Subject: [PATCH] wait for mined transaction in embarkjs when applicable --- js/build/embark.bundle.js | 50 ++++++++++++++++++++--- js/embark.js | 43 ++++++++++++++++--- test_app/app/contracts/simple_storage.sol | 4 ++ 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/js/build/embark.bundle.js b/js/build/embark.bundle.js index 93d602482..dbe2e5a4a 100644 --- a/js/build/embark.bundle.js +++ b/js/build/embark.bundle.js @@ -107,7 +107,44 @@ var EmbarkJS = }; return true; } else if (typeof self._originalContractObject[p] === 'function') { - self[p] = Promise.promisify(self._originalContractObject[p]); + self[p] = function(_args) { + var args = Array.prototype.slice.call(arguments); + var fn = self._originalContractObject[p]; + var props = self.abi.find((x) => x.name == p); + + var promise = new Promise(function(resolve, reject) { + args.push(function(err, transaction) { + promise.tx = transaction; + if (err) { + return reject(err); + } + + var getConfirmation = function() { + self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { + if (err) { + return reject(err); + } + + if (receipt !== null) { + return resolve(receipt); + } + + setTimeout(getConfirmation, 1000); + }); + }; + + if (typeof(transaction) !== "string" || props.constant) { + resolve(transaction); + } else { + getConfirmation(); + } + }); + + fn.apply(fn, args); + }); + + return promise; + }; return true; } return false; @@ -131,16 +168,12 @@ var EmbarkJS = var promise = new Promise(function(resolve, reject) { contractParams.push(function(err, transaction) { - console.log("callback"); if (err) { - console.log("error"); reject(err); } else if (transaction.address !== undefined) { - console.log("address contract: " + transaction.address); resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address})); } }); - console.log(contractParams); // returns promise // deploys contract @@ -251,6 +284,13 @@ var EmbarkJS = var ipfs; if (provider === 'whisper') { this.currentMessages = EmbarkJS.Messages.Whisper; + if (web3 === undefined) { + if (options === undefined) { + web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); + } else { + web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); + } + } this.currentMessages.identity = web3.shh.newIdentity(); } else if (provider === 'orbit') { this.currentMessages = EmbarkJS.Messages.Orbit; diff --git a/js/embark.js b/js/embark.js index 6ddfdcd11..b4d4de70c 100644 --- a/js/embark.js +++ b/js/embark.js @@ -60,7 +60,44 @@ EmbarkJS.Contract = function(options) { }; return true; } else if (typeof self._originalContractObject[p] === 'function') { - self[p] = Promise.promisify(self._originalContractObject[p]); + self[p] = function(_args) { + var args = Array.prototype.slice.call(arguments); + var fn = self._originalContractObject[p]; + var props = self.abi.find((x) => x.name == p); + + var promise = new Promise(function(resolve, reject) { + args.push(function(err, transaction) { + promise.tx = transaction; + if (err) { + return reject(err); + } + + var getConfirmation = function() { + self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { + if (err) { + return reject(err); + } + + if (receipt !== null) { + return resolve(receipt); + } + + setTimeout(getConfirmation, 1000); + }); + }; + + if (typeof(transaction) !== "string" || props.constant) { + resolve(transaction); + } else { + getConfirmation(); + } + }); + + fn.apply(fn, args); + }); + + return promise; + }; return true; } return false; @@ -84,16 +121,12 @@ EmbarkJS.Contract.prototype.deploy = function(args) { var promise = new Promise(function(resolve, reject) { contractParams.push(function(err, transaction) { - console.log("callback"); if (err) { - console.log("error"); reject(err); } else if (transaction.address !== undefined) { - console.log("address contract: " + transaction.address); resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address})); } }); - console.log(contractParams); // returns promise // deploys contract diff --git a/test_app/app/contracts/simple_storage.sol b/test_app/app/contracts/simple_storage.sol index 13957b2de..7ca17d62c 100644 --- a/test_app/app/contracts/simple_storage.sol +++ b/test_app/app/contracts/simple_storage.sol @@ -14,4 +14,8 @@ contract SimpleStorage { return storedData; } + function getS() constant returns (string d) { + return "hello"; + } + }