contract deploy returns now new instance

This commit is contained in:
Fabian Vogelsteller 2017-02-28 21:12:39 +01:00
parent c800df8d6d
commit fd2d0269fc
No known key found for this signature in database
GPG Key ID: E51EADA77F1A4124
4 changed files with 85 additions and 53 deletions

View File

@ -248,7 +248,7 @@ deploy
myContract.deploy(options) myContract.deploy(options)
Call this function to deploy the contract to the blockchain. Call this function to deploy the contract to the blockchain.
After successfull deployment the ``myContract.options.address`` will be set automatically to the newly deployed contract. After successfull deployment the promise will resolve with a new contract instance.
---------- ----------
Parameters Parameters
@ -266,7 +266,7 @@ Returns
``Object``: The transaction object: ``Object``: The transaction object:
- ``Array`` - arguments: The arguments passed to the method before. They can be changed. - ``Array`` - arguments: The arguments passed to the method before. They can be changed.
- ``Function`` - :ref:`send <contract-send>`: Will deploy the contract. - ``Function`` - :ref:`send <contract-send>`: Will deploy the contract. The promise will resolve with the new contract instance, instead of the receipt!
- ``Function`` - :ref:`estimateGas <contract-estimateGas>`: Will estimate the gas used for deploying. - ``Function`` - :ref:`estimateGas <contract-estimateGas>`: Will estimate the gas used for deploying.
- ``Function`` - :ref:`encodeABI <contract-encodeABI>`: Encodes the ABI of the deployment, which is contract data + constructor parameters - ``Function`` - :ref:`encodeABI <contract-encodeABI>`: Encodes the ABI of the deployment, which is contract data + constructor parameters
@ -290,11 +290,11 @@ Example
.on('error', function(error){ ... }) .on('error', function(error){ ... })
.on('transactionHash', function(transactionHash){ ... }) .on('transactionHash', function(transactionHash){ ... })
.on('receipt', function(receipt){ .on('receipt', function(receipt){
// same as when the promise gets resolved, see below console.log(receipt.contractAddress) // contains the new contract address
}) })
.on('confirmation', function(confirmationNumber, receipt){ ... }) .on('confirmation', function(confirmationNumber, receipt){ ... })
.then(function(receipt){ .then(function(newContractInstance){
console.log(myContract.options.address) // gives the new contract address console.log(newContractInstance.options.address) // instance with the new contract address
}); });
@ -309,8 +309,8 @@ Example
gas: 1500000, gas: 1500000,
gasPrice: '30000000000000' gasPrice: '30000000000000'
}) })
.then(function(receipt){ .then(function(newContractInstance){
console.log(myContract.options.address) // gives the new contract address console.log(newContractInstance.options.address) // instance with the new contract address
}); });
@ -484,7 +484,7 @@ Returns
The **callback** will return the 32 bytes transaction hash. The **callback** will return the 32 bytes transaction hash.
``PromiEvent``: A :ref:`promise combined event emitter <promiEvent>`. Will be resolved when the transaction *receipt* is available. Additionally the following events are available: ``PromiEvent``: A :ref:`promise combined event emitter <promiEvent>`. Will be resolved when the transaction *receipt* is available. If this ``send()`` is is called from a ``someContract.deploy()`` then the promise will resolve with the *new contract instance*. Additionally the following events are available:
- ``"transactionHash"`` returns ``String``: is fired right after the transaction is send and a transaction hash is available. - ``"transactionHash"`` returns ``String``: is fired right after the transaction is send and a transaction hash is available.
- ``"receipt"`` returns ``Object``: is fired when the transaction receipt is available. - ``"receipt"`` returns ``Object``: is fired when the transaction receipt is available.

View File

@ -231,7 +231,14 @@ Method.prototype._confirmTransaction = function (defer, result, payload, extraFo
if (code.length > 2) { if (code.length > 2) {
defer.eventEmitter.emit('receipt', receipt); defer.eventEmitter.emit('receipt', receipt);
defer.resolve(receipt);
// if contract, return instance instead of receipt
if (extraFormatters && extraFormatters.contractDeployFormatter) {
defer.resolve(extraFormatters.contractDeployFormatter(receipt));
} else {
defer.resolve(receipt);
}
} else { } else {
utils._fireError(new Error('The contract code couldn\'t be stored, please check your gas limit.'), defer.eventEmitter, defer.reject); utils._fireError(new Error('The contract code couldn\'t be stored, please check your gas limit.'), defer.eventEmitter, defer.reject);

View File

@ -777,16 +777,33 @@ Contract.prototype._executeMethod = function _executeMethod(){
if (_.isArray(receipt.logs)) { if (_.isArray(receipt.logs)) {
// decode logs // decode logs
receipt.events = _.map(receipt.logs, function(log) { var events = _.map(receipt.logs, function(log) {
return _this._parent._decodeEventABI.call({ return _this._parent._decodeEventABI.call({
name: 'ALLEVENTS', name: 'ALLEVENTS',
jsonInterface: _this._parent.options.jsonInterface jsonInterface: _this._parent.options.jsonInterface
}, log); }, log);
}); });
// make log names keys
receipt.events = {};
var count = 0;
events.forEach(function (ev) {
if (ev.event) {
receipt.events[ev.event] = ev;
} else {
receipt.events[count] = ev;
count++;
}
});
delete receipt.logs; delete receipt.logs;
} }
return receipt; return receipt;
},
contractDeployFormatter: function (receipt) {
var newContract = _this._parent.clone();
newContract.options.address = receipt.contractAddress;
return newContract;
} }
}; };

View File

@ -1388,48 +1388,51 @@ describe('contract', function () {
transactionHash: '0x1234', transactionHash: '0x1234',
blockNumber: 10, blockNumber: 10,
gasUsed: 0, gasUsed: 0,
events: events: {
[ { address: address, Unchanged: {
blockNumber: 10, address: address,
transactionHash: '0x1234', blockNumber: 10,
blockHash: '0x1345', transactionHash: '0x1234',
logIndex: 4, blockHash: '0x1345',
id: 'log_9ff24cb4', logIndex: 4,
transactionIndex: 0, id: 'log_9ff24cb4',
returnValues: { transactionIndex: 0,
value: '2', returnValues: {
addressFrom: address, value: '2',
t1: '5' addressFrom: address,
t1: '5'
},
event: 'Unchanged',
raw: {
topics: ['0xf359668f205d0b5cfdc20d11353e05f633f83322e96f15486cbb007d210d66e5',
'0x0000000000000000000000000000000000000000000000000000000000000002',
'0x000000000000000000000000' + addressLowercase.replace('0x', '')],
data: '0x0000000000000000000000000000000000000000000000000000000000000005',
}
}, },
event: 'Unchanged', Changed: {
raw: { address: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
topics: [ '0xf359668f205d0b5cfdc20d11353e05f633f83322e96f15486cbb007d210d66e5', blockNumber: 10,
'0x0000000000000000000000000000000000000000000000000000000000000002', transactionHash: '0x1234',
'0x000000000000000000000000'+ addressLowercase.replace('0x','') ], blockHash: '0x1345',
data: '0x0000000000000000000000000000000000000000000000000000000000000005', logIndex: 4,
id: 'log_9ff24cb4',
transactionIndex: 0,
returnValues: {
from: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
amount: '1',
t1: '1',
t2: '8'
},
event: 'Changed',
raw: {
topics: ['0x792991ed5ba9322deaef76cff5051ce4bedaaa4d097585970f9ad8f09f54e651',
'0x000000000000000000000000' + addressLowercase.replace('0x', ''),
'0x0000000000000000000000000000000000000000000000000000000000000001'],
data: '0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000008',
}
} }
}, { }
address: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
blockNumber: 10,
transactionHash: '0x1234',
blockHash: '0x1345',
logIndex: 4,
id: 'log_9ff24cb4',
transactionIndex: 0,
returnValues: {
from: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
amount: '1',
t1: '1',
t2: '8'
},
event: 'Changed',
raw: {
topics: [ '0x792991ed5ba9322deaef76cff5051ce4bedaaa4d097585970f9ad8f09f54e651',
'0x000000000000000000000000'+ addressLowercase.replace('0x',''),
'0x0000000000000000000000000000000000000000000000000000000000000001' ],
data: '0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000008',
}
}]
}); });
done(); done();
@ -1993,10 +1996,15 @@ describe('contract', function () {
}) })
.on('receipt', function (receipt) { .on('receipt', function (receipt) {
assert.equal(address, receipt.contractAddress); assert.equal(address, receipt.contractAddress);
assert.isNull(contract.options.address);
}) })
.then(function(receipt) { .then(function(newContract) {
assert.equal(address, receipt.contractAddress); assert.equal(newContract.options.address, address);
done(); assert.isTrue(newContract !== contract, 'contract objects shouldn\'t the same');
setTimeout(function () {
done();
}, 1);
}); });
// .on('error', function (value) { // .on('error', function (value) {
// console.log('error', value); // console.log('error', value);