[Ethereum] Uniswap/Pancakeswap SWAP swapExactTokensForTokens / swapExactETHForTokens FAILED (Sniping Bot)


I'm a beginner and for the past few days I have tried to recreate this tutorial:

At first I tried to implement it on PancakeSwap, but got a Transaction Error, so after not finding a solution I returned to Uniswap.

I went on Uniswap (https://app.uniswap.org/#/swap), connected with my Metamask Account running on Ropsten test network and swapped some ETH for UNI… It worked. I looked At the Transaction and used WETH an UNI addresses bellow…

As before on Bscscan Testnet, now on Ropsten Etherscan I tried to execute function 14. swapExactTokensForTokens on UniswapV2Router (https://ropsten.etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d#writeContract) as shown in Image 1. As soon as I clicked write I saw on Metamask: "Transaction Error. Exception thrown in contract code.", Gas price (GWEI): 185 and amount + gass fee was only equal to gas fee. And result was as shown on Image 1, with Transaction Status: Status:
Fail with error 'TransferHelper: TRANSFER_FROM_FAILED' (https://ropsten.etherscan.io/tx/0x5a1caf70fa0ae3e7a74bd80f6fad1faeb5b0185426b27eb012176e15deb5931d).

Image 1:
Image 1

I used the same input fields on function 10. swapExactETHForTokens and it SUCCEEDED!

So I decided to use this function in code as shown below:

const ethers = require('ethers');
const addresses = {
  WETH: '0xc778417e063141139fce010982780140aa0cd5ab',
  factory: '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f', 
  router: '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
  recipient: '0x483A7239dB71fc99a630E6C71dB2508A4dE64508'
const mnemonic = 'MY_MNEMONIC_KEY';
const provider = new ethers.providers.WebSocketProvider('wss://ropsten.infura.io/ws/v3/INFRA_NUMBER');
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
const account = wallet.connect(provider);

const router = new ethers.Contract(
    'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
    'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
    'function swapExactETHForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'


async function LOL(){
  const ethAmount = ethers.utils.parseEther("0.1");
  //const tx = await router.swapExactTokensForTokens(
    const tx = await router.swapExactETHForTokens(
    ['0xc778417e063141139fce010982780140aa0cd5ab', '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984'],
    Date.now() + 1000 * 60 * 10, //10 minutes,
        'gasLimit': 300000,
        'gasPrice': ethers.utils.parseUnits('185', 'gwei'),
  console.log("https://ropsten.etherscan.io/tx/" + tx.hash);
  const receipt = await tx.wait(); 
  console.log('Transaction receipt');

The result was a FAILURE as seen below:


Any help/input would be much appreciated, as I have been stuck on this for a while now…

Best Answer

Before swaping a token (such as WETH), you have to approve a sufficient amount to the router address.

Use the approve() function of the token contract before attempting the swap.

Related Topic