// File: src/Lootto.sol



// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.19;

import {Owned} from "solmate/auth/Owned.sol";
import {IUniswapV2Router02} from "v2-periphery/interfaces/IUniswapV2Router02.sol";

// PAIN LOOTTO
// The Memecoin Misfortune Extravaganza!

// twitter: https://twitter.com/loottoerc20
// telegram: https://t.me/visitthedungeon
contract Lootto is Owned {
    event Transfer(address indexed from, address indexed to, uint256 amount);

    event Approval(address indexed owner, address indexed spender, uint256 amount);

    event IsAddressExcludedChanged(address indexed user, bool value);

    error MaxBuyExceeded(uint256 maxBuy, uint256 amount);
    error TradingNotEnabled();
    error PainAlreadyInflicted();

    string public name;

    string public symbol;

    uint8 public immutable decimals;

    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;

    mapping(address => mapping(address => uint256)) public allowance;

    uint256 internal immutable INITIAL_CHAIN_ID;

    bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;

    mapping(address => uint256) public nonces;

    mapping(address => bool) public isAddressExcluded;
    uint256 private constant EMOTIONAL_DAMAGE = 3000;
    uint256 private constant EVERY_NTH_TRANSFER_TAXED = 5;
    uint256 private constant HUNDRED_PERCENT = 10000;
    address private immutable TREASURY;
    IUniswapV2Router02 private immutable router;
    address private immutable WETH;
    uint216 private transferNumber;

    uint256 private constant MAX_BUY = 1e26;
    uint256 private constant MAX_BUY_DURATION = 10 minutes;
    uint40 private MAX_BUY_END_TIME;

    constructor(address treasury, address uniV2Router, address weth) Owned(msg.sender) {
        name = "PAIN LOOTTO";
        symbol = "LOOTTO";
        decimals = 18;

        INITIAL_CHAIN_ID = block.chainid;
        INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();

        _mint(msg.sender, 1e28);

        isAddressExcluded[msg.sender] = true;
        isAddressExcluded[treasury] = true;
        isAddressExcluded[address(this)] = true;

        TREASURY = treasury;

        router = IUniswapV2Router02(uniV2Router);
        allowance[address(this)][uniV2Router] = type(uint256).max;

        WETH = weth;
    }

    function approve(address spender, uint256 amount) public returns (bool) {
        allowance[msg.sender][spender] = amount;

        emit Approval(msg.sender, spender, amount);

        return true;
    }

    function transfer(address to, uint256 amount) public returns (bool) {
        balanceOf[msg.sender] -= amount;

        if (!isAddressExcluded[msg.sender]) {
            amount = _processEmotionalDamage(amount);

            if (MAX_BUY_END_TIME == 0) revert TradingNotEnabled();
        }

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint224 value.
        // taxAmount is always less than amount
        unchecked {
            uint256 newAmount = amount + balanceOf[to];

            if (!isAddressExcluded[to]) {
                _revertOnMaxBuyExceeded(newAmount);
            }

            balanceOf[to] = newAmount;
        }

        emit Transfer(msg.sender, to, amount);

        return true;
    }

    function transferFrom(address from, address to, uint256 amount) public returns (bool) {
        uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.

        if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;

        balanceOf[from] -= amount;

        if (!isAddressExcluded[from]) {
            amount = _processEmotionalDamage(amount);
        }

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint224 value.
        // taxAmount is always less than amount
        unchecked {
            uint256 newAmount = amount + balanceOf[to];

            if (!isAddressExcluded[to]) {
                _revertOnMaxBuyExceeded(newAmount);

                if (MAX_BUY_END_TIME == 0) revert TradingNotEnabled();
            }

            balanceOf[to] = newAmount;
        }

        emit Transfer(from, to, amount);

        return true;
    }

    function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
        public
    {
        require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");

        // Unchecked because the only math done is incrementing
        // the owner's nonce which cannot realistically overflow.
        unchecked {
            address recoveredAddress = ecrecover(
                keccak256(
                    abi.encodePacked(
                        "\x19\x01",
                        DOMAIN_SEPARATOR(),
                        keccak256(
                            abi.encode(
                                keccak256(
                                    "Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
                                ),
                                owner,
                                spender,
                                value,
                                nonces[owner]++,
                                deadline
                            )
                        )
                    )
                ),
                v,
                r,
                s
            );

            require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");

            allowance[recoveredAddress][spender] = value;
        }

        emit Approval(owner, spender, value);
    }

    function DOMAIN_SEPARATOR() public view returns (bytes32) {
        return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
    }

    function computeDomainSeparator() internal view returns (bytes32) {
        return keccak256(
            abi.encode(
                keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
                keccak256(bytes(name)),
                keccak256("1"),
                block.chainid,
                address(this)
            )
        );
    }

    function _mint(address to, uint256 amount) internal {
        totalSupply += amount;

        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(address(0), to, amount);
    }

    function _processEmotionalDamage(uint256 amount) internal returns (uint256) {
        unchecked {
            uint256 newTransferNumber = ++transferNumber;

            if (newTransferNumber % 5 != 0) {
                return amount;
            }
            // cant overflow because:
            // block.timestamp <= minTaxOn
            // all numbers are small enough for type(uint256).max
            uint256 taxAmount = EMOTIONAL_DAMAGE * amount / HUNDRED_PERCENT;

            if (taxAmount == 0) {
                return amount;
            }

            uint256 newAmount = taxAmount + balanceOf[address(this)];
            balanceOf[address(this)] = newAmount;

            address[] memory path = new address[](2);
            path[0] = address(this);
            path[1] = WETH;

            try router.swapExactTokensForETH(newAmount, 0, path, TREASURY, block.timestamp) {
                // SWAP was successful.
            } catch {
                // Swap can fail if amount is too low, we dont want to handle it, next tax will sell everything together.
            }

            return (amount - taxAmount);
        }
    }

    function _revertOnMaxBuyExceeded(uint256 newAmount) internal view {
        if (block.timestamp > MAX_BUY_END_TIME) {
            return;
        }

        if (newAmount > MAX_BUY) revert MaxBuyExceeded(MAX_BUY, newAmount);
    }

    function maxBuy() external view returns (uint256) {
        if (block.timestamp > MAX_BUY_END_TIME) {
            return type(uint256).max;
        }

        return MAX_BUY;
    }

    function setIsAddressExcluded(address user, bool value) external onlyOwner {
        isAddressExcluded[user] = value;

        emit IsAddressExcludedChanged(user, value);
    }

    function renounceOwnership() external {
        transferOwnership(address(0));
    }

    function inflictPain() external onlyOwner {
        if (MAX_BUY_END_TIME != 0) revert PainAlreadyInflicted();

        MAX_BUY_END_TIME = uint40(block.timestamp + MAX_BUY_DURATION);
    }

    function isPainInflicted() external view returns (bool) {
        return MAX_BUY_END_TIME != 0;
    }
}

// File: lib/solmate/src/auth/Owned.sol



// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @notice Simple single owner authorization mixin.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/auth/Owned.sol)
abstract contract Owned {
    /*//////////////////////////////////////////////////////////////
                                 EVENTS
    //////////////////////////////////////////////////////////////*/

    event OwnershipTransferred(address indexed user, address indexed newOwner);

    /*//////////////////////////////////////////////////////////////
                            OWNERSHIP STORAGE
    //////////////////////////////////////////////////////////////*/

    address public owner;

    modifier onlyOwner() virtual {
        require(msg.sender == owner, "UNAUTHORIZED");

        _;
    }

    /*//////////////////////////////////////////////////////////////
                               CONSTRUCTOR
    //////////////////////////////////////////////////////////////*/

    constructor(address _owner) {
        owner = _owner;

        emit OwnershipTransferred(address(0), _owner);
    }

    /*//////////////////////////////////////////////////////////////
                             OWNERSHIP LOGIC
    //////////////////////////////////////////////////////////////*/

    function transferOwnership(address newOwner) public virtual onlyOwner {
        owner = newOwner;

        emit OwnershipTransferred(msg.sender, newOwner);
    }
}

// File: lib/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol



pragma solidity >=0.6.2;

import './IUniswapV2Router01.sol';

interface IUniswapV2Router02 is IUniswapV2Router01 {
    function removeLiquidityETHSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountETH);
    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountETH);

    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable;
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
}

// File: lib/v2-periphery/contracts/interfaces/IUniswapV2Router01.sol



pragma solidity >=0.6.2;

interface IUniswapV2Router01 {
    function factory() external pure returns (address);
    function WETH() external pure returns (address);

    function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB, uint liquidity);
    function addLiquidityETH(
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);
    function removeLiquidity(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETH(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountToken, uint amountETH);
    function removeLiquidityWithPermit(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETHWithPermit(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountToken, uint amountETH);
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapTokensForExactTokens(
        uint amountOut,
        uint amountInMax,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);
    function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
        external
        returns (uint[] memory amounts);
    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        returns (uint[] memory amounts);
    function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);

    function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
    function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
    function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}