0

I need to query a list of Campaign from smart contract to display on my dapp. It would be easily done in a traditional database (we have did this in the past using traditional db but now we want to migrate it to blokchain) but i am unaware of how this can be done efficiently in blockchain.

Obviously, I can't be afford to looping each of the campaign address, follow by loop the detail inside (it would be total disaster if i have let say 10k campaign). I am trying to make use of event log.

However, I still think that watching over 10,000 address in event log for each event needed (and keep increasing) and processing it in server after retrieval at a same time would be an issue with web3.

Any expert have been working on similar issue. Would appreciate if you could give some hint on this.

Note: The actual retrieval result list and filtering is far complicated than below (with our existing structure on traditional db) but I need to get some initial idea on how to approach this.

The data of campaign list to get back as follow.

[
    {
        campaignName: "string", (note that campaignName can be updated)
        minimumContribution: "int",
        campaignContributionWithMostContributionName: "string",
        campaignContributionWithMostContributorUserCount: "int",
        compaignContributorTotalUserCount: "int"
    }
]

Contract sample

contract CampaignFactory {
    address[] public deployedCampaigns;

    function createCampaign(uint minimum) public {
        address newCampaign = new Campaign(campaignName, minimum, msg.sender);
        deployedCampaigns.push(newCampaign);
    }

    function getDeployedCampaigns() public view returns (address[]) {
        return deployedCampaigns;
    }
}

contract Campaign {
    bytes32[] public contributionNameList;

    event UserContribution(address indexed _owner, bytes32 indexed _outcome, uint _tokenAmount);
    event CampaignContributionAdded(bytes32 indexed _id);

    function Campaign(string campaignName, uint minimum, address creator) public {
        manager = creator;
        minimumContribution = minimum;
    }

    function setCampaignName(string _campaignName) public {
        campaignName = _campaignName;
    }

    function addCampaignContribution(bytes32 contributionName) {
        ...
        emit CampaignContributionAdded(contributionName);
    }

    function addUserContribution(uint tokenAmount, bytes32 contributionName) {
        ...
        emit UserContribution(msg.sender, contributionName, tokenAmount);
    }
}
2

As you want to query large amount of data, indexing on log data will help to get the data faster. But you have to use cache layer as it is not that fast to query such large amount of data. You can use https://github.com/vulcanize/vulcanizedb Or you can go for other traditional DB for caching like couchDB

  • Hi, thanks for the feedback. It is a good suggestion but we are looking for a solution by smart contract design only if possible at current stage. A cache layer mechanism is last resort that we will opt to if it found out to be really deteriorate the performance of querying. – vincentsty Nov 17 '18 at 11:01
  • 1
    As of now there is no other solution. Btw if you are going for public ethereum chain, then you can check this out-cloud.google.com/blog/products/data-analytics/… – Ganeshdip Nov 17 '18 at 11:25
  • Sadly I'd also say there is no somehow performant way to process logs and events other than using an additional database. If you don't feel like hosting your own, you might want to have a look at eth.events (disclaimer, I'm the CTO ;-)). It's free and you can just query for events using ElasticSearch queries. – Sascha Goebel Nov 28 '18 at 11:59

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.