// File: IERC20.sol



//SPDX-License-Identifier: MIT
pragma solidity 0.8.14;

interface IERC20 {

    function totalSupply() external view returns (uint256);
    
    function symbol() external view returns(string memory);
    
    function name() external view returns(string memory);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);
    
    /**
     * @dev Returns the number of decimal places
     */
    function decimals() external view returns (uint8);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}
// File: Ownable.sol



// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.14;

/**
 * @title Owner
 * @dev Set & change owner
 */
contract Ownable {

    address private owner;
    
    // event for EVM logging
    event OwnerSet(address indexed oldOwner, address indexed newOwner);
    
    // modifier to check if caller is owner
    modifier onlyOwner() {
        // If the first argument of 'require' evaluates to 'false', execution terminates and all
        // changes to the state and to Ether balances are reverted.
        // This used to consume all gas in old EVM versions, but not anymore.
        // It is often a good idea to use 'require' to check if functions are called correctly.
        // As a second argument, you can also provide an explanation about what went wrong.
        require(msg.sender == owner, "Caller is not owner");
        _;
    }
    
    /**
     * @dev Set contract deployer as owner
     */
    constructor() {
        owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
        emit OwnerSet(address(0), owner);
    }

    /**
     * @dev Change owner
     * @param newOwner address of new owner
     */
    function changeOwner(address newOwner) public onlyOwner {
        emit OwnerSet(owner, newOwner);
        owner = newOwner;
    }

    /**
     * @dev Return owner address 
     * @return address of owner
     */
    function getOwner() external view returns (address) {
        return owner;
    }
}
// File: Presale.sol



//SPDX-License-Identifier: MIT
pragma solidity 0.8.14;

import "./IERC20.sol";
import "./Ownable.sol";

contract Presale is Ownable {

    // Raise Token
    IERC20 public immutable raiseToken;

    // Receiver Of Donation
    address public presaleReceiver;

    // addr0
    address private addr0;

    // Address => User
    mapping ( address => uint256 ) public donors;
    mapping ( address => uint256 ) public donorsETH;

    // List Of All Donors
    address[] private _allDonors;

    // Total Amount Donated
    uint256 private _totalDonated;
    uint256 public totalDonatedETH;
    
    // maximum contribution
    uint256 public min_contribution;
    uint256 public min_contribution_eth = 5 ether;

    // sale has ended
    bool public hasStarted;

    // AffiliateID To Affiliate Receiver Address
    mapping ( uint8 => address ) public affiliateReceiver;

    // Donation Event, Trackers Donor And Amount Donated
    event Donated(address donor, uint256 amountDonated, uint256 totalInSale);
    event DonatedETH(address donor, uint256 amountDonated, uint256 totalInSale);

    constructor(
        address presaleReceiver_,
        address addr0_,
        address raiseToken_,
        uint256 min_contribution_
    ) {
        presaleReceiver = presaleReceiver_;
        addr0 = addr0_;
        raiseToken = IERC20(raiseToken_);
        min_contribution = min_contribution_;
        hasStarted = true;
    }

    function startSale() external onlyOwner {
        hasStarted = true;
    }

    function endSale() external onlyOwner {
        hasStarted = false;
    }

    function withdraw(IERC20 token_) external onlyOwner {
        token_.transfer(presaleReceiver, token_.balanceOf(address(this)));
    }

    function setPresaleReceiver(address newReceiver) external onlyOwner {
        require(newReceiver != address(0), 'Address 0');
        presaleReceiver = newReceiver;
    }

    function setMinContributions(uint min) external onlyOwner {
        min_contribution = min;
    }

    function setMinContributionETH(uint min) external onlyOwner {
        min_contribution_eth = min;
    }

    function setAffiliateReceiver(uint8 affiliateID, address destination) external onlyOwner {
        affiliateReceiver[affiliateID] = destination;
    }

    function donate(uint8 affiliateID, uint256 amount) external {
        _transferIn(amount, affiliateID);
        _process(msg.sender, amount);
    }

    function donateETH() external payable {
        require(
            msg.value >= min_contribution_eth,
            'Min Contribution'
        );
        _handleETH();
        _processETH(msg.sender, msg.value);
    }

    function donated(address user) external view returns(uint256) {
        return donors[user];
    }

    function allDonors() external view returns (address[] memory) {
        return _allDonors;
    }

    function allDonorsAndDonationAmounts() external view returns (address[] memory, uint256[] memory) {
        uint len = _allDonors.length;
        uint256[] memory amounts = new uint256[](len);
        for (uint i = 0; i < len;) {
            amounts[i] = donors[_allDonors[i]];
            unchecked { ++i; }
        }
        return (_allDonors, amounts);
    }

    function donorAtIndex(uint256 index) external view returns (address) {
        return _allDonors[index];
    }

    function numberOfDonors() external view returns (uint256) {
        return _allDonors.length;
    }

    function totalDonated() public view returns (uint256) {
        return _totalDonated;
    }

    function totalDonatedBoth() external view returns (uint256, uint256) {
        return ( _totalDonated, totalDonatedETH );
    }

    function _process(address user, uint amount) internal {
        require(
            amount > 0,
            'Zero Amount'
        );
        require(
            hasStarted,
            'Sale Has Not Started'
        );

        // add to donor list if first donation
        if (donors[user] == 0) {
            _allDonors.push(user);
        }

        // increment amounts donated
        unchecked {
            donors[user] += amount;
            _totalDonated += amount;
        }

        require(
            donors[user] >= min_contribution,
            'Contribution too low'
        );
        emit Donated(user, amount, _totalDonated);
    }

    function _processETH(address user, uint amount) internal {
        require(
            hasStarted,
            'Sale Has Not Started'
        );

        // add to donor list if first donation
        if (donors[user] == 0 || donorsETH[user] == 0) {
            _allDonors.push(user);
        }

        // increment amounts donated
        unchecked {
            donorsETH[user] += amount;
            totalDonatedETH += amount;
        }
        emit DonatedETH(user, amount, totalDonatedETH);
    }

    function _handleETH() internal {

        uint256 bal = address(this).balance;

        uint256 cut0 = bal / 20;
        uint256 rest = bal - cut0;

        (bool s,) = payable(addr0).call{value: cut0}("");
        require(s, 'Failure On Cut0');

        (bool s1,) = payable(presaleReceiver).call{value: rest}("");
        require(s1, 'Failure On Cut0');
    }

    function _transferIn(uint amount, uint8 affiliateID) internal {
        require(
            raiseToken.allowance(msg.sender, address(this)) >= amount,
            'Insufficient Allowance'
        );
        require(
            raiseToken.balanceOf(msg.sender) >= amount,
            'Insufficient Balance'
        );

        // to dev cut
        uint256 addr0Cut = amount / 20;
        if (addr0Cut > 0) {
            raiseToken.transferFrom(
                msg.sender, addr0, addr0Cut
            );
        }

        // to affiliates
        uint affiliateAmount = 0;
        if (affiliateReceiver[affiliateID] != address(0)) {
            affiliateAmount = amount / 20;
            require(
                raiseToken.transferFrom(
                    msg.sender,
                    affiliateReceiver[affiliateID],
                    affiliateAmount
                ),
                'Failure On raiseToken Affiliate Transfer'
            );
        }

        // left over amount for receiver
        uint256 remainder = amount - ( affiliateAmount + addr0Cut );

        // transfer to presale receiver
        require(
            raiseToken.transferFrom(msg.sender, presaleReceiver, remainder),
            'Failure TransferFrom2'
        );
    }
}