1

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

0

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.

4
  • Thanks, but is there anyway we can check the address of the token that is sent? 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. 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? Aug 11, 2019 at 22:22
  • You obviously did not understand me answer. Please read it more carefully. Aug 12, 2019 at 5:15
0

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.

1
  • 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
    Mar 11 at 21:44
0

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

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

Your Answer

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

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