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.


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.

| improve this answer | |
  • Thanks, but is there anyway we can check the address of the token that is sent? – Fariha Abbasi Aug 11 '19 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. – Lauri Peltonen Aug 11 '19 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? – Fariha Abbasi Aug 11 '19 at 22:22
  • You obviously did not understand me answer. Please read it more carefully. – Lauri Peltonen Aug 12 '19 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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