1

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?

2 Answers 2

0

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.

2
  • 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.
    – Radek_pl
    Dec 6, 2018 at 15:06
  • 1
    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.
    – natewelch_
    Dec 6, 2018 at 15:12
1

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

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

Documentation

4
  • Unfortunately that solution didn't help. Did this and got bunch of "null" in the console.
    – Radek_pl
    Dec 6, 2018 at 14:34
  • If you get null it means it is not a contract creation but a "normal" transaction. (from my knowledge) Dec 6, 2018 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.
    – Radek_pl
    Dec 6, 2018 at 14:44
  • Oh, ok, I did not get it. Sorry, I have never tried it so can't really help you there. Dec 6, 2018 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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