2

I can create NFT's with an ERC20 token but I want to set the price of a NFT to an ERC20 token too, not ETH.

For example on OpenSea I can sell my NFT's for ETH but I want to get payed in any ERC20 token I would like to.How could I do that?

enter image description here


You can reach the contract I developed here and you can get help by contacting me.

Current Solution


Diagram of the contract structure

enter image description here

3

3 Answers 3

1

Yes, this is a common question.

I have fully spelled this out as a demo project including the smart contracts and the front end JavaScript.

https://github.com/fulldecent/spend-ERC20-create-ERC721

This project was sponsored by Arianee.

6
  • 1
    youtube.com/watch?v=3SiMPZbwlR0 I watched the video now I'm trying to solve the steps Nov 7, 2021 at 13:41
  • 1
    ok this looks like a workable way, i tried it and it works. Nov 7, 2021 at 16:21
  • Please accept answer if this is good / this will allow me to mark other related questions as duplicate Nov 11, 2021 at 23:46
  • github.com/FaruNuriSonmez/Solidity-Developer-s-Guide/tree/… I recreated the codes for solidity 0.8 version. Nov 12, 2021 at 15:51
  • function _addSpender(address account) internal { spenders.add(account); emit SpenderAdded(account); } I'm updating the function for solidity 8, I'm not sure if it's correct can you help? function _addSpender(address account) internal { _setupRole(BURNER_ROLE,account); emit SpenderAdded(account); } Nov 27, 2021 at 18:51
0

Where to start - read about ERC20 tokens and the approval mechanism.

Your front end will issue 2 transactions for the user to sign - one to approve the ERC20 payment, and one to your "mint" function which will transfer the payment to you and send the tokens to the user.

If you don't know how to do this, read about ERC20's allowance mechanism.

5
  • " Your front end will issue 2 transactions for the user to sign " - i don't understand this can you help me. Oct 27, 2021 at 23:50
  • Currently I have created web application and created an ERC 721 Token and through web application I can generate nft with eth pricing and another user can purchase this nft for eth. Oct 27, 2021 at 23:51
  • Now, how will I price the contract I created with erc721 with erc20 tokens without using eth while generating nft? Oct 27, 2021 at 23:51
  • So at the moment you have a web application that calls something like the "mint" function of your contract. In your webapp, before you call the mint function, you should call the ERC20 token's "approve" method and approve the NFT contract to spend the user's funds. Then, in your contract, will mint function will change - it will not receive ETH anymore but would use transferFrom to transfer the price of the NFT from the user to you. If this is still not clear, please read on ERC20 approval mechanism. Oct 28, 2021 at 1:46
  • function mint(address to, uint256 tokenId, string memory tokenURI) public { require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint"); _mint(to, tokenId); _setTokenURI(tokenId, tokenURI); } Is this possible ti pay for mint process with erc20 token not eth? Nov 1, 2021 at 1:56
0

I come late to the party but I want to answer as I was working on a similar problem for several days and the answer is quite simple.

I am showing here an example to use USDC inside a ERC721 contract. The first step is to use an interface in your ERC721 contract so you can access the functions in the ERC20 contract:

interface USDC{
    function balanceOf(address owner) external view returns (uint);    
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}

Then in your constructor you have to add the address of the ERC20 token contract (in my case I used the goerli USDC contract):

constructor() ERC721("Mavon Minted NFTs", "MNFTs") {
        owner = msg.sender;
        USDc = USDC(0x98339D8C260052B7ad81c28c16C0b98420f2B46a);
 }

And finally, in the function where the transaction is made, you add the following ERC20 function:

 USDc.transferFrom(msg.sender, seller, priceUSD);   

Now, a very important thing and that took me a couple of days to discover, is that to use transferFrom, the buyer has to give your contract approval to move the funds in his wallet. This is accomplished by using the ERC20 function approve(spender, amount). The thing is, this approve function cannot be called within the same contract where you have the function that makes the funds transaction, so you have two options:

  • make another contract to approve the transaction so you can use transferFrom;
  • call the approve function in the ERC20 contract from the frontend, like you would do with any other contract.

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.