factory contract storage patterns
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?
solidity solidity-best-practices
add a comment |
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?
solidity solidity-best-practices
add a comment |
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?
solidity solidity-best-practices
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?
solidity solidity-best-practices
solidity solidity-best-practices
asked Dec 19 '18 at 20:48
user1935987user1935987
1597
1597
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Your solution is good, but if the arrays get too big, you may not be able to return them all at once. Different nodes impose different limits, but there's often a timeout and a maximum allowed payload on calls to view functions.
You may be interested in pagination (specifically the "paging through an array" section): https://programtheblockchain.com/posts/2018/04/20/storage-patterns-pagination/.
add a comment |
Check this simple storage patterns in Solidity. It talks about pros/cons using different combinations of data structures.
In may opinion, it depends on your needs.
For example, can the need "to be able to get all deployed contracts" be solved by calling externally getCampaignByAddress()
multiple times? It would involve multiple smart contract calls and the knowledge about all addresses with at least a campaign, but may save your support array data struct.
I struggled a lot about this issue before, hope my contribution helps.
it is possible to get all the data by callinggetCampaignByAddress()
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
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "642"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f64349%2ffactory-contract-storage-patterns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your solution is good, but if the arrays get too big, you may not be able to return them all at once. Different nodes impose different limits, but there's often a timeout and a maximum allowed payload on calls to view functions.
You may be interested in pagination (specifically the "paging through an array" section): https://programtheblockchain.com/posts/2018/04/20/storage-patterns-pagination/.
add a comment |
Your solution is good, but if the arrays get too big, you may not be able to return them all at once. Different nodes impose different limits, but there's often a timeout and a maximum allowed payload on calls to view functions.
You may be interested in pagination (specifically the "paging through an array" section): https://programtheblockchain.com/posts/2018/04/20/storage-patterns-pagination/.
add a comment |
Your solution is good, but if the arrays get too big, you may not be able to return them all at once. Different nodes impose different limits, but there's often a timeout and a maximum allowed payload on calls to view functions.
You may be interested in pagination (specifically the "paging through an array" section): https://programtheblockchain.com/posts/2018/04/20/storage-patterns-pagination/.
Your solution is good, but if the arrays get too big, you may not be able to return them all at once. Different nodes impose different limits, but there's often a timeout and a maximum allowed payload on calls to view functions.
You may be interested in pagination (specifically the "paging through an array" section): https://programtheblockchain.com/posts/2018/04/20/storage-patterns-pagination/.
answered Dec 19 '18 at 20:52
smarxsmarx
18.1k1718
18.1k1718
add a comment |
add a comment |
Check this simple storage patterns in Solidity. It talks about pros/cons using different combinations of data structures.
In may opinion, it depends on your needs.
For example, can the need "to be able to get all deployed contracts" be solved by calling externally getCampaignByAddress()
multiple times? It would involve multiple smart contract calls and the knowledge about all addresses with at least a campaign, but may save your support array data struct.
I struggled a lot about this issue before, hope my contribution helps.
it is possible to get all the data by callinggetCampaignByAddress()
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
add a comment |
Check this simple storage patterns in Solidity. It talks about pros/cons using different combinations of data structures.
In may opinion, it depends on your needs.
For example, can the need "to be able to get all deployed contracts" be solved by calling externally getCampaignByAddress()
multiple times? It would involve multiple smart contract calls and the knowledge about all addresses with at least a campaign, but may save your support array data struct.
I struggled a lot about this issue before, hope my contribution helps.
it is possible to get all the data by callinggetCampaignByAddress()
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
add a comment |
Check this simple storage patterns in Solidity. It talks about pros/cons using different combinations of data structures.
In may opinion, it depends on your needs.
For example, can the need "to be able to get all deployed contracts" be solved by calling externally getCampaignByAddress()
multiple times? It would involve multiple smart contract calls and the knowledge about all addresses with at least a campaign, but may save your support array data struct.
I struggled a lot about this issue before, hope my contribution helps.
Check this simple storage patterns in Solidity. It talks about pros/cons using different combinations of data structures.
In may opinion, it depends on your needs.
For example, can the need "to be able to get all deployed contracts" be solved by calling externally getCampaignByAddress()
multiple times? It would involve multiple smart contract calls and the knowledge about all addresses with at least a campaign, but may save your support array data struct.
I struggled a lot about this issue before, hope my contribution helps.
answered Dec 19 '18 at 21:23
0Alic0Alic
313
313
it is possible to get all the data by callinggetCampaignByAddress()
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
add a comment |
it is possible to get all the data by callinggetCampaignByAddress()
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
it is possible to get all the data by calling
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
it is possible to get all the data by calling
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
add a comment |
Thanks for contributing an answer to Ethereum Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f64349%2ffactory-contract-storage-patterns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown