Skip to content

Instantly share code, notes, and snippets.

@alex-damjanovic
Created October 25, 2021 16:40
Show Gist options
  • Select an option

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

Select an option

Save alex-damjanovic/e3543c81f414b2fb77c0e61b29343801 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
/// @notice Can only be used for the most basic simulation
/// @dev So far didnt spot any errors
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "./ucenje.sol";
contract DonationPlatform is Ownable {
/// @notice Grouping campaign attributes
struct Campaign {
uint256 id;
string name;
string description;
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;
mapping (address => bool) public hasDonatedBefore;
/// @notice addingCampaign and storing it
function addCampaign(
string memory name, string memory description,
uint goal, uint deadline
)
public onlyOwner {
uint raised = 0;
bool isComplete = false;
campaignCount +=1;
campaigns[campaignCount] = Campaign(campaignCount, name, description, 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;
(bool success, ) = msg.sender.call{value:_amount}("");
require(success, "Transfer failed.");
} 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