Allowance error in erc20 token receive contract

Hello, I am testing a smart contract that accepts erc20 and erc721 tokens. I am having a problem of sending erc20 to this smart contract. When im calling the function which transfers tokens, the error “execution reverted: Check the token allowance” is displayed. Obviously, this error is due to the fact that the allowance of my erc20 tokens is 0. The function code is attached below. What am I doing wrong with allowance setup?
:computer: Environment

I’m using online remix ide for developing my contracts and then they have been deployed it to rinkeby testnet. To interact with contracts, I am using my ether wallet.
:memo:Details

:1234: Code to reproduce
here is the code of my function:

  function put_aktiv(uint256 number_erc20, uint256 number_erc721, uint256 token721_id) public payable  {   
	uint256 token1155_id;
        token20.approve(address(this), 100); //approve  allowance
        uint256 allowance = token20.allowance(msg.sender, address(this));
        require(allowance >= 1, "Check the token allowance");
        token20.transfer(address(this),1);
        token20.transferFrom(msg.sender, address(this), number_erc20); 
        token721.transferFrom(msg.sender, address(this), token721_id);
        token1155_id = create_erc1155_token(number_erc20, token721_id); 
        token1155.safeTransferFrom(address(this), msg.sender, token1155_id, 1,"");  
    }

Hi, welcome! :wave:

It seems like you use a wrong way to approve, you should call token20.approve(address(CONTRACT), 100) directly rather than in a contract function, cause when you call approve in a contract, the msg.sender is the contract rather than the user.

1 Like

how can i realise this solution in my contract? Can I add approve function to contract inheritance, such as: "contract MAINCONTRACT5 is ERC20", so i can call approve function from contract?

You should call token20.approve(address(CONTRACT), 100) in the token20 contract rather than in you own contract.

I solved this problem by passing the sender and contract addresses to the function, this is unsafe, but it may exist in the mvp. Thanks for your help

1 Like

Hi @Volandemort,

See the example: Example on how to use ERC20 token in another contract


As an aside you can Format code in the forum