0

I'm very new to blockchain/Ethereum development and would like to allow users to directly deploy contracts from a site using MetaMask, however, I want to make sure that I can keep a record of contract addresses to be cross-referenced with user addresses. I would then allow users to view their open contracts based on the MetaMask account number.

I will also need to send data to the smart contract from the server at certain points, so it's crucial that I be able to record all smart contract addresses.

I don't want to run the risk of having to capture new contract addresses in the browser and send them to the server and, from what I'm seeing, I'm not sure that I can communicate data from a smart contract to an outside source without first having the address. So what options do I have?

If all else fails, I can design a different structure to centrally manage contracts, but this could be a lot more complicated, use more gas, etc.

0

If you use a smart-contract factory model to create the smart-contracts on the fly, you'd know any addresses of child contracts in advance due to how they're generated.

To make it simpler still you could have the parent contract store any generated addresses, meaning you can query this whenever required in order to have a complete collection of all deployed contracts.

contract Factory {

    address[] public contracts;

    function createContract() public {
        address childContract = new Contract();
        contracts.push(childContract);
    } 

    function getContractsLength() public returns (uint) {
        return contracts.length;
    }
}

contract Contract {
    // Code...
}

Here, the getter for the contracts array length makes it easier for your front end to retrieve all stored contract addresses.

  • Very cool, I was not aware that I could generate a contract from within a contract. It adds a layer of complexity, but at least there’s a solution. Thanks. – SuperCodeBrah Jul 7 '18 at 10:35
  • Just tested it in Remix. Success! – SuperCodeBrah Jul 7 '18 at 11:45
  • Excellent! It does add a layer of complexity and so of course gas overhead, but it will guarantee you have a permanent record of all contract addresses etched into the blockchain. Which by the sounds of it is exactly what you require! – Greg Kapka Jul 8 '18 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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