First of all, I'm searching for how to resolve my pb for 3 days. I've read a lot of topics describing the same error, but none of them had helped me.
I ONLY need to deposit and withdraw an ERC20 token in my contract. But I have an insufficient allowance error.
execution reverted: ERC20: insufficient allowance
Here is my code :
// SPDX-License-Identifier: Me
pragma solidity ^0.8.9;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Staking {
mapping(address => uint) TKNbalance;
address TokenAddress = 0x1234567891234567891234567891234567891234;
function approveTKN(uint256 _amount) external {
IERC20(TokenAddress).approve(address(this), _amount);
}
function depositTKN(uint256 _amount) external {
IERC20(TokenAddress).transferFrom(msg.sender, address(this), _amount);
TKNbalance[msg.sender] += _amount;
}
function withdrawTKN(uint256 _amount) external {
require(_amount <= TKNbalance[msg.sender], "!!! Not enough funds deposited !!!");
IERC20(TokenAddress).transfer(msg.sender, _amount);
TKNbalance[msg.sender] -= _amount;
}
function getTKNbalance() public view returns(uint) {
return TKNbalance[msg.sender];
}
}
I've tested like always in remix before writing my frontend. When I use my approveTKN function, it works, and the transaction is apparently a success. But when I call my allowance function in the token contract : Allowance error
No way to trasnfer anything. However, if I use directly the approve function in my token contract to allow the main contract to spend my tokens, it works, my allowance is increased by the value needed, and I can use the transfer function of my main contract...
It seems that I don't use correctly the approve function of the erc20 contract through the main contract...
I'm tearing my hair out...