Approve and transferfrom in moralis

I want to be able to approve and the transferfrom on all erc20 tokens with moralis. i.e when a user that has usdt clicks on the approve function it will give my dapp the permission to transfer the token out of eth address. same with other erc20 token like usdc and busd.

You can call the smart contract function to approve and transfer using executeFunction

https://docs.moralis.io/moralis-dapp/web3/web3#example-of-calling-a-write-contract-method

Thank you very much for your response… but does it automatically approve any erc20 tokens?

No. User will have to approve it from the metamask with gas fee.

what i mean is… if the user has usdt will it give my contract permission to transfer that token calling the transferfrom method… same with any other erc20 tokens like usdc or busd.

Yes, You can do it using executeFunction and you will have to pass the required option parameter to the executeFunction while calling it.
Check the docs for more details.

God bless you bro… I appreciate

I am sorry for disturbing you. but pls can you show me an example.

The documentation does not cover much

Moralis.executeFunction(approve)?

This is an example with approve and transferFrom function calls

const options = {
  contractAddress: "Erc20ContractAddress",
  abi: [{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]   
};

const approve = await Moralis.executeFunction({
  functionName: "approve",
    params: {
    spender: "Address",
    amount: "Amount"
  },
  ...options,
});
let approveTx = await approve.wait();

const transfer = await Moralis.executeFunction({
  functionName: "transferFrom",
    params: {
    from: "FromAddress",
    to: "ToAddress",
    amount: "TokenAmount"
  },
  ...options,
});
let transferTx = await transfer.wait();

Hello @mikedominion I am also facing this problem I am trying to wrap my head around it, were you able to finalize your work?

Hi @emma

Please note that moralis SDK no longer has the .executeFunction or anything related to calling smart contract functions. So using other sdk’s like wagmi is recommended for calling smart contract functions.

In case the previous answer to this post does not help, please create a new post for further questions.:smiley: