@@ -1417,7 +1417,7 @@ module.exports = {
14171417
14181418},{"bignumber.js":"bignumber.js"}],8:[function(require,module,exports){
14191419module.exports={
1420- "version": "0.7.1 "
1420+ "version": "0.8.0 "
14211421}
14221422
14231423},{}],9:[function(require,module,exports){
@@ -1839,6 +1839,55 @@ var contract = function (abi) {
18391839 return new ContractFactory(abi);
18401840};
18411841
1842+ /**
1843+ * Should be called to create new ContractFactory
1844+ *
1845+ * @method checkForContractAddress
1846+ * @param {Object} contract
1847+ * @param {Function} callback
1848+ * @returns {Undefined}
1849+ */
1850+ var checkForContractAddress = function(contract, callback){
1851+ var count = 0;
1852+
1853+ // wait for receipt
1854+ var filter = web3.eth.filter('latest', function(e){
1855+ if(!e) {
1856+ count++;
1857+
1858+ // stop watching after 50 blocks (timeout)
1859+ if(count > 50) {
1860+ if(callback)
1861+ callback(new Error('Contract couldn\'t be deployed'));
1862+
1863+ filter.stopWatching();
1864+
1865+ } else {
1866+
1867+ web3.eth.getTransactionReceipt(contract.transactionHash, function(e, receipt){
1868+ if(receipt) {
1869+
1870+ web3.eth.getCode(receipt.contractAddress, function(e, code){
1871+ if(code.length > 2) {
1872+
1873+ contract.address = receipt.contractAddress;
1874+
1875+ if(callback)
1876+ callback(null, contract);
1877+
1878+ } else if(callback) {
1879+ callback(new Error('The contract code couldn\'t be stored'));
1880+ }
1881+
1882+ filter.stopWatching();
1883+ });
1884+ }
1885+ });
1886+ }
1887+ }
1888+ });
1889+ };
1890+
18421891/**
18431892 * Should be called to create new ContractFactory instance
18441893 *
@@ -1857,10 +1906,11 @@ var ContractFactory = function (abi) {
18571906 * @param {Any} contract constructor param2 (optional)
18581907 * @param {Object} contract transaction object (required)
18591908 * @param {Function} callback
1860- * @returns {Contract} returns contract if no callback was passed,
1861- * otherwise calls callback function (err, contract)
1909+ * @returns {Contract} returns contract instance
18621910 */
18631911ContractFactory.prototype.new = function () {
1912+ var contract = new Contract(this.abi);
1913+
18641914 // parse arguments
18651915 var options = {}; // required!
18661916 var callback;
@@ -1880,18 +1930,27 @@ ContractFactory.prototype.new = function () {
18801930 var bytes = encodeConstructorParams(this.abi, args);
18811931 options.data += bytes;
18821932
1883- if (!callback) {
1884- var address = web3.eth.sendTransaction(options);
1885- return this.at(address);
1933+
1934+ if(callback) {
1935+
1936+ // wait for the contract address adn check if the code was deployed
1937+ web3.eth.sendTransaction(options, function (err, hash) {
1938+ if (err) {
1939+ callback(err);
1940+ } else {
1941+ // add the transaction hash
1942+ contract.transactionHash = hash;
1943+ checkForContractAddress(contract, callback);
1944+ }
1945+ });
1946+ } else {
1947+ var hash = web3.eth.sendTransaction(options);
1948+ // add the transaction hash
1949+ contract.transactionHash = hash;
1950+ checkForContractAddress(contract);
18861951 }
1887-
1888- var self = this;
1889- web3.eth.sendTransaction(options, function (err, address) {
1890- if (err) {
1891- callback(err);
1892- }
1893- self.at(address, callback);
1894- });
1952+
1953+ return contract;
18951954};
18961955
18971956/**
0 commit comments