-1

I am trying to build a no-code tool for users, I want to be able to transfer tokens on the user's behalf.

anytime I call the IERC20(token address).approvefunction, my metamask does not a return a screen like the attached image and the IERC20(token address).transferFrom function does not execute.

Approve function


tps://i.stack.imgur.com/NfyMC.png

dayo
  • 3
  • 2

1 Answers1

0

Calling approve() function from within your contract would just approve tokens to be spend from your contract address - not from the user.

You need to invoke MetaMask popup from your frontend app - not from the contract.

Either using their request method, requesting a transaction to the token contract, with data field of the approve() function and its arguments.

Or with more high-level approach: Creating a frontend instance of web3js, ethers.js, or whichever library you prefer, connecting to the window.ethereum local provider, and then invoking the approve() function on the token contract from here. This will also open the MetaMask popup windows asking the user to sign the transaction.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100