Created
November 21, 2021 21:26
-
-
Save alex-damjanovic/1bd3be3d566f25c7e66ff82d6661b581 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.0; | |
| /// @title DonationPlatform | |
| /// @author Aleks | |
| /// @dev Bug when returning funds , figured out events | |
| import "@openzeppelin/contracts/access/Ownable.sol"; | |
| contract DonationPlatform is Ownable { | |
| /// @notice Create campaigns and recieve funds | |
| /// @notice Campaign struct with propperties | |
| struct Campaign { | |
| string name; | |
| string description; | |
| uint256 goal; | |
| uint256 deadline; | |
| uint256 raised; | |
| bool isComplete; | |
| } | |
| /// @notice number of campaings initially 0 and campaoigns mapping | |
| uint256 campaignCount; | |
| mapping (uint => Campaign) public campaigns; | |
| /// @notice events for creation and donations | |
| event CampaignCreated(uint CampaignId, Campaign campaign_info ); | |
| event DonationSent(uint campaignId, uint value, address sender); | |
| /// @notice creates donation campaign with properties | |
| /// @param _name, _description, _goal, _deadline raised and complete set to default | |
| function addCampaign( | |
| string memory _name, string memory _description, | |
| uint _goal, uint _deadline | |
| ) | |
| public onlyOwner { | |
| campaignCount +=1; | |
| campaigns[campaignCount] = Campaign(_name, _description, _goal, _deadline + block.timestamp, 0, false ); | |
| emit CampaignCreated(campaignCount, campaigns[campaignCount]); | |
| } | |
| /// @notice donation function , adds msg.value to raised amount of corresponding campaign | |
| /// @param id Id of campaign | |
| /// @dev It doesnt return the change funds via call. | |
| function donate(uint id) payable public { | |
| require(block.timestamp < campaigns[id].deadline, "Campaign is over!"); | |
| require(!(campaigns[id].isComplete), "The goal was already achieved!"); | |
| campaigns[id].raised += msg.value; | |
| if (campaigns[id].raised + msg.value > campaigns[id].goal) { | |
| uint _amount = campaigns[id].raised - campaigns[id].goal; | |
| campaigns[id].raised -= _amount; | |
| campaigns[id].isComplete = true; | |
| (bool success,) = msg.sender.call{value:_amount}(""); | |
| require(success, "Transfer failed."); | |
| } else if (campaigns[id].raised == campaigns[id].goal) campaigns[id].isComplete = true; | |
| emit DonationSent(id, msg.value, msg.sender ); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment