Skip to content

Instantly share code, notes, and snippets.

@alex-damjanovic
Created October 19, 2021 20:03
Show Gist options
  • Select an option

  • Save alex-damjanovic/6c03244ebc5e6750c079e626c3cac4ad to your computer and use it in GitHub Desktop.

Select an option

Save alex-damjanovic/6c03244ebc5e6750c079e626c3cac4ad 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=
// SPDX-License-Identifier: MIT;
pragma solidity ^0.8.0;
/// @title DonationPlatform
/// @author Aleksandar
/// @notice Can only be used for the most basic simulation
/// @dev So far didnt spot any errors
import "github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
contract DonationPlatform is Ownable {
/// @notice Grouping campaign attributes
struct Campaign {
uint256 id;
string name;
string desc;
uint256 goal;
uint256 deadline;
uint256 raised;
bool isComplete;
}
/// @notice Campaign count so that the index of campaigns starts with 1
uint256 campaignCount = 0;
mapping (uint => Campaign) public campaigns;
/// @notice addingCampaign and storing it
function addCampaign(
string memory name, string memory desc,
uint goal, uint deadline,
uint raised, bool isComplete
)
public onlyOwner {
campaignCount +=1;
campaigns[campaignCount] = Campaign(campaignCount, name, desc, goal,deadline + block.timestamp , raised, isComplete);
}
/// @notice Donate function if the donations exceed goal it returns the extra funds to the sender
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;
payable(msg.sender).transfer(_amount);
} else if (campaigns[id].raised == campaigns[id].goal) campaigns[id].isComplete = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment