1

I am trying to create a factory contract that dynamically creates new instances of another contract. And I want the factory to return the newly created contract's address; however, when I test it in the Remix IDE, the function does not return anything. Here is my current code:

contract CreateTournament {
    Tournament public newTournament;
    address[] newTournaments;
    uint index = 0;

    function createNewTournament(string _title, uint32 
_maxParticipants, uint _entryFee, uint _surcharge) public 
returns(address) {
        newTournament = new Tournament(_title, _maxParticipants, 
_entryFee, _surcharge);
        newTournaments.push(newTournament);
        return newTournament;
    }

    function checkAddress(address tournament) public view returns(bool) 
{
        bool exists = false;

        for (uint i = 0; i < newTournaments.length; i++) {
            if (tournament == newTournaments[i]) {
                exists = true;
            }
        }

        return exists;
    }
}

Any help would be appreciated!

1

Functions that write data to the blockchain don't return any values. They return the transaction hash, so you won't be able to get the value from outside Solidity.

You can have the function fire an Event and then capture the log from web3 once the transaction has been mined to get the address of the newly created contract.

| improve this answer | |

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