Solidity ERC20 – Understanding and Resolving Insufficient Allowance Issues

etherethers.jsnftremixsolidity

I get this error when the createVaultItem function is called. My solidity knowledge is not enough. Why am I getting this error?

Market.sol

  function createVaultItem(address nftContract,uint256 tokenId,uint256 price) public payable nonReentrant {
    require(price > 0, "Price cannot be zero");
    _itemIds.increment();
    require(msg.value == listingFee, "Price cannot be listing fee");
    require(mytoken.transferFrom(msg.sender,address(nftContract), listingFee), "Sending Failed");
    uint256 itemId = _itemIds.current();
    idToVaultItem[itemId] =  VaultItem(itemId,nftContract,tokenId,payable(msg.sender),payable(address(0)),price,false);
    IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId);
    emit VaultItemCreated(itemId,nftContract,tokenId,msg.sender,address(0),price,false);
    }

nft.sol

    function createNFT(string memory tokenURI) public returns (uint) {
        _tokenIds.increment();
        uint256 newItemId = _tokenIds.current();
        _mint(msg.sender, newItemId);
        _setTokenURI(newItemId, tokenURI);
        setApprovalForAll(contractAddress, true);
        return newItemId;
    }

ether.js

    async function createNFT(url) {
        let amount = 900000000000;
        const web3Modal = new Web3Modal()
        const connection = await web3Modal.connect()
        const provider = new ethers.providers.Web3Provider(connection)
        const signer = provider.getSigner()
        let cri = new ethers.Contract(hhtoken,Token,signer)
        await cri.approve(hhnft,amount)
        let contract = new ethers.Contract(nftcontract, NFT, signer)
        let transaction = await contract.createNFT(url)
        let tx = await transaction.wait()
        let event = tx.events[0]
        let value = event.args[2]
        let tokenId = value.toNumber()
        contract = new ethers.Contract(market, Market, signer)
        await cri.approve(hhmarket,amount)
        let listingFee = await contract.getListingFee()
        listingFee = listingFee.toString() 
        const price = ethers.utils.parseUnits(formInput.price, 'ether')
        transaction = await contract.createVaultItem(nftcontract, tokenId, price, { value: listingFee })
        await transaction.wait()
        router.push('/')
    }```

Best Answer

You need to increase the allowance inside the ERC20 token contract.

Check which transferFrom() fails and increase the allowance of the 'from' to the 'to' of the transferFrom that lack allowance

Try something like myToken.increaseAllowance() with the parameters you need.

Hope this helps

Related Topic