Created
October 26, 2021 20:42
-
-
Save DubovinaDj/464b0e1687e028b44d4192033e5960a6 to your computer and use it in GitHub Desktop.
This file contains 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.4; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "./NFT_ERC721.sol"; | |
contract Donation is Ownable { | |
address public immutable ADMIN; | |
struct Campaign { // grouped attributes | |
string name; | |
string description; | |
uint duration; | |
uint goal; | |
uint raised; | |
bool completed; | |
} | |
mapping(uint => Campaign) public campaigns; //mapping from uint to Campaign attributes (id to Campaign) | |
mapping(address => bool) public ownerDonated; // mapping from address to bool (address - true or false) | |
constructor() { | |
ADMIN = payable(msg.sender); | |
} | |
modifier onlyAdmin { | |
require(msg.sender == ADMIN, "Only admin can call this function"); | |
_; | |
} | |
// Create campaigns (only Admin can create campaigns) | |
function createCampaign( | |
uint _index, string memory _name, string memory _description, | |
uint _duration, uint _goal | |
) public onlyAdmin { | |
uint raised = 0; | |
bool completed = false; | |
campaigns[_index] = Campaign(_name, _description, _duration + block.timestamp, _goal, raised, completed); | |
} | |
//Function for donation | |
function donate(uint _index) public payable { | |
require (block.timestamp < campaigns[_index].duration, "Campaign Failed!"); | |
require(campaigns[_index].raised < campaigns[_index].goal,"Goal achieved"); | |
campaigns[_index].raised += msg.value; | |
if (campaigns[_index].raised + msg.value > campaigns[_index].goal) { | |
uint _amount = campaigns[_index].raised - campaigns[_index].goal; | |
campaigns[_index].raised -= _amount; | |
campaigns[_index].completed = true; | |
payable(msg.sender).call{value: _amount}; | |
} else if (campaigns[_index].raised == campaigns[_index].goal) campaigns[_index].completed = true; | |
if (!ownerDonated[msg.sender]){ //Donator will get NFT as gift only first time | |
ownerDonated[msg.sender] = true; | |
DonateAndTakeNFT (0x9bF88fAe8CF8BaB76041c1db6467E7b37b977dD7).mint(msg.sender); //Calling other contracts( NFT_ERC721.sol ) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment