Minting erc721 but paying with erc20 token instead of ether

hope all is well.

I have erc721 contract from openzeppelin @openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol

Where I today let users mint with ether:

function mint(address _to, uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    require(!paused);
    require(_mintAmount > 0);
    require(_mintAmount <= maxMintAmount);
    

    if (msg.sender != owner()) {
        if(whitelisted[msg.sender] != true) {
          require(msg.value >= cost * _mintAmount);
        }
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(_to, supply + i);
    }
  }

Ive been trying to figure out how to switch the ether into my own erc20 token for days now and have been googling around but cannot find anything. If someone has any ideas they can share or links to point me into right direction that would be much appreciated!

thanks in advance


Solution 1:

For doing this you should implement ERC20 into your ERC721 contract.

I would suggest first importing the ERC20 file from openzeppelin, and then creating an ERC20 variable which will be a pointer to your existing ERC20 token. Something like this:

ERC20 token = ERC20('address to your desired ERC20 Token');

Then you will be able to interact with the ERC20 token balance of the msg.sender using the 'balanceOf', 'approve', and 'transferFrom' functions.

Hope you find this information useful :)