Address with default unlimited allowance

I’m writing a smart contract to emit events. I’d like to charge erc20 tokens for this service using the “transferFrom” function. However, the current erc20 implementation forces the paying user to first allow the smart contract address to withdraw tokens from him.

Is there any way I can allow my smart contract to withdraw tokens from any account without needing explicit allowance?

:1234: Code to reproduce

contract StampProofs {
    uint256 constant private STAMP_PRICE = 1;
    DAOInterface DAOContract;
    EIP20Interface erc20;

    event stampProof(bytes32 indexed hash, uint256 timestamp);

    constructor(address _daoAddress) public {
        DAOContract = DAOInterface(_daoAddress);
        address erc20Address = DAOContract.getERC20();
        erc20 = EIP20Interface(erc20Address);
    }

    function setProof(address payerAddress, bytes32 hash) public{
        erc20.transferFrom(payerAddress, address(this), STAMP_PRICE);
        emit stampProof(hash, block.timestamp);
    } 
}
1 Like

Hi @nilquera,

Welcome to the community :wave:

With ERC20 tokens, token holders need to approve an allowance before they can be used in another contract (see: Example on how to use ERC20 token in another contract)

There is a discussion on a permit function but this would only be for tokens that were created with this functionality: Add ERC20 permit() function

Unfortunately, you will need token holders to perform two transactions.

Hi @nilquera,

Did you have any more questions?

Hi @abcoathup, your answer helped me a lot.

Our blockchain is private, so we are thinking of modifying the ERC20 implementation to simply allow certain smart contracts to move tokens as they want. This seems the most straight-forward solution. Do you think it’s a good idea?

1 Like

Hi @nilquera,

Assuming that you are creating new tokens on your chain rather than using existing ones then I would be tempted to add a permit to the ERC20.

Potentially this one: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2206#issuecomment-706788109

Alternatively you could use ERC777. (https://docs.openzeppelin.com/contracts/3.x/erc777)

Out of interest what are you building?