cannot estimate gas; transaction may fail or may require manual gas limit When trying to deposit ETH

246 views Asked by At

I am building a React webapp attempting to deposit some ETH coin to certain strategy; before user press the deposit button, browser would pop up MetaMask extension to let user connect wallet.

However, at the code side, after calling depositLiquidity() , this error occurred:

cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="execution reverted: msg.value != _inputAmount", method="estimateGas", transaction={"from":"0x1A1f6bB945914e79a01b9c243Aef7Ca99Bd7871F","to":"0xBaf1b741FEf2D06169543372ca547db6f696F1B0","data":"0xb35924a1000000000000000000000000fcb00b689fbb30848a7603312051446a7549a3ab000000000000000000000000000000000000000000000000000000000000000100000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab100000000000000000000000000000000000000000000000000000000000027100000000000000000000000000000000000000000000000000000000000002710000000000000000000000000000000000000000000000000000000000131a936","accessList":null}, error={"code":-32603,"message":"Internal JSON-RPC error.","data":{"code":3,"data":"0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000196d73672e76616c756520213d205f696e707574416d6f756e7400000000000000","message":"execution reverted: msg.value != _inputAmount"},"stack":"{\n "code": -32603,\n "message": "Internal JSON-RPC error.",\n "data": {\n "code": 3,\n "data": "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000196d73672e76616c756520213d205f696e707574416d6f756e7400000000000000",\n "message": "execution reverted: msg.value != _inputAmount"\n },\n "stack": "Error: Internal JSON-RPC error.\n

const provider = new ethers.providers.Web3Provider(window.ethereum);
                await provider.send('eth_requestAccounts', []);
                const signer = await provider.getSigner();
                const swapContract = new ethers.Contract(MinimumSwapOutAmountCalculatorAddr, minimumSwapOutAbi, provider);
                const { minimumSwapOutAmount, swapInAmount } = await swapContract.getDepositMinimumSwapOutAmount(
                    WethArbStrategyAddr, tokenAddrs[selectedTokenName].address,
                    depositValue);
                console.log("getDepositMinimumSwapOutAmount: ", minimumSwapOutAmount, swapInAmount);

                
                const farmContract = new ethers.Contract(FarmContractAddr, FarmAbi, signer);
                console.log(WethArbStrategyAddr, true,
                    tokenAddrs[selectedTokenName].address,
                    "depositValue:", depositValue,
                    "swapInAmount:", parseInt(swapInAmount._hex, 16),
                    "swapOutAmount:", parseInt(minimumSwapOutAmount._hex, 16)
                );
                const result = await farmContract.depositLiquidity(
                    WethArbStrategyAddr,
                    true,
                    tokenAddrs[selectedTokenName].address,
                    depositValue,
                    parseInt(swapInAmount._hex, 16),
                    parseInt(minimumSwapOutAmount._hex, 16)
                );
                console.log("farmContract ", result);

Here is part of the abi:

    {
    "inputs": [
        {
            "internalType": "address",
            "name": "_strategyContract",
            "type": "address"
        },
        {
            "internalType": "bool",
            "name": "_isETH",
            "type": "bool"
        },
        {
            "internalType": "address",
            "name": "_inputToken",
            "type": "address"
        },
        {
            "internalType": "uint256",
            "name": "_inputAmount",
            "type": "uint256"
        },
        {
            "internalType": "uint256",
            "name": "_swapInAmount",
            "type": "uint256"
        },
        {
            "internalType": "uint256",
            "name": "_minimumSwapOutAmount",
            "type": "uint256"
        }
    ],
    "name": "depositLiquidity",
    "outputs": [

    ],
    "stateMutability": "payable",
    "type": "function"
}

I am sure that all of the parameters passed into depositLiquidity() is correct, because I have examined it with etherscan website.

Might someone tell me where I wrote wrongly?

1

There are 1 answers

0
Cindy On

It turns out that I lack the passing of msg.value.

As a result, this modification of inserting another parameter works:

const result = await farmContract.depositLiquidity(
                    WethArbStrategyAddr,
                    true,
                    tokenAddrs[selectedTokenName].address,
                    depositValue,
                    parseInt(swapInAmount._hex, 16),
                    parseInt(minimumSwapOutAmount._hex, 16),
                    {
                        value: depositValue
                    }
                );

Note: The unit of the depositValue variable is wei.