0

I wrote the following contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract enviaToken {

    address public tokenAddress = 0x;

    address public acc1 = 0x;
    address public acc2 = 0x;


    function giveAllowance() public {

        IERC20 token = IERC20(tokenAddress);
        token.approve(address(this), 1);
    }

    function send() public {

        IERC20 token = IERC20(tokenAddress);

        require(token.allowance(acc1, address(this)) > 0);

        uint256 saldoacc1 = token.balanceOf(acc1);
        require(saldoacc1 >= 1 ether, "Saldo insuficiente na acc1");

        require(token.transferFrom(acc1, acc2, 1));
    }

}

But when I call the send function in remix i receive the "insufficient allowance" error...

This is just a test contract, thats why I hardcoded the address (removed the hex here).

My MetaMask is connected to the remix, when I deploy, call "giveAllowance" and "send" a Pop-Up appears.

My acc1 has 100 tokens in Sepholia.

I searched everywhere, and always is the same answer: call the approve function to give allowance.

But even giving the needed allowance (calling the "giveAllowance" before transfering, successfully) I got the error.

2 Answers 2

0

IERC20 has these two methods:

allowance(address owner, address spender)

You can call this function to set approval for the contract address as spender.

increaseAllowance(address spender, uint256 addedValue)

You can call this function to increase allowance for the contract address to spend.

Transfer the tokens from account 1 to contract address, thus making the contract address the owner of those tokens then simply call function from within the contract address to transfer to another account.

transfer(address recipient, uint256 amount)
0
  • here is non-functional way to approve tokens to save time with that remix error.

enter image description here

  • paste the token address in "at address" section which you want to approve on that particular chain.

enter image description here

  • now you have approve section for that token.

  • enter address and value that you want to approve.

  • then you have approve section of metamask

enter image description here

this is not recommended way but for saving time with no code of JavaScript to approve tokens.

- although you must try with javascript code to manually approve tokens for users if you are using for Dapp or Defi.

here is your answer about why giveAllowance is not wokring:

  • it is not working because it attempts to approve the contract to spend tokens from its own balance, but allowances must be granted by the token owner (in this case, acc1), not the contract itself. Approval must come from the owner's wallet, not within the 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.