Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.4 kB
2
Indexable
Never
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
pragma abicoder v2;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./interfaces/Campain.sol";

contract Organizaiton is Ownable, Campain {

    using SafeMath for uint;
    using SafeERC20 for IERC20;

    Campaign[] public campaigns;

    function setCampainStatus(uint _campainId, Status _status) private Ownable {
        campaigns[_campainId].status = _status;
    }

    function isCampaignActive(uint _campaignId) override public view returns(bool) {
        return campaigns[_campaignId].status == Status.Active;
    }

    function getCampaign(uint _campaignId) override public view returns(Campaign memory) {
        return campaigns[_campaignId];
    }

    function createCampaign(address payable _beneficiary, Status _status, string calldata uri) override external {
        address payable beneficiary;
        if(_beneficiary != address(0)) {
            beneficiary = _beneficiary;
        } else {
            beneficiary = msg.sender;
        }

        Campaign memory campaign = Campaign(_endTimestamp, beneficiary, uri);
        uint campaignId = campaigns.length;

        campaigns.push(campaign);

        emit CampaignCreated(campaignId, campaign);
    }

    function donate(uint _campaignId, address _token, uint _amount) override external {
        require(_campaignId < campaigns.length, "Campain::donate: Non existent campaign id provided");
        require(isCampaignActive(_campaignId), "Campain::donate: Campaign not active");

        address beneficiary = campaigns[_campaignId].beneficiary;

        IERC20(_token).transferFrom(msg.sender, beneficiary, _amount);
        emit Donation(_campaignId, _amount, _token);
    }

    function donateETH(uint _campaignId) override external payable {
        require(_campaignId < campaigns.length, "SupportChildren::donateETH: Non existent campaign id provided");
        require(isCampaignActive(_campaignId), "SupportChildren::donateETH: Campaign not active");
        require(msg.value > 0, "SupportChildren::donateETH: You must send ether");

        Campaign memory campaign = campaigns[_campaignId];
        campaign.beneficiary.transfer(msg.value);

        emit Donation(_campaignId, msg.value, address(0));
    }
}