add new contract.new behaviour

This commit is contained in:
Fabian Vogelsteller 2015-07-06 16:47:49 +02:00
parent ea0e48d700
commit 08639a1324
6 changed files with 166 additions and 34 deletions

55
dist/web3-light.js vendored
View File

@ -1838,10 +1838,11 @@ var ContractFactory = function (abi) {
* @param {Any} contract constructor param2 (optional) * @param {Any} contract constructor param2 (optional)
* @param {Object} contract transaction object (required) * @param {Object} contract transaction object (required)
* @param {Function} callback * @param {Function} callback
* @returns {Contract} returns contract if no callback was passed, * @returns {Contract} returns contract instance
* otherwise calls callback function (err, contract)
*/ */
ContractFactory.prototype.new = function () { ContractFactory.prototype.new = function () {
var contract = new Contract(this.abi);
// parse arguments // parse arguments
var options = {}; // required! var options = {}; // required!
var callback; var callback;
@ -1861,18 +1862,56 @@ ContractFactory.prototype.new = function () {
var bytes = encodeConstructorParams(this.abi, args); var bytes = encodeConstructorParams(this.abi, args);
options.data += bytes; options.data += bytes;
if (!callback) {
var address = web3.eth.sendTransaction(options);
return this.at(address);
}
// wait for the contract address adn check if the code was deployed
var self = this; var self = this;
web3.eth.sendTransaction(options, function (err, address) { var count = 0;
web3.eth.sendTransaction(options, function (err, hash) {
if (err) { if (err) {
callback(err); callback(err);
} else {
// wait for receipt
var filter = web3.eth.filter('latest', function(e, res){
if(!e) {
count++;
// stop watching after 50 blocks (timeout)
if(count > 50) {
if(callback)
callback(new Error('Contract couldn\'t be deployed'));
filter.stopWatching();
} else {
web3.eth.getTransactionReceipt(hash, function(e, receipt){
if(receipt) {
web3.eth.getCode(receipt.contractAddress, function(e, code){
if(code.length > 2) {
contract.address = receipt.contractAddress;
if(callback) {
callback(null, contract);
} }
self.at(address, callback);
} else {
callback(new Error('The contract code couldn\'t be stored'));
}
filter.stopWatching();
}); });
}
});
}
}
})
}
});
return contract;
}; };
/** /**

File diff suppressed because one or more lines are too long

55
dist/web3.js vendored
View File

@ -1838,10 +1838,11 @@ var ContractFactory = function (abi) {
* @param {Any} contract constructor param2 (optional) * @param {Any} contract constructor param2 (optional)
* @param {Object} contract transaction object (required) * @param {Object} contract transaction object (required)
* @param {Function} callback * @param {Function} callback
* @returns {Contract} returns contract if no callback was passed, * @returns {Contract} returns contract instance
* otherwise calls callback function (err, contract)
*/ */
ContractFactory.prototype.new = function () { ContractFactory.prototype.new = function () {
var contract = new Contract(this.abi);
// parse arguments // parse arguments
var options = {}; // required! var options = {}; // required!
var callback; var callback;
@ -1861,18 +1862,56 @@ ContractFactory.prototype.new = function () {
var bytes = encodeConstructorParams(this.abi, args); var bytes = encodeConstructorParams(this.abi, args);
options.data += bytes; options.data += bytes;
if (!callback) {
var address = web3.eth.sendTransaction(options);
return this.at(address);
}
// wait for the contract address adn check if the code was deployed
var self = this; var self = this;
web3.eth.sendTransaction(options, function (err, address) { var count = 0;
web3.eth.sendTransaction(options, function (err, hash) {
if (err) { if (err) {
callback(err); callback(err);
} else {
// wait for receipt
var filter = web3.eth.filter('latest', function(e, res){
if(!e) {
count++;
// stop watching after 50 blocks (timeout)
if(count > 50) {
if(callback)
callback(new Error('Contract couldn\'t be deployed'));
filter.stopWatching();
} else {
web3.eth.getTransactionReceipt(hash, function(e, receipt){
if(receipt) {
web3.eth.getCode(receipt.contractAddress, function(e, code){
if(code.length > 2) {
contract.address = receipt.contractAddress;
if(callback) {
callback(null, contract);
} }
self.at(address, callback);
} else {
callback(new Error('The contract code couldn\'t be stored'));
}
filter.stopWatching();
}); });
}
});
}
}
})
}
});
return contract;
}; };
/** /**

8
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -114,10 +114,11 @@ var ContractFactory = function (abi) {
* @param {Any} contract constructor param2 (optional) * @param {Any} contract constructor param2 (optional)
* @param {Object} contract transaction object (required) * @param {Object} contract transaction object (required)
* @param {Function} callback * @param {Function} callback
* @returns {Contract} returns contract if no callback was passed, * @returns {Contract} returns contract instance
* otherwise calls callback function (err, contract)
*/ */
ContractFactory.prototype.new = function () { ContractFactory.prototype.new = function () {
var contract = new Contract(this.abi);
// parse arguments // parse arguments
var options = {}; // required! var options = {}; // required!
var callback; var callback;
@ -137,18 +138,56 @@ ContractFactory.prototype.new = function () {
var bytes = encodeConstructorParams(this.abi, args); var bytes = encodeConstructorParams(this.abi, args);
options.data += bytes; options.data += bytes;
if (!callback) {
var address = web3.eth.sendTransaction(options);
return this.at(address);
}
// wait for the contract address adn check if the code was deployed
var self = this; var self = this;
web3.eth.sendTransaction(options, function (err, address) { var count = 0;
web3.eth.sendTransaction(options, function (err, hash) {
if (err) { if (err) {
callback(err); callback(err);
} else {
// wait for receipt
var filter = web3.eth.filter('latest', function(e, res){
if(!e) {
count++;
// stop watching after 50 blocks (timeout)
if(count > 50) {
if(callback)
callback(new Error('Contract couldn\'t be deployed'));
filter.stopWatching();
} else {
web3.eth.getTransactionReceipt(hash, function(e, receipt){
if(receipt) {
web3.eth.getCode(receipt.contractAddress, function(e, code){
if(code.length > 2) {
contract.address = receipt.contractAddress;
if(callback) {
callback(null, contract);
} }
self.at(address, callback);
} else {
callback(new Error('The contract code couldn\'t be stored'));
}
filter.stopWatching();
}); });
}
});
}
}
})
}
});
return contract;
}; };
/** /**

View File

@ -219,15 +219,27 @@ describe('web3.eth.contract', function() {
] ]
}]; }];
var steps = 1;
provider.injectResult(address); provider.injectResult(address);
provider.injectValidation(function (payload) { provider.injectValidation(function (payload) {
if(steps === 1) {
assert.equal(payload.jsonrpc, '2.0'); assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_sendTransaction'); assert.equal(payload.method, 'eth_sendTransaction');
assert.equal(payload.params[0].data, code + '0000000000000000000000000000000000000000000000000000000000000002'); assert.equal(payload.params[0].data, code + '0000000000000000000000000000000000000000000000000000000000000002');
steps++;
} else if(steps === 2) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_newBlockFilter');
done(); done();
}
}); });
var myCon = contract(description).new(2, {data: code}); contract(description).new(2, {data: code}, function(e, myCon){
});
}); });
}); });