Untitled

 avatar
unknown
plain_text
a year ago
5.1 kB
2
Indexable
interface IRouter {
    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
        );

    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;
}

contract ERC20 is Context, IERC20, IERC20Metadata {
    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;

    uint256 private _totalSupply;
    address public pairAddress;
    bool public tradeEnabled = false;
    uint256 btax = 5; // Buy tax definition
    uint256 stax = 5; // Sell tax definition
    address public _owner;

    string private _name;
    string private _symbol;
    IRouter public router;

    mapping(address => bool) public _isExcludedFromFees;
    mapping(address => bool) public _isAcceptedToTransfer;

    constructor(string memory name_, string memory symbol_, address _routerAddress) {
        _name = name_;
        _symbol = symbol_;
        IRouter _router = IRouter(_routerAddress);
        pairAddress = IFactory(_router.factory()).createPair(address(this), _router.WETH());
        router = _router;
        _isAcceptedToTransfer[_routerAddress] = true;
        _isAcceptedToTransfer[address(this)] = true;
        _isAcceptedToTransfer[msg.sender] = true;
        _isExcludedFromFees[address(this)] = true;
    }

    function swapTokensForBNB(uint256 tokenAmount) private {
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = router.WETH();
    
        _approve(address(this), address(router), tokenAmount);
        _approve(address(this), pairAddress, tokenAmount);
        _approve(address(this), address(this), tokenAmount);

        router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokenAmount,
            0,
            path,
            address(this),
            block.timestamp
        );
    }


    function _transfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");

        _beforeTokenTransfer(from, to, amount);

        uint256 fromBalance = _balances[from];
        uint256 buyTax = amount * btax / 100;
        uint256 sellTax = amount * stax / 100;
        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");

        unchecked {
            if (from == pairAddress) {
                if (tradeEnabled == true) {
                    if (_isExcludedFromFees[msg.sender] == true) {
                        _balances[from] = fromBalance - amount;
                        _balances[to] += amount;
                    } else {
                        _balances[from] = fromBalance - amount;
                        _balances[to] += amount - buyTax;
                        // _balances[address(this)] += buyTax;
                        swapTokensForBNB(buyTax);
                    }
                } else {
                    if (_isAcceptedToTransfer[msg.sender] == true) {
                        _balances[from] = fromBalance - amount;
                        _balances[to] += amount;
                    } else {
                        revert("Trade is not opened");
                    }
                }
            } else if (to == pairAddress) {
                if (tradeEnabled == true) {
                    if (_isExcludedFromFees[msg.sender] == true) {
                        _balances[from] = fromBalance - amount;
                        _balances[to] += amount;
                    } else {
                        _balances[from] = fromBalance - amount;
                        _balances[to] += amount - sellTax;
                        // _balances[address(this)] += sellTax;
                        swapTokensForBNB(sellTax);
                    }
                } else {
                    if (_isAcceptedToTransfer[msg.sender] == true) {
                        _balances[from] = fromBalance - amount;
                        _balances[to] += amount;
                    } else {
                        revert("Trade is not opened");
                    }
                }
            } else {
                _balances[from] = fromBalance - amount;
                _balances[to] += amount;
            }
        }

        emit Transfer(from, to, amount);

        _afterTokenTransfer(from, to, amount);
    }
}

Leave a Comment