0

I have a big doubt. I'm building an application where the user can create their NFT, however, the payment that will be used to create the NFT will be my token that ERC20.

My question is how do I use the ERC20 token as a charge when the user creates the NFT? Remembering that the charge is made to the account of the person who is creating the NFT.

For a basic example my contract ERC721 there is the following method...

contract MyNFT is ERC721 {
.....
function createNft() public{
//need here add the method payment using my token erc20
}
.....
}
1

1 Answer 1

0

You need to import the IERC20 interface, then define the token used for the transaction as a state variable.

IERC20 token = IERC20(0x123...)

and inside your function, you need to call token.transferFrom(msg.sender, address(this), amount) function, this function will take token from user's balance and transfer it to the contract, beforehand it requires that the user called approve function on your ERC20 Token, to increase the allowance of your contract on its token.

That's the reason why you need to approve every time you're interacting with a smart contract (e.g, Uniswap, Pancakeswap, Aave, etc.)

So to recap:

  1. You must deploy your Token (ERC20)
  2. You must load your ERC20 using IERC20 interface on your NFT contract (wheres the mint function).
  3. You must deploy your NFT.
  4. User calls approve function of your ERC20 token (first contract deployed), with your contract address as spender (NFT contract so it can use user's balance with transferFrom)
  5. User calls your mint function (NFT contract), it will take tokens from its balance and send them to the smart contract.
2
  • Hi @adam, thanks for your answer. Let me get this straight, on my website (frontend) I call the web3 function passing the approve method, correct? And then after I call the approve method I can call the function of my ERC contract to create NFT, however a question, when I use IERC20 the address I would use to instantiate the interface is the ERC721 contract or will it be the address from the user's wallet?
    – Crazy
    Mar 27, 2022 at 13:29
  • Yes from your frontend, you need to call approve from your ERC20 Token (already deployed ofc). when you use IERC20 it should be the address of the ERC20 Token deployed beforehand. I'm editing my answer for more clarity
    – Adam Boudj
    Mar 27, 2022 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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