I am testing my ERC-20 token in Remix on the virtual machines. The transfer function works, but I cannot get the transferFrom function to work, it keeps throwing "ERC20: transfer amount exceeds allowance". As address A in the VM, I have approved B to transfer to address C. I increased allowances and even checked the allowance function for address B but I still cant get this function to work.
Code to reproduce
// SPDX-License-Identifier: MIT
pragma solidity ^ 0.8.10;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
//-----------------------------------------------------------------------------------------| BEGIN CONTRUCTOR AND STATE VARIABLES
contract ElonGatoMarsTest is ERC20 {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply = 1000000000000 * (10 ** 18);
uint256 private _minimumSupply = 69420000 * (10 ** 18);
string private _symbol = "EGMT";
string private _name = "EGMTTest";
constructor() ERC20("ElonGatoMarsTest", "EGMT") {
_mint(msg.sender, 1000000000000 * (10 ** 18));
}
//-----------------------------------------------------------------------------------------| END CONSTRUCTOR, BEGIN BURN LOGIC
function calculateBurnAmount(uint256 amount) private view returns (uint256) {
uint256 burnAmount = 0;
if (_totalSupply > _minimumSupply) {
burnAmount = (amount / 20);
}
return burnAmount;
}
//-----------------------------------------------------------------------------------------| END BURN LOGIC, BEGIN BURN IMPLEMENNTATION
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
uint256 amountToBurn = calculateBurnAmount(amount);
uint256 amountToTransfer = (amount - amountToBurn);
_transfer(_msgSender(), recipient, amountToTransfer);
_burn(msg.sender, amountToBurn);
assert(_totalSupply >= _minimumSupply);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
uint256 amountToBurn = calculateBurnAmount(amount);
uint256 amountToTransfer = (amount - amountToBurn);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
unchecked {
_approve(sender, _msgSender(), currentAllowance - amount);
}
_transfer(sender, recipient, amountToTransfer);
_burn(sender, amountToBurn);
assert(_totalSupply >= _minimumSupply);
return true;
}
}
Environment
Is there something wrong with my transferFrom function?