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