0

Im currently working on a project which can create a uniswap liquidity pool on the fallback function however the transaction is not even broadcasted to the blockchain when trying to send some ETH via metamask to the contract. Currently using solidity 0.6.12. Below is the code fragment

    function _mint(address account, uint256 amount) internal virtual {
    require(account != address(0), "ERC20: mint to the zero address");

    _totalSupply = _totalSupply.add(amount);
    _balances[account] = _balances[account].add(amount);
    emit Transfer(address(0), account, amount);
}

function mint(address account, uint256 amount) public onlyStaker{
    _mint(account, amount);
}

bool createUniswapAlreadyCalled = false;

function createUniswap() public payable{
    require(!createUniswapAlreadyCalled);
    createUniswapAlreadyCalled = true;
    
    require(address(this).balance > 0);
    uint toMint = address(this).balance;
    _mint(address(this), toMint);
    
    address UNIROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
    _allowances[address(this)][UNIROUTER] = toMint;
    Uniswap(UNIROUTER).addLiquidityETH{ value: address(this).balance }(address(this), toMint, 1, 1, address(this), 33136721748);
}

receive() external payable {
    createUniswap();
}

And here is the uniswap interface for the addLiquidityETH function

interface Uniswap{
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts);
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts);
function addLiquidityETH(address token, uint amountTokenDesired, uint amountTokenMin, uint amountETHMin, address to, uint deadline) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function getPair(address tokenA, address tokenB) external view returns (address pair);
function WETH() external pure returns (address);

}

4
  • Does this answer your question? What's a fallback function when using address.send(...)? Sep 17 at 7:38
  • Im using the receive() function that is used as a fallback function to receive ether. The receive() external payable function should trigger on sending ETH via metamask
    – Felix
    Sep 17 at 8:25
  • That's not always true, it depends on a host of factors. Refer to the docs and check out my answer here. Sep 17 at 9:02
  • How are you testing? How do you send ether to the contract's receive function?
    – Ismael
    Sep 19 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.