0

very new to solidity development. I'm trying to buy ERC20 tokens from a smart contract but all my transactions are reverted.

I'm doing these tests on ropsten network.

Here is a part of the contract :

import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";

contract Contract {
    address internal constant UNISWAP_ROUTER_ADDRESS =
        0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;

    IUniswapV2Router02 public uniswapRouter;
    constructor() {
        uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
    }

    function swapEthForTokenWithUniswap(uint256 ethAmount, address tokenAddress)
        public
    {
        // Verify we have enough funds
        require(
            ethAmount <= address(this).balance,
            "Not enough Eth in contract to perform swap."
        );

        // Build arguments for uniswap router call
        address[] memory path = new address[](2);
        path[0] = uniswapRouter.WETH();
        path[1] = tokenAddress;

        // Make the call and give it 15 seconds
        // Set amountOutMin to 0 but no success with larger amounts either
        uniswapRouter.swapExactETHForTokens{value: ethAmount}(
            0,
            path,
            address(this),
            block.timestamp + 15
        );
    }

    // important to receive ETH
    receive() external payable {}
}

I'm able to send the eths to my contract but not buying a token. I'm running this piece of code :

const createTransaction2 = await web3.eth.accounts.signTransaction({
    from: MAIN_WALLET,
    to: CONTRACT_ADDY,
    gas: 210000,
    data: web3.eth.abi.encodeFunctionSignature('swapEthForTokenWithUniswap(0.001, 0xaD6D458402F60fD3Bd25163575031ACDce07538D)'),
    gasPrice: web3.utils.toHex(web3.utils.toWei('220','gwei')),
    chainId: 3
    
}, KEY);

const receipt2 = await web3.eth.sendSignedTransaction(createTransaction2.rawTransaction);

Here is the hash in case it could help https://ropsten.etherscan.io/tx/0x86d166bb2cda4aa4ebe7ab6ceb1ce0fabfb2ad0005bf2d915d7faa3d931312b9

Thanks for your help !

1
  • Without the exact code we can only guess. Gas used is just 21,232 it means the call fails very early probably before making any call to uniswap router.
    – Ismael
    Commented Apr 8, 2021 at 19:52

1 Answer 1

0

I found the issue, I needed to pass an amount in wei since 0.0001 is not a proper uint256 value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.