1

I'm a beginner and for the past few days I have tried to recreate this tutorial: https://www.youtube.com/watch?v=a5at-FEyITQ

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(
  addresses.router,
  [
    '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)'

  ],
  account
);

async function LOL(){
  const ethAmount = ethers.utils.parseEther("0.1");
  //const tx = await router.swapExactTokensForTokens(
    const tx = await router.swapExactETHForTokens(
    ethAmount,
    0,
    ['0xc778417e063141139fce010982780140aa0cd5ab', '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984'],
    addresses.recipient, 
    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');
  console.log(receipt);
}
LOL();

The result was a FAILURE as seen below:

https://ropsten.etherscan.io/tx/0xa18d2df70f04366a7ed74144d91669c37b3be02a0a57ad3a8e81c2da82806ef9

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

6
  • 1
    Just wanted to add to my answer that you should be careful when you deploy such a bot on mainnet (bsc, ethereum). Someone could easily exploit it by creating many pancakeswap pairs with bogus tokens so that your bot buys them instantly. The person would them withdraw his liquidity from the pairs and make a profit on your back.
    – Undead8
    Apr 7, 2021 at 18:00
  • I'm aware of this and will be careful. Thank you for your warning, it is much appreciated.
    – Marjetica
    Apr 8, 2021 at 15:31
  • Did you get this to work? can you share your working code? Apr 20, 2021 at 13:52
  • Bro, why the pancakeswap version of this is not working?? I tried several possible ways still no solution found.
    – rANDOM GUy
    May 17, 2021 at 22:10
  • I facing the same issue, this you resolved this one Thank
    – DMC
    May 20, 2021 at 4:25

1 Answer 1

2

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.

4
  • Ty for the reply! Sry for not mentioning this above, but I did approve it on Etherscan. When that didn't work I tried approving all sorts of stuff lol. Can you confirm that this is the right way of approving without using code ofcourse: I went on address of WETH on Ropsten Etherscan (/address/0xc778417e063141139fce010982780140aa0cd5ab#writeContract). Connected to Web3 with same address that I use as recipient in code. As guy(address) I inputed the Router Addres (0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D) and as wad(uint256) 10(and thirty more 0s). Still my transaction Fails...
    – Marjetica
    Apr 8, 2021 at 15:28
  • Yes that's how you do it, and I see that the allowance shows the right number. However, your address has no WETH at all. You must have the WETH you are trying to swap in your account first. FYI you can exchange ETH for WETH by calling the deposit() function on the WETH contract.
    – Undead8
    Apr 8, 2021 at 17:29
  • THANK YOU! My problem was my stupidity hehe, not thinking that I need to swap ETH to WETH, before executing swapExactTokensForTokens...
    – Marjetica
    Apr 10, 2021 at 8:54
  • @Undead8, thanks for sharing your inputs here. Sorry for reaching you out of the blue. I was wondering if you could assist me with this. I am kind of not finding any information anywhere else. ethereum.stackexchange.com/questions/118130/… Jan 6, 2022 at 21:43

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