'Large amount of wei not being processed by Solidity on Remix

U am trying to implement liquidity pools with Solidity, and had written two functions : addLiquidity() and withdraw() for it. However, the withdraw function doesn't seem to work with Remix when I try to withdraw large sums (like 0.001 ether), but works with sums like 150000 wei or something.

It doesn't seem to be an issue with Remix's IDE (i read somehere it has a problem working with large numbers), because even when I pass the 149999998499999985165 wei in double quotes (e.g. "149999998499999985165") the same error appears.

The error states: "Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution reverted { "originalError": { "code": 3, "data": "0x4e487b710000000000000000000000000000000000000000000000000000000000000011", "message": "execution reverted" } }"

Code:

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

interface linkStandardToken {
    function transferFrom(address _from, address _to, uint256 _value) external returns (bool) ;
    function balanceOf(address _owner) external  returns (uint256) ;
    function transfer(address to, uint tokens) external returns (bool success);
}



contract Uniswap 
{
    using SafeMath for uint256;

    uint public totalLiquidity;
    uint public balance;
    address public owner;
    address public tokenAddress = 0xaFF4481D10270F50f203E0763e2597776068CBc5; // REPLACE WITH ACTUAL TOKEN
    linkStandardToken token;
    bool public poolInit = false;
    uint public protocolFees = 30; //in basis points i.e. divide by 10,000
    uint public tempTokenPrice = 0;
    mapping(address => uint) public liquidityBalances;

    constructor() 
    {
        owner = msg.sender;
        token = linkStandardToken(tokenAddress);
    }


    function init(uint _tokenAmount) public payable
    {
        require(totalLiquidity == 0, "Already initialized");
        require(_tokenAmount > 0, "Token amount must be > 0");
        require(msg.value > 0, "Eth amount must be > 0");
        totalLiquidity = totalLiquidity.add(_tokenAmount);
        balance = balance.add(msg.value);
        poolInit = true;
        require(token.transferFrom(msg.sender, address(this), _tokenAmount), "Can't transfer tokens to contract");
        setTokenToEthPrice();
    }

    fallback() payable external{}
    receive() payable external{}

    // _amount - input token amount, X - input token reserve, Y- output token reserve
    function _swap(uint _amount, uint X , uint Y) public view returns (uint)
    {
        // code omitted
    }


    function swapEthToToken(/*uint _inputEthAmount*/) public payable 
    {
        // code omitted
    }

    function swapTokenToEth(uint _tokenAmount) public payable 
    {
                // code omitted
    }

    function setTokenToEthPrice() public // set to internal later 
    {
       tempTokenPrice =  _swap(1, balance , token.balanceOf(address(this))) ;
    }



    function addLiquidity(uint maxTokens) payable public  returns (uint)
    {
        require(msg.value > 0, "msg.val <= 0");
        require(totalLiquidity > 0, "totalLiquidity <= 0");
        uint tokensBalance = getTokenBalance(address(this));
        uint tokensToAdd = msg.value.mul(tokensBalance)/balance;
        require(tokensToAdd <= maxTokens , "tokensToAdd > maxTokens");
        balance= balance.add(msg.value);

        uint mintedLiquidity = msg.value.mul(totalLiquidity)/balance;
        liquidityBalances[msg.sender] = liquidityBalances[msg.sender].add(mintedLiquidity);
        totalLiquidity = totalLiquidity.add(mintedLiquidity);

        require(linkStandardToken(
            0xaFF4481D10270F50f203E0763e2597776068CBc5)
            .transferFrom(msg.sender, address(this), tokensToAdd));

        return mintedLiquidity;
    }


    function withdraw9(uint256 amount, uint minimumEth, uint minimumTokens) public
    {
        require(liquidityBalances[msg.sender] >= amount, "Liquidity Balance of msg send < amount");
        require(totalLiquidity > 0, "totalLiquidity <= 0");

        uint tokenBalance = getTokenBalance(address(this));
        uint temp = amount.mul(totalLiquidity);
        uint etherToTransfer = temp.div(balance);
        uint temp1 = amount.mul(totalLiquidity);
        uint tokensToTransfer = temp1.div(tokenBalance);

        require(minimumEth < etherToTransfer, "minimumEth >= etherToTransfer");
        require(minimumTokens < tokensToTransfer, "minimumTokens >= tokensToTransfer");

        balance = balance - etherToTransfer;
        totalLiquidity = totalLiquidity.sub(amount);
        liquidityBalances[msg.sender] = liquidityBalances[msg.sender].sub(amount);

        address payable  addr = payable(msg.sender);
        addr.transfer(etherToTransfer);
        require(linkStandardToken(
            0xaFF4481D10270F50f203E0763e2597776068CBc5)
            .transfer(msg.sender, tokensToTransfer), "Token transfer unsuccesful");
    }


}



library SafeMath {
    ....// code emitted for compactness
}


Solution 1:[1]

As i can see in the last line of widthdraw9 function you use .transfer in order to send ether to some contract. I guess this contract have some code in receive function, so .transfer is not your choose. This function has a gas limitation and any code in receive can break it. If i correctly saw the problem then you should use .call function with enough amount of gas. More about these functions.

Solution 2:[2]

The "Gas estimation errored" message doesn't necessarily mean that the problem is with the amount of gas given, just that it hit some error while estimating the amount of gas needed.

The originalError.data has "0x4e487b71...". Looking up those high order 4 bytes on 4byte.directory shows that it's the signature for Panic(uint256), and the code in the low order bytes is "...00011" so the error code is 0x11 (17 decimal). The Solidity doc lists these codes, indicating:

0x11: If an arithmetic operation results in underflow or overflow outside of an unchecked { ... } block.

In the code, balance is not declared in uint etherToTransfer = temp.div(balance). If it's a state variable, could it be 0? Or is there a missing uint256 balance = liquidityBalances[msg.sender]?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ?????? ????????
Solution 2