ERC20 Tokens – How to Build an Abstract Contract for Users to Claim Tokens

allowanceerc-20solidity

My goal is to build an abstract contract and send a specific erc20 token into it so that others can claim the erc20token with merkletree validations.
However I am having problem approving the contract to spend this erc20. Here is my claiming function. Please help me figure out how to assign allowance or otherwise. Is there a walk-around to achieve the goal ?

function claimTokens(IERC20 _token, uint256 amount, bytes32[] calldata merkleProof) public {
        uint256 erc20balance = _token.balanceOf(address(this));
        require(amount <= erc20balance, "balance is low");
        bytes32 leaf = keccak256(abi.encodePacked(msg.sender, amount));
        bool valid = MerkleProof.verify(merkleProof, merkleRoot, leaf);
        require(valid, "Chimpz: Valid proof required.");
        require(!claimed[msg.sender], "Tokens already claimed.");
        claimed[msg.sender] = true;
    
        emit Claim(msg.sender, amount);
        _token.transferFrom(address(this), msg.sender, amount);
    }

Best Answer

You should be able to transfer tokens via the transfer() function:

 function transfer(address recipient, uint amount) external returns (bool);

Transfer does not require approval.

Related Topic