Encountering allowance issue when minting NFT with ERC20 token payment

Hey everyone! I’m working on a project with two contracts: an ERC1155 for NFT minting and an ERC20 for token payments. I’m trying to let users mint NFTs by paying with the ERC20 token, but I’m running into a problem with the transferFrom() function.

The error I’m getting is: brownie.exceptions.VirtualMachineError: revert: ERC20: transfer amount exceeds allowance. I’ve been looking around for solutions, but no luck so far.

Here’s a simplified version of my ERC1155 contract:

contract CryptoCollectibles is ERC1155, Ownable {
    IERC20 private _cryptoToken;

    constructor(IERC20 cryptoToken) ERC1155("ipfs://QmRandomHash/{id}.json") {
        _cryptoToken = cryptoToken;
    }

    function mintCollectible(address user, uint256 tokenId, uint256 quantity) public {
        require(_cryptoToken.transferFrom(msg.sender, address(this), 1));
        _mint(user, tokenId, quantity, "");
    }
}

And here’s the ERC20 contract:

contract CryptoToken is ERC20, Ownable {
    uint256 public maxSupply = 1000000 * 10**18;

    constructor() ERC20("CryptoCollectibles Token", "CCT") {
        _mint(msg.sender, maxSupply);
    }

    function grantTokens(address to, uint256 amount, bool condition) public onlyOwner {
        require(condition == true);
        _mint(to, amount);
    }

    function approveContractSpending(address contractAddress) public onlyOwner {
        approve(contractAddress, maxSupply);
    }
}

Any ideas on what I’m doing wrong? Also, should I make the mintCollectible() function payable? Thanks for any help!

hey there fellow nft enthusiast! :art::sparkles:

i’ve been tinkering with similar stuff and ran into the same headache. have you considered using safeTransferFrom() instead of transferFrom()? it’s a bit safer and might help dodge that pesky allowance issue.

also, curious about your tokenomics - how are you planning to distribute those erc20 tokens? any cool ideas for incentivizing early adopters or community engagement?

oh, and random thought - have you looked into gasless minting? could be a game-changer for user experience. what do you think?

keep us posted on your progress! can’t wait to see what awesome collectibles you’re cooking up :sunglasses:

The issue you’re encountering is likely due to insufficient allowance for the ERC1155 contract to spend tokens on behalf of the user. Here’s what you need to do:

  1. Users must approve the ERC1155 contract to spend their tokens before minting.
  2. In your frontend or script, call the approve() function on the ERC20 contract, specifying the ERC1155 contract address and the amount to approve.
  3. After approval, users can call mintCollectible().

Your mintCollectible() function looks correct and doesn’t need to be payable since you’re using ERC20 tokens for payment.

Also, consider implementing a mintPrice variable in your ERC1155 contract to make the token cost configurable. This way, you can easily adjust the price without changing the contract code.

Remember to thoroughly test your contracts and consider getting a security audit before deployment.

yo mate, the problm is probs with allowance. make sure ur users approve the ERC1155 contract to spend their tokens before they try to mint. they gotta call approve() on the ERC20 contract first, then they can mint.

also, u might wanna add a mintPrice variable to ur ERC1155 contract so u can change the price easily. good luck!