-2

This is the contract I have written which contains addLiquidity whose job is to simply transfer token from my wallet to smart contract.

This is the javascript code I have written so that I can approve the smart contract to accept ERC20 token.

This is the error ie appearing when I am running the application.

I just want to send ERC20 token from my wallet to smart contract. Why I want to do this is because I want to create a pool of tokens on the smart contract which I would use for some new kind of swapping which I have thought of. But for all this to happen tokens need to go from user's wallet to smart contract. PLease tell me where am I going wrong ?

1 Answers1

0

Before using transferFrom(), the user needs to approve() your contract address as the spender.

They need to do it in a separate transaction to the token2 contract, not through your contract.

In other words - the user needs to send two transactions. First one to token.approve(yourContract, amount), and the second one to yourContract.addLiquidity().

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • `IERC20(token2).approve( address(this), amount2 ); IERC20(token2).transferFrom( msg.sender, address(this), amount2 ); ` – yash mathur Jun 24 '22 at 02:50
  • I added the approve line within the addLiquidity function. So first it must approve the contract to withdraw upto amount2 from msg.sender wallet. Now when I compiled it and tried to rerun it, again gives the same error that gas estimation is not possible. – yash mathur Jun 24 '22 at 02:53
  • Can you please guide where am I going wrong now ? – yash mathur Jun 24 '22 at 02:53
  • @yashmathur The user needs to execute the `approve()` function directly on the token contract - not through your contract. So there's effectively two transactions - one to the token contract (invoking `approve()`), and other to your contract. – Petr Hejda Jun 24 '22 at 07:13
  • please check the edits on the post – yash mathur Jun 24 '22 at 07:34
  • please check the edited post. I have attached photos of what I have done. Please tell me where am I going wrong – yash mathur Jun 24 '22 at 07:37
  • @yashmathur The edit has changed the question meaning, and made it not reproducible anymore. Please post a separate question with a minimal reproducible example of your issue. – Petr Hejda Jun 24 '22 at 13:13
  • 1
    Thank You for your help it's sorted. As you said I approved from the client side. The only mistake I did was that I did not wait for the transaction to complete after I called the approve function and immediately called the liquidity function. Due to this although there was no permission yet I was trying to use transferFrom method which ultimately produced error. – yash mathur Jun 25 '22 at 02:19