wait for mined transaction in embarkjs when applicable

This commit is contained in:
Iuri Matias 2017-02-27 20:32:26 -05:00
parent 5477b7f11c
commit 8b107355cb
3 changed files with 87 additions and 10 deletions

View File

@ -107,7 +107,44 @@ var EmbarkJS =
}; };
return true; return true;
} else if (typeof self._originalContractObject[p] === 'function') { } 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 true;
} }
return false; return false;
@ -131,16 +168,12 @@ var EmbarkJS =
var promise = new Promise(function(resolve, reject) { var promise = new Promise(function(resolve, reject) {
contractParams.push(function(err, transaction) { contractParams.push(function(err, transaction) {
console.log("callback");
if (err) { if (err) {
console.log("error");
reject(err); reject(err);
} else if (transaction.address !== undefined) { } else if (transaction.address !== undefined) {
console.log("address contract: " + transaction.address);
resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address})); resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address}));
} }
}); });
console.log(contractParams);
// returns promise // returns promise
// deploys contract // deploys contract
@ -251,6 +284,13 @@ var EmbarkJS =
var ipfs; var ipfs;
if (provider === 'whisper') { if (provider === 'whisper') {
this.currentMessages = EmbarkJS.Messages.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(); this.currentMessages.identity = web3.shh.newIdentity();
} else if (provider === 'orbit') { } else if (provider === 'orbit') {
this.currentMessages = EmbarkJS.Messages.Orbit; this.currentMessages = EmbarkJS.Messages.Orbit;

View File

@ -60,7 +60,44 @@ EmbarkJS.Contract = function(options) {
}; };
return true; return true;
} else if (typeof self._originalContractObject[p] === 'function') { } 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 true;
} }
return false; return false;
@ -84,16 +121,12 @@ EmbarkJS.Contract.prototype.deploy = function(args) {
var promise = new Promise(function(resolve, reject) { var promise = new Promise(function(resolve, reject) {
contractParams.push(function(err, transaction) { contractParams.push(function(err, transaction) {
console.log("callback");
if (err) { if (err) {
console.log("error");
reject(err); reject(err);
} else if (transaction.address !== undefined) { } else if (transaction.address !== undefined) {
console.log("address contract: " + transaction.address);
resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address})); resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address}));
} }
}); });
console.log(contractParams);
// returns promise // returns promise
// deploys contract // deploys contract

View File

@ -14,4 +14,8 @@ contract SimpleStorage {
return storedData; return storedData;
} }
function getS() constant returns (string d) {
return "hello";
}
} }