I have a factory contract which has a function which returns a new contract:

contract AddOrRemoveAdminVotingBuilder
{
    function build(
        address pPlatform,
        address pAdminManager
    )
        external
        returns(address)
    {
        return address(
            new AddOrRemoveAdminVoting(
                pPlatform, 
                pAdminManager
            )
        );
    }
}

I'm trying to receive the address from my website script with web3. The usual async function call returns txhash.

Tried this:

votingBuilders[0].build(platform.address, adminManagerAddress, function(error, result)
{
    let txhash = result;
    console.log("tx: " + txhash);

    let interval = setInterval(function()
    {
        web3.eth.getTransactionReceipt(txhash, function(error, result)
        {
            if(!error && result !== null)
            {
                if(result["status"] == "0x0") // fail
                {

                }
                else if(result["status"] == "0x1") // success
                {
                    incomingVotes.push(result);
                    console.log(result);
                    clearInterval(interval);
                }
            }
        });
    }, 1000);
});

This code puts the successfuly mined transaction to an array. But how can I get the return address of newly created cotract from the return value?

up vote 0 down vote accepted

The simplest solution is to log the address

contract AddOrRemoveAdminVotingBuilder{
    event NewContract(address contractAddress);

    function build(
        address pPlatform,
        address pAdminManager
    )
        external
    {
        emit NewContract(address(
            new AddOrRemoveAdminVoting(
                pPlatform, 
                pAdminManager
            )
        ));
    }
}

Then you can fetch the logs (which are available in the transaction receipt if you watch for that) to get the address of the created contract.

  • Thanks, this looks simple indeed, probably will implement your solution, but I'm still curious how can I solve the topic's problem because I might need it in the future. – Radeg90 Dec 6 at 15:06
  • Logs are the only way. Since you can't get return values from top level functions (e.g. the function called directly in the transaction) due to the value not being in the transaction receipt, that wont work. Then the normal way to get created contract addresses, the contractAddress field in the receipt, only works if the transaction is a contract creation transaction (i.e. a tx creating a contract, not a contract creating a contract). If the contract is created within a contract, contractAddress isn't used. It makes sense when you realize you can create many contracts in a tx. – flygoing Dec 6 at 15:12

If you use getTransactionreceipt, then you can use the contractAddress property.

getTransactionReceipt(hash, function(error, result) {
    console.log("address =  " + result.contractAddress);
});

Documentation

  • Unfortunately that solution didn't help. Did this and got bunch of "null" in the console. – Radeg90 Dec 6 at 14:34
  • If you get null it means it is not a contract creation but a "normal" transaction. (from my knowledge) – Andromelus Dec 6 at 14:41
  • Sorry for misunderstanding. The problem is as shown in the main post. I have a contract with function which creates a new contract and returns it address. When I'm calling that function from web3 script I want to receive the address of that newly created contract. As far as I know I have to wait until mining is done and then I can somehow access return data. But I have no idea how. – Radeg90 Dec 6 at 14:44
  • Oh, ok, I did not get it. Sorry, I have never tried it so can't really help you there. – Andromelus Dec 6 at 15:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.