My first time here, please forgive me for any mistakes.

I made a custom ERC20 token using Openzeppelin ERC20PresetFixedSupply. I want to use that in my contract. I was using this code to test how IERC20 approve and allowance work.

    constructor(IERC20 token) {
        _token = token;
    IERC20 private _token;
    function ERC20Balance() external view returns (uint256) {
        return _token.balanceOf(msg.sender);
    function ERC20TotalSupply() external view returns (uint256) {
        return _token.totalSupply();
    function ERC20Allowance() external view returns (uint256) {
        return _token.allowance(msg.sender, address(this));
    function ERC20Approve() external returns (bool) {
        return _token.approve(address(this), _token.balanceOf(msg.sender));

This is the output I got after execution. I approved balanceOf(msg.sender) for the contract to spend but it shows contract has 0 allowance.


What am I doing wrong here? Thanks.

  • Inside ERC20Aprove(), the caller of the approve function is the contract itself, not the transaction sender. So currently your contract is just approving itself.
    – clement
    May 2 at 12:42
  • Your user has to directly call the approve function on the ERC20 smart contrat : copy paste the ERC20 contract on Remix and call approve with your contract address as the spender parameter.
    – clement
    May 2 at 12:46
  • 1
    Okay, so the caller is the contract itself, got it. I'll try out your suggestions. Thanks. May 2 at 17:07