0

This is a solidity code for simple crowd funding project. The getCampaigns() function is supposed to only return the campaigns that have not past the deadline ie. are not expired. But it seems it is returning the campaigns that already have deadline expired as it shows in my frontend.

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

contract CrowdFunding {
    struct Campaign {
        address owner;
        string title;
        string description;
        // target amount to acheive
        uint256 target;
        uint256 deadline;
        uint256 amountCollected;
        string image;
        address[] donators;
        uint256[] donations;
    }
    mapping(uint256 => Campaign) public campaigns;
    
    uint256 public numberOfCampaigns = 0;
    function createCampaign(address _owner, string memory _title, string memory _description, 
    uint256 _target, uint256 _deadline , string memory _image) public returns (uint256){
    
        Campaign storage campaign = campaigns[numberOfCampaigns];
        require(campaign.deadline < block.timestamp, "Deadline should be a date in the future.");
        campaign.owner = _owner;
        campaign.title = _title;
        campaign.description = _description;
        campaign.target = _target;
        campaign.deadline = _deadline;
        campaign.amountCollected =  0;
        campaign.image =_image;
        numberOfCampaigns++;
        return (numberOfCampaigns-1) ;

    }
    function donateToCampaign(uint256 _id) public payable {
        uint256 amount = msg.value;
        Campaign storage campaign = campaigns[_id];
        campaign.donators.push(msg.sender);
        campaign.donations.push(amount);
        (bool sent,) = payable(campaign.owner).call{value: amount}("");
        if (sent){
            campaign.amountCollected = campaign.amountCollected + amount;
        }
    }

    function getDonators(uint256 _id) view public returns (address[] memory, uint256[] memory){
        return (campaigns[_id].donators, campaigns[_id].donations);
    }
    function getCampaigns() public view returns(Campaign[] memory) {
    uint256 validCampaignsCount = 0;
    for (uint256 i = 0; i < numberOfCampaigns; i++) {
        if (campaigns[i].deadline >= block.timestamp) {
            validCampaignsCount++;
        }
    }
        Campaign[] memory allCampaigns = new Campaign[](validCampaignsCount);

        uint256 index = 0;

        for (uint256 i = 0; i <numberOfCampaigns; i++){
            // checking if the campaign's deadline is in future
            if (campaigns[i].deadline >= block.timestamp){
                // fetching the specific campaign from storage
                // and populating it to allCampaigns
                Campaign storage item = campaigns[i];
                allCampaigns[index] = item;
                index++;
            }
        }
        return allCampaigns;
    }
}
2
  • I recommend you remove code as much as possible, until you find exactly where the problem is. Whenever I do that, I usually find the bug by myself. Commented May 24 at 12:33
  • In the line 24, probably you mean: require(deadline > block.timestamp, "Deadline should be a date in the future."). I tested the contract and everything is working fine here, I could not reproduce the error, consider to review you frontend.
    – Rafael
    Commented May 24 at 13:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.