factory contract storage patterns












3















i'm new to solidity and trying to understand the better storage option for the factory contract. i want:




  1. to be able to get all deployed contracts.

  2. 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?










share|improve this question



























    3















    i'm new to solidity and trying to understand the better storage option for the factory contract. i want:




    1. to be able to get all deployed contracts.

    2. 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?










    share|improve this question

























      3












      3








      3








      i'm new to solidity and trying to understand the better storage option for the factory contract. i want:




      1. to be able to get all deployed contracts.

      2. 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?










      share|improve this question














      i'm new to solidity and trying to understand the better storage option for the factory contract. i want:




      1. to be able to get all deployed contracts.

      2. 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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 19 '18 at 20:48









      user1935987user1935987

      1597




      1597






















          2 Answers
          2






          active

          oldest

          votes


















          2














          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/.






          share|improve this answer































            1














            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.






            share|improve this answer
























            • 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











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            2














            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/.






            share|improve this answer




























              2














              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/.






              share|improve this answer


























                2












                2








                2







                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/.






                share|improve this answer













                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/.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 19 '18 at 20:52









                smarxsmarx

                18.1k1718




                18.1k1718























                    1














                    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.






                    share|improve this answer
























                    • 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
















                    1














                    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.






                    share|improve this answer
























                    • 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














                    1












                    1








                    1







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 19 '18 at 21:23









                    0Alic0Alic

                    313




                    313













                    • 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

















                    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


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    Origin of the phrase “under your belt”?