i'm new to solidity and trying to understand the better storage option for the factory contract. i want:
- to be able to get all deployed contracts.
- to be able to get all deployed contracts by creator
The option i see is to create an array to track all contracts and mapping to reference the creators, ex:
contract CampaignFactory {
Campaign[] campaigns;
mapping(address => Campaign[]) campaignsByOwner;
function createCampaign(uint min_contrib) public {
Campaign newCampaign = new Campaign(min_contrib, msg.sender);
campaignsByOwner[msg.sender].push(newCampaign);
campaigns.push(newCampaign);
}
function getAllCampaigns() public view returns (Campaign[] memory) {
return campaigns;
}
function getCampaignByAddress(address creator) public view returns (Campaign[] memory) {
return campaignsByOwner[creator];
}
}
alternatively, i could use a struct like:
struct CampaignDeployed() {
address creator;
address[] campaigns;
}
but in that case as i understand, to get a campaigns by owner i will have to iterate over all in for cycle, which is something possibly too expensive (?).
So, the questions is, what is the typical approach to tackle such problem? does the solution with array + mapping acceptable?
getCampaignByAddress()theoretically, but practically there will be to much constraints to use it - all the creator addresses not always available when this call need to be made for example. Thanks for list, seen that. – user1935987 Dec 19 '18 at 21:34