2

new to solidity.

Trying to understand the better approach for using a mapping storage in factory contract.

2 options:

  1. Use mapping(address => Contract[])
  2. Use mapping(address => address[])

Sample factory:

contract CampaignFactory {

    mapping(address => Campaign[]) campaigns;

    function createCampaign(uint min_contrib) public {
        Campaign newCampaign = new Campaign(min_contrib, msg.sender);
        campaigns[msg.sender].push(newCampaign);
    }

    function getCampaignsByAddress(address creator) public view returns (Campaign[] memory) {
        return campaigns[creator];
    }
}

As i understand, the first option suppose to use more storage space to store the whole contract instance. However, for both options createCampaign cost (remix estimation) is the same +- 660000 gas. Additionally, getCampaignsByAddress with the return type Campaign[] strangely returns only addresses instead of full instances (so i suppose only the address is stored).

Question: what difference does it make to use option #1 or #2?

3

There's no difference except that you'll get more type checking help from the Solidity compiler if you use Campaign[].

In both cases, as you noted, the only thing stored is an address. Using the address type just means the compiler can't help you if you accidentally try to store a non-Campaign in your array.

So my advice is to use Campaign[].

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.