For this kind of scenario, I would use a Solidity event which is a great feature to return data from a transaction. Because of the asynchronous nature of the blockchain, a transaction / write operation (payable or not) doesn't return any data. However, you can trigger an event and read this event from a watcher.
Example:
I created two contracts A and B where A create B.
A.sol
pragma solidity ^0.4.18;
import './B.sol';
contract A {
// Declare event
event newContractB(address contractAddress);
function createB() returns (){
// Deploy a new contract B
address addressB = new B();
// Trigger event
newContractB(addressB);
}
}
B.sol
pragma solidity ^0.4.18;
contract B {
// ...
}
Using Web3, you can define a filter of event to watch it:
var filter = web3.eth.filter({ romBlock: 0, toBlock: "latest" });
filter.watch(function (error, result) {
console.log(JSON.stringify(result.data));
});
> "0x000000000000000000000000c7f1527fcf66a3c53d06299646e4a0e3bd8f6d17"
Using Truffle, you an directly get the event from the transaction receipt
var A = artifacts.require("./A.sol");
contract('A', function(accounts) {
it("test", function() {
var aInstance;
A.deployed().then(function (instance) {
aInstance = instance;
aInstance.createB(5, {from: accounts[0]})
.then(function (tx) {
console.log(tx.logs[0].args);
}).catch(function (error) {
console.log(error);
})
})
});
});
> { contractAddress: '0xc7f1527fcf66a3c53d06299646e4a0e3bd8f6d17' }
I pushed my example on GitHub, you can test via:
> truffle compile
> truffle migrate
> truffle test