// 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: SeedRound.sol



//SPDX-License-Identifier: MIT
pragma solidity 0.8.14;

import "./IERC20.sol";
import "./Ownable.sol";

contract SeedRound is Ownable {

    // Donor Structure
    struct Donor {
        uint256 totalDonated;
        mapping ( uint8 => uint256 ) donatedPerStage;
    }

    // Address => User
    mapping ( address => Donor ) public donors;

    // List Of All Donors
    address[] private _allDonors;

    // Total Amount Donated
    uint256 private _totalDonated;

    // Receiver Of Donation
    address private presaleReceiver = 0xc792e5FA3539FD1b469383D61361fFc01b91935f;

    // maximum contribution
    uint256 public min_contribution = 20 * 10**6;

    // soft / hard cap
    uint256 public hardCap = 3_000_000 * 10**6;

    // Stage Structure
    struct Stage {
        uint256 hardCap;
        uint256 totalDonated;
    }

    // Stage => Stage Structure
    mapping ( uint8 => Stage ) public stages;

    // Current Stage
    uint8 public currentStage;

    // sale has ended
    bool public hasStarted;

    // Raise Token
    IERC20 public raiseToken = IERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7);

    // Donation Event, Trackers Donor And Amount Donated
    event Donated(address donor, uint256 amountDonated, uint256 totalInSale, uint256 totalInStage);


    constructor () {

        stages[0].hardCap = 2_000_000 * 10**6;
        stages[1].hardCap = 1_250_000 * 10**6;
        stages[2].hardCap = 1_500_000 * 10**6;
        stages[3].hardCap = 1_750_000 * 10**6;
        stages[4].hardCap = 2_000_000 * 10**6;
        stages[5].hardCap = 2_250_000 * 10**6;
        stages[6].hardCap = 2_500_000 * 10**6;
        stages[7].hardCap = 2_750_000 * 10**6;
        stages[8].hardCap = 3_000_000 * 10**6;
        stages[9].hardCap = 19_000_000 * 10**6;

    }

    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 setMinContribution(uint min) external onlyOwner {
        min_contribution = min;
    }

    function setHardCap(uint8 stage, uint hardCap_) external onlyOwner {
        stages[stage].hardCap = hardCap_;
    }

    function setRaiseToken(address token) external onlyOwner {
        require(
            token != address(0),
            'Zero Address'
        );
        raiseToken = IERC20(token);
    }

    function setPresaleReceiver(address newReceiver) external onlyOwner {
        require(
            newReceiver != address(0),
            'Zero Address'
        );
        presaleReceiver = newReceiver;
    }

    function nextStage() external onlyOwner {
        currentStage++;
    }

    function donate(uint256 amount) external {
        uint received = _transferIn(amount);
        _process(msg.sender, received);
    }

    function currentHardCap() external view returns (uint256) {
        return stages[currentStage].hardCap;
    }

    function currentRaise() external view returns (uint256) {
        return stages[currentStage].totalDonated;
    }

    function donated(address user, uint8 stage) external view returns(uint256) {
        return donors[user].donatedPerStage[stage];
    }

    function totalUserDonated(address user) external view returns(uint256) {
        return donors[user].totalDonated;
    }

    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]].totalDonated;
            unchecked { ++i; }
        }
        return (_allDonors, amounts);
    }

    function amountDonatedAtEachStage(address user) external view returns (uint256[] memory) {
        uint len = currentStage + 1;
        uint256[] memory amounts = new uint256[](len);

        for (uint i = 0; i < len;) {
            amounts[i] = donors[user].donatedPerStage[uint8(i)];
            unchecked { ++i; }
        }
        return (amounts);
    }

    function donorAtIndex(uint256 index) external view returns (address) {
        return _allDonors[index];
    }

    function numberOfDonors() external view returns (uint256) {
        return _allDonors.length;
    }

    function totalDonated() external view returns (uint256) {
        return _totalDonated;
    }

    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].totalDonated == 0) {
            _allDonors.push(user);
        }

        // increment amounts donated
        unchecked {
            stages[currentStage].totalDonated += amount;
            donors[user].totalDonated += amount;
            donors[user].donatedPerStage[currentStage] += amount;
            _totalDonated += amount;
        }

        require(
            donors[user].donatedPerStage[currentStage] >= min_contribution,
            'Contribution too low'
        );
        require(
            stages[currentStage].totalDonated <= stages[currentStage].hardCap,
            'Hard Cap Reached'
        );
        emit Donated(user, amount, _totalDonated, stages[currentStage].totalDonated);
    }

    function _transferIn(uint amount) internal returns (uint256) {
        require(
            raiseToken.allowance(msg.sender, address(this)) >= amount,
            'Insufficient Allowance'
        );

        // to presale recipient
        raiseToken.transferFrom(
            msg.sender,
            presaleReceiver,
            amount
        );
        
        return amount;
    }
}