2

I am trying to write a function in our erc721 contract to purchase an nft token with an erc20 token. As I understand it, it is not possible to import erc20 into an erc721. Does that mean the purchase function goes into a separate contract by itself? In that case, how would we access the mint function from the erc721 contract?

I have searched all over google and stack exchange with "solidity purchase nft with erc20 token", and can't find anything. Which means i am probably missing something obvious

2

1 Answer 1

1

It is possible, but the flow/ux will be slightly different than buying from eth (where user send eth along the mint() transaction):

-user will have to approve (ie your frontend need to handle it) your erc721 contract as spender of his (the users') erc20

-your mint function will then call the erc20.transferFrom(user, you/your contract, the amount) and mint if successful.

function mint(uint256 quantity) external returns (bool) {
    require(erc20.transferFrom(msg.sender, address(this), price*quantity), "transfer error");
    _mint(quantity, user);
} //this is a mock and need further logic/control, esp if you accept third-party erc20
3
  • Thank you I appreciate this answer! The front-end calling Approve was the missing piece of the puzzle. Commented Nov 12, 2021 at 18:28
  • @DabblinDantheDino I'm stuck at exactly the same problem. I want to deploy my contract on Polygon but require users to pay in WETH not MATIC. can you please guide on how did you manage to get this done? I'd appreciate any help!!! thanks Commented Jan 21, 2022 at 11:18
  • Sure thing, have you tried making an erc20 interface? If the token follows the standard, you should be able to use this eips.ethereum.org/EIPS/eip-20 docs.openzeppelin.com/contracts/2.x/api/token/erc20 Commented Jan 24, 2022 at 22:18

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.