I'm trying to swap tokens using Uniswap v2 interface. The code of the contract is here.

I'm able to deposit Eth to the contract and but I'm not able to invoke the swapEthForTokenWithUniswap() successfully through javascript.

const swap = async() => {
    const res = await defitry.methods.swapEthForTokenWithUniswap(web3.utils.toWei('0.1', 'ether'))
               gas: 100000})

The transaction keeps reverting but i can call the function and swap using the writeContract tab on etherscan. I keep getting Object.TransactionRevertedWithoutReasonError


I assume it's due to the gas limit you pass along. If you look at a successful transaction, it actually consumed 222,750 gas, so you need to adjust your limit or use the estimateGas function.

One other thing that I noticed, be aware that using now inside your contract to use as deadline is a security risk. The deadline timestamp should be passed from the frontend, because now will always just be the time when a miner includes the transaction. Also see my blog post for further details.

Your Answer

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

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