// File: hourglassnew2.sol



// Hearken, weary souls, and heed the tale of the Hourglass, a mystic contract wrought upon Ethereum's realm by the creator of the REAPER'S GAMBIT, where $RG tokens dwell. 
// In this somber domain, mortals may submit their tokens, a minimum of 300,000, as tribute to the contract's dark embrace.
// In exchange, a shadowy reduction doth consume the tokens, ere long, and bestow upon the depositor a fleeting taste of immortality, granting a brief reprieve from the Reaper's cold grasp.
// Should one's deposit, combined with the tokens already ensnared, exceed a third of the total tokens entrapped, the reduction shall be halved, thus lessening the merciless toll exacted by the contract's malevolent grasp. 
// Upon withdrawal, one may select any address they desire, a final act of defiance in the face of Death.

// The tokens, once offered, are shackled and bound for 4800 blocks, unable to flee the grasp of this unholy contract. 
// As the lock is lifted, these captive tokens may be withdrawn, though not without a price, 0.09% for every 2400 block cycles. 
// The reduction, cruel as fate itself, shall be cast upon them, leaving but a fraction to return to the mortal world.

// Behold the Reduction Pool, a forsaken pit that gathers the tokens consumed by the merciless reduction. 
// To empty this accursed pool, one must possess 30 times the pool's dark bounty in their balance, and still have tokens in the hourglass. 
// Then, two-thirds shall be cast into the abyss, forever lost to the dead, whilst the remaining third shall find its way to a chosen destination.

// In the gloom of this contract, queries may be whispered, revealing the secrets of one's deposit, the foreboding reduction pool, 
// and the wretched souls who dare to partake in this unhallowed pact. 
// Thus, the Hourglass sinister workings are laid bare, a testament to the inescapable reduction and the inevitability of fate.
//
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Hourglass {
    struct DepositInfo {
        uint256 amount;
        uint256 blockNumber;
        uint256 reductionPercentage;
    }

    mapping(address => DepositInfo) private _deposits;

    IERC20 private _token;

    uint256 private _totalDeposits;
    mapping(uint256 => address) private _depositers;
    mapping(address => uint256) private _depositerIndex;
    uint256 private _totalDepositedAmount;
    uint256 private _reductionPool;

    constructor(IERC20 token) {
        require(address(token) == 0x2C91D908E9fab2dD2441532a04182d791e590f2d, "Invalid token address");
        _token = token;
    }

    modifier onlyDepositor() {
        require(_deposits[msg.sender].amount > 0, "Not a depositor");
        _;
    }

    function deposit(uint256 amount) external {
        require(amount >= 300000 * 1e18, "Minimum deposit is 300,000 tokens");
        require(_token.allowance(msg.sender, address(this)) >= amount, "Allowance not sufficient");

        DepositInfo storage depositInfo = _deposits[msg.sender];
        uint256 previousDeposit = depositInfo.amount;

        depositInfo.amount += amount;
        depositInfo.blockNumber = block.number;

        uint256 reductionPercentage = 9e14;
        if ((depositInfo.amount + amount) > _totalDepositedAmount / 3) {
            reductionPercentage /= 2;
        }
        depositInfo.reductionPercentage = reductionPercentage;

        if (previousDeposit == 0) {
            _totalDeposits += 1;
            _depositers[_totalDeposits] = msg.sender;
            _depositerIndex[msg.sender] = _totalDeposits;
        }

        _totalDepositedAmount += amount;
        _token.transferFrom(msg.sender, address(this), amount);
    }

    function withdrawTo(address recipient) external onlyDepositor {
        require(block.number - _deposits[msg.sender].blockNumber >= 4800, "Tokens are locked");

        uint256 blocksSinceDeposit = block.number - _deposits[msg.sender].blockNumber;
        uint256 reductionCycles = blocksSinceDeposit / 2400;

        uint256 reduction = (_deposits[msg.sender].amount * _deposits[msg.sender].reductionPercentage * reductionCycles) / 1e18;
        _reductionPool += reduction;
        uint256 withdrawAmount = _deposits[msg.sender].amount - reduction;

        _token.transfer(recipient, withdrawAmount);
        
        _totalDepositedAmount -= _deposits[msg.sender].amount;
        uint256 removedIndex = _depositerIndex[msg.sender];
        if (removedIndex < _totalDeposits) {
            address lastDepositer = _depositers[_totalDeposits];
            _depositers[removedIndex] = lastDepositer;
            _depositerIndex[lastDepositer] = removedIndex;
        }
        delete _depositers[_totalDeposits];
        delete _depositerIndex[msg.sender];
        delete _deposits[msg.sender];
        _totalDeposits -= 1;
    }

    function emptyReductionPool(address specifiedAddress) external onlyDepositor {
    require(_reductionPool > 0, "Reduction pool is empty, cannot empty pool");
    require(_token.balanceOf(msg.sender) >= _reductionPool * 30, "Insufficient token balance to empty pool");

    uint256 oneThird = _reductionPool / 3;
    uint256 twoThird = oneThird * 2;

    _token.transfer(address(0x000000000000000000000000000000000000dEaD), twoThird);
    _token.transfer(specifiedAddress, oneThird);

    _reductionPool = 0;
}

    function getDepositInfo(address depositor) external view returns (uint256 amount, uint256 blockNumber, uint256 reductionPercentage) {
        DepositInfo memory depositInfo = _deposits[depositor];
        return (depositInfo.amount, depositInfo.blockNumber, depositInfo.reductionPercentage);
    }

    function getReductionPool() external view returns (uint256) {
        return _reductionPool;
    }

    function getHourglassInfo() external view returns (address[] memory depositors, uint256 numberOfDepositors, uint256 totalDeposited) {
    depositors = new address[](_totalDeposits);
    for (uint256 i = 1; i <= _totalDeposits; i++) {
        depositors[i - 1] = _depositers[i];
    }
    numberOfDepositors = _totalDeposits;
    totalDeposited = _totalDepositedAmount;
    return (depositors, numberOfDepositors, totalDeposited);
    }
}


// File: @openzeppelin/contracts/token/ERC20/IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @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);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, 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 `from` to `to` 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 from,
        address to,
        uint256 amount
    ) external returns (bool);
}