I have a smart contract, which has an address when deployed. I want users to send ERC20 tokens to the smart contract address without using specific functions of the contract (USDC for instance) and I want to be able to trigger functions when this happens. Fundamentally, I want to keep track of the number of ERC20 tokens that each address has sent to the contract address.
I am looking for a similar solution to the receive() function, which would work if what I receive in the smart contracts is ETH, and I can just use msg.value. However, with ERC20 the receive function is not triggered.
Any ideas? Thanks in advance
I want users to send ERC20 tokens to the smart contract address without using specific functions of the contract (USDC for instance) and I want to be able to trigger functions when this happens.
ERC-20 does not support this. Newer token standards, like ERC-777, support this use case. With some luck, some tokens implement transferAndCall() or similar function, but it is not part of ERC-20 standard.
For ERC-20 you need to approve() and transferFrom() dance - which is not ideal.
After some research, I think it is not possible to trigger an event from the smart contract when the tokens arrive, because, in the process of sending the ERC20 tokens, the interaction happens with ERC20 token contract, not with the smart contract where the tokens will arrive.
This is because the only place where the ERC20 balances are altered is in the ERC20 token contract itself, not in your smart contract.
Therefore, there is no way to trigger any event when the erc20 tokens arrive at the smart contract. That I know of.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With