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.

1 Answer 1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.