I have an ERC20 token XYZ smart contract created. People will send certain amount of tokens back to the contract address to initiate certain functions. I just want to know that what will happen if someone send other tokens to my contract? May be token ABC, UUY, etc, like any token other than my own "XYZ" token. I want to grab those tokens in my other wallet and dont want to initiate any functions based on those. I think any functionality that can check address of the tokens sent can be beneficial, but I am open to suggestions.

3 Answers 3


You can't prevent tokens being sent to a contract as the tokens are not explicitly sent anywhere, only balances are shuffled around. Furthermore due to the same reason you can't detect when your token receives tokens. You can read more about this for example here: Smart contract that receives only one token

You can, however, add functionality in your contract to handle the other tokens - for example transfer them to another wallet. But this kind of check has to be initiated by someone (probably you) and you have to tell the contract which tokens to check - the contract can't check all possible token balances.

Easiest would be probably to include basic token transfer functionality in your contract (not to only transfer the token created by the contract but other tokens) and to implement a backend system which checks for token balances and initiates a token transfer if needed. But also in this case your backend system has to know which token balances to monitor.

  • Thanks, but is there anyway we can check the address of the token that is sent? Commented Aug 11, 2019 at 17:27
  • You have to know the token contract address to be able to monitor token transfers. If you don't know the token contract address you can't know whether you receive those tokens or not - that address is the only place which keeps track of who has how many tokens. Commented Aug 11, 2019 at 19:59
  • I want to filter all other tokens except mine. Can we retrieve the token contract address when transfer is made? like checking which token (address) has been transferred? Commented Aug 11, 2019 at 22:22
  • You obviously did not understand me answer. Please read it more carefully. Commented Aug 12, 2019 at 5:15

You could have a function like this:

    function transferTokens(address _erc20) external onlyOwner {
        if(_erc20.balanceOf(address(this) > 0)) {
            uint balanceOfContract = _erc20.balanceOf(address(this));
            _erc20.transfer(msg.sender, balanceOfContract);

So that way, no matter what erc20 is sent to your contract, you have a safe way to send them from your contract to your address.

Now, you can't really know what erc20 is sent to your contract, but you can check etherscan to see if anyone interacted with your contract and call the transferTokens function if someone sent you some tokens.

I'm sure there are better ways, but can't think of any at the moment.

  • If you do this also make sure to blacklist any tokens that you want to be transferred in or used for other purposed in the contract, to remove centralized risk
    – Bruce
    Commented Mar 11, 2023 at 21:44

I like to implemente the receive() special funcion to this cases:

receive() external payable {
  • This works when sending ETH not tokens
    – 0xSanson
    Commented Oct 10, 2022 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.