interface icon indicating copy to clipboard operation
interface copied to clipboard

code=UNPREDICTABLE_GAS_LIMIT

Open aress31 opened this issue 5 years ago • 6 comments

I am in the process of implementing an ERC20 token with a basic add to liquidity feature, this is the relevant code snippet:

    // Track: https://forum.openzeppelin.com/t/updating-balance-on-inherited-contracts/8133
    function _beforeTokenTransfer(address sender, uint256 amount)
        internal
        returns (uint256)
    {
        uint256 updatedAmount = amount;

        if (_liquidityTax != 0) {
            uint256 liquidityFee = (amount * _liquidityTax) / (10**2);

            _balances[sender] -= liquidityFee;
            _balances[address(this)] += liquidityFee;

            bool overMinTokensRequiredBeforeSwap =
                _balances[address(this)] >= _minTokensRequiredBeforeSwap;

            if (
                overMinTokensRequiredBeforeSwap &&
                !_inSwapAndLiquify &&
                _isAutoSwapAndLiquify
            ) {
                uint256 halfContractBalance = _balances[address(this)] / 2;
                uint256 ethReceived = _swap(halfContractBalance);
                (uint256 amountETH, uint256 amountToken) =
                    _liquify(halfContractBalance, ethReceived);

                _totalLiquidityETH += amountETH;
                _totalLiquidity += amountToken;
            }

            updatedAmount -= liquidityFee;
        }

        return updatedAmount;
    }

    receive() external payable {}

    function _swap(uint256 tokensToSwap) private returns (uint256) {
        // contract's current BNB/ETH balance
        uint256 initialBalance = address(this).balance;

        // swap half of the tokens to ETH
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = _uniswapV2Router.WETH();

        _approve(address(this), address(_uniswapV2Router), tokensToSwap);

        // Swap tokens for ETH/BNB
        _uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokensToSwap,
            0,
            path,
            address(this), // this contract will receive the ETH that were swapped from the token
            block.timestamp
        );

        // Figure out the exact amount of tokens received from swapping
        // Check: https://github.com/Uniswap/uniswap-v2-periphery/issues/92
        uint256 ethReceived = address(this).balance - initialBalance;

        emit SwapExactTokensForETHSupportingFeeOnTransferTokens(
            tokensToSwap,
            ethReceived
        );

        return ethReceived;
    }

    function _liquify(uint256 tokensToAddToLiq, uint256 ethReceived)
        private
        returns (uint256, uint256)
    {
        // To cover all possible scenarios, msg.sender should have already given
        // the router an allowance of at least amountTokenDesired on token.
        _approve(address(this), address(_uniswapV2Router), ethReceived);

        (uint256 amountToken, uint256 amountETH, ) =
            _uniswapV2Router.addLiquidityETH{value: ethReceived}(
                address(this),
                tokensToAddToLiq, //amountTokenDesired
                0, // amountTokenMin
                0, // amountETHMin
                owner(),
                block.timestamp
            );

        emit AddLiquidityETH(amountToken, amountETH);

        return (amountETH, amountToken);
    }

When deploying this token to ropsten and after having manually added liquidity to the pool, I am unable to buy the token.

See:

image

Swap failed: cannot estimate gas; transaction may fail or may require manual gas limit (error={"code":-32603,"message":"execution reverted: UniswapV2: TRANSFER_FAILED","data":{"originalError":{"code":3,"data":"0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a556e697377617056323a205452414e534645525f4641494c4544000000000000","message":"execution reverted: UniswapV2: TRANSFER_FAILED"}}}, method="estimateGas", transaction={"from":"0x8c5ABc25C0cfD9D62E469340fD7d812a742090AC","to":"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D","value":{"type":"BigNumber","hex":"0x016cde"},"data":"0xfb3bdb410000000000000000000000000000000000000000000000056bc75e2d6310000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000008c5abc25c0cfd9d62e469340fd7d812a742090ac00000000000000000000000000000000000000000000000000000000609b07930000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c778417e063141139fce010982780140aa0cd5ab00000000000000000000000050c39e87a915910520e8cc2cf594ae2cc547b5d6","accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.1.1)

The contract address is: https://ropsten.etherscan.io/address/0x50c39e87a915910520e8cc2cf594ae2cc547b5d6

Could anyone please assist with this?

aress31 avatar May 11 '21 22:05 aress31

I have this bug on the production dapp trying to remove liquidity

CRYPTO-GENIK avatar May 12 '21 08:05 CRYPTO-GENIK

I have this bug on the production dapp trying to remove liquidity

Please let me know if/when you manage to solve it.

aress31 avatar May 12 '21 12:05 aress31

This looks like a duplicate of https://github.com/Uniswap/uniswap-interface/issues/1526

siromivel avatar May 12 '21 13:05 siromivel

Not the same error code.

aress31 avatar May 12 '21 13:05 aress31

Transfer failed means the token you're buying (TT) is unable to be transferred from the pool to the recipient (the account doing the swap), which is almost definitely an issue with the token. The error has been made more readable.

moodysalem avatar May 13 '21 22:05 moodysalem

@moodysalem that's the thing, the transfer function works just fine and I used - as far as I know - the swap function properly so not sure why it is failing and displaying this error message...

aress31 avatar May 13 '21 22:05 aress31