web3 - function returning new contract's address

by Radeg90   Last Updated December 06, 2018 14:28 PM

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?

Tags : web3js


Answers 1


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

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

Documentation

Andromelus
Andromelus
December 06, 2018 14:24 PM

Related Questions


Updated November 19, 2018 05:28 AM

Updated December 08, 2018 16:28 PM

Updated June 25, 2017 22:28 PM

Updated July 01, 2017 13:28 PM

Updated June 24, 2017 11:28 AM