AutoBuybackNonReverting

Description:

Smart contract deployed on Ethereum.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

interface IUniswapV2Router {
    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable;
    function WETH() external pure returns (address);
    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
}

interface IERC20 {
    function balanceOf(address account) external view returns (uint256);
}

contract AutoBuybackNonReverting {
    address public constant TOKEN_ADDRESS = 0x90ba94F4e64E327c444d7Ac7f1056Ead4Ea6FD98;
    address public constant DEAD_ADDRESS = 0x000000000000000000000000000000000000dEaD;
    
    IUniswapV2Router public uniswapRouter;
    
    uint256 public totalETHUsed;
    uint256 public totalTokensBurned;
    uint256 public totalTransactions;
    uint256 public minBuyAmount = 0.001 ether;
    uint256 public gasReserve = 0.003 ether;
    uint256 public slippageTolerance = 2000; // 20% slippage for 5/5 tax token

    address public owner;
    bool public emergencyStop = false;

    event BuybackExecuted(uint256 ethAmount, uint256 tokensBurned);
    event GasReserveUpdated(uint256 newReserve);
    event MinBuyAmountUpdated(uint256 newMinAmount);
    event SlippageUpdated(uint256 newSlippage);
    event EmergencyWithdrawal(address indexed recipient, uint256 amount);
    event EmergencyStopToggled(bool stopped);
    event ETHReceived(address indexed sender, uint256 amount);

    modifier onlyOwner() {
        require(msg.sender == owner, "Only owner");
        _;
    }

    modifier whenNotEmergency() {
        require(!emergencyStop, "Contract in emergency mode");
        _;
    }

    constructor(address _uniswapRouter) {
        uniswapRouter = IUniswapV2Router(_uniswapRouter);
        owner = msg.sender;
    }

    // ???? RECEIVE ETH - NEVER REVERTS, ALWAYS ACCEPTS ETH
    receive() external payable {
        emit ETHReceived(msg.sender, msg.value);
        
        // Skip buyback if in emergency mode
        if (emergencyStop) {
            return;
        }
        
        // Calculate available ETH for buyback (never goes below gas reserve)
        uint256 totalBalance = address(this).balance;
        
        // If total balance is less than or equal to gas reserve, no buyback
        if (totalBalance <= gasReserve) {
            return;
        }
        
        uint256 availableForBuyback = totalBalance - gasReserve;
        
        // Only execute buyback if above minimum amount
        if (availableForBuyback >= minBuyAmount) {
            _buyAndBurn(availableForBuyback);
        }
    }

    // ???? MANUAL TRIGGER - USE ALL AVAILABLE ETH (above gas reserve)
    function executeBuyback() external whenNotEmergency {
        uint256 totalBalance = address(this).balance;
        
        // Ensure we have enough ETH above gas reserve
        if (totalBalance <= gasReserve) {
            revert("Not enough ETH after gas reserve");
        }
        
        uint256 availableETH = totalBalance - gasReserve;
        require(availableETH >= minBuyAmount, "Not enough ETH for minimum buy");
        
        _buyAndBurn(availableETH);
    }

    // ???? MANUAL TRIGGER - USE SPECIFIC AMOUNT
    function executeBuyback(uint256 amount) external whenNotEmergency {
        uint256 totalBalance = address(this).balance;
        
        // Ensure we have enough ETH above gas reserve
        if (totalBalance <= gasReserve) {
            revert("Not enough ETH after gas reserve");
        }
        
        uint256 availableETH = totalBalance - gasReserve;
        require(amount <= availableETH, "Amount exceeds available ETH");
        require(amount >= minBuyAmount, "Amount below minimum");
        
        _buyAndBurn(amount);
    }

    // ???? BUYBACK FUNCTION WITH PROPER SLIPPAGE FOR 5/5 TAX
    function _buyAndBurn(uint256 ethAmount) internal {
        uint256 startBalance = IERC20(TOKEN_ADDRESS).balanceOf(DEAD_ADDRESS);
        
        address[] memory path = new address[](2);
        path[0] = uniswapRouter.WETH();
        path[1] = TOKEN_ADDRESS;

        // Calculate minimum tokens with 5% buy tax + slippage
        uint256 minTokensOut = _calculateMinTokensOut(ethAmount);

        uniswapRouter.swapExactETHForTokensSupportingFeeOnTransferTokens{value: ethAmount}(
            minTokensOut,
            path,
            DEAD_ADDRESS,
            block.timestamp + 300 // 5 minute deadline
        );

        uint256 endBalance = IERC20(TOKEN_ADDRESS).balanceOf(DEAD_ADDRESS);
        uint256 burned = endBalance - startBalance;

        totalETHUsed += ethAmount;
        totalTokensBurned += burned;
        totalTransactions++;

        emit BuybackExecuted(ethAmount, burned);
    }

    // ???? CALCULATE MIN TOKENS WITH 5/5 TAX CONSIDERATION
    function _calculateMinTokensOut(uint256 ethAmount) internal view returns (uint256) {
        address[] memory path = new address[](2);
        path[0] = uniswapRouter.WETH();
        path[1] = TOKEN_ADDRESS;

        try uniswapRouter.getAmountsOut(ethAmount, path) returns (uint[] memory amounts) {
            uint256 expectedTokens = amounts[1];
            
            // For 5/5 tax token: 
            // - 5% buy tax on purchase
            // - Additional slippage tolerance
            uint256 afterBuyTax = (expectedTokens * 95) / 100; // 5% buy tax
            uint256 withSlippage = (afterBuyTax * (10000 - slippageTolerance)) / 10000;
            
            return withSlippage;
        } catch {
            return 0; // If estimation fails, accept any amount
        }
    }

    // ???? ESTIMATE TOKENS RECEIVED AFTER 5% TAX
    function estimateTokensAfterTax(uint256 ethAmount) external view returns (uint256) {
        address[] memory path = new address[](2);
        path[0] = uniswapRouter.WETH();
        path[1] = TOKEN_ADDRESS;

        try uniswapRouter.getAmountsOut(ethAmount, path) returns (uint[] memory amounts) {
            uint256 expectedTokens = amounts[1];
            return (expectedTokens * 95) / 100; // 5% buy tax
        } catch {
            return 0;
        }
    }

    // ???? EMERGENCY FUNCTIONS
    function toggleEmergencyStop(bool _stop) external onlyOwner {
        emergencyStop = _stop;
        emit EmergencyStopToggled(_stop);
    }

    function emergencyWithdrawAll() external onlyOwner {
        uint256 totalBalance = address(this).balance;
        require(totalBalance > 0, "No ETH to withdraw");
        
        gasReserve = 0;
        payable(owner).transfer(totalBalance);
        emit EmergencyWithdrawal(owner, totalBalance);
    }

    function emergencyWithdraw(uint256 amount) external onlyOwner {
        require(amount <= address(this).balance, "Insufficient balance");
        
        if (amount > gasReserve) {
            gasReserve = 0;
        } else {
            gasReserve -= amount;
        }
        
        payable(owner).transfer(amount);
        emit EmergencyWithdrawal(owner, amount);
    }

    // ⚙️ OWNER CONFIGURATION
    function setMinBuyAmount(uint256 _minAmount) external onlyOwner {
        minBuyAmount = _minAmount;
        emit MinBuyAmountUpdated(_minAmount);
    }

    function setGasReserve(uint256 _gasReserve) external onlyOwner {
        gasReserve = _gasReserve;
        emit GasReserveUpdated(_gasReserve);
    }

    function setSlippageTolerance(uint256 _slippage) external onlyOwner {
        require(_slippage <= 3000, "Slippage too high"); // Max 30%
        slippageTolerance = _slippage;
        emit SlippageUpdated(_slippage);
    }

    function addGasReserve() external payable onlyOwner {
        gasReserve += msg.value;
    }

    // ???? VIEW FUNCTIONS
    function getStats() external view returns (
        uint256 ethUsed, 
        uint256 tokensBurned, 
        uint256 txCount, 
        uint256 totalBalance,
        uint256 availableForBuyback,
        uint256 currentGasReserve,
        bool isEmergency,
        uint256 currentSlippage
    ) {
        uint256 available = address(this).balance > gasReserve ? address(this).balance - gasReserve : 0;
        return (totalETHUsed, totalTokensBurned, totalTransactions, address(this).balance, available, gasReserve, emergencyStop, slippageTolerance);
    }

    function getAvailableForBuyback() external view returns (uint256) {
        uint256 totalBalance = address(this).balance;
        if (totalBalance <= gasReserve) {
            return 0;
        }
        return totalBalance - gasReserve;
    }

    function canExecuteBuyback() external view returns (bool) {
        uint256 totalBalance = address(this).balance;
        if (totalBalance <= gasReserve) {
            return false;
        }
        uint256 available = totalBalance - gasReserve;
        return available >= minBuyAmount;
    }
}

Tags:
addr:0x01601b88e3ff31081048da705bd2dabd3c5f1bb7|verified:true|block:23391897|tx:0xdb7129e976b841b306a7489f785dd2ca3ac2182e33b792ae0767b5464eb4a226|first_check:1758273188

Submitted on: 2025-09-19 11:13:09

Comments

Log in to comment.

No comments yet.