0

I want my ERC20 contract to be transferred to EOA addresses only and some specific contract address only.

I don't want to lose my tokens by mistakenly transferring to a contract address that cannot handle ERC20 tokens.

How can I prevent my ERC20 contract to prevent the transfer of tokens to contract addresses?

MYANZIK shrestha
  • 788
  • 1
  • 7
  • 14

2 Answers2

0

Include this line at the beginning of your ERC20 transfer function:

require (to != address(this));
Extrange planet
  • 685
  • 5
  • 11
0

You should modify the contract's transfer and approve functions to check if the target address is a contract address. If it is, revert the transaction.

You can check if an address is a contract address with:

function isContract(address _addr) private returns (bool isContract){
  uint32 size;
  assembly {
    size := extcodesize(_addr)
  }
  return (size > 0);
}

(From https://ethereum.stackexchange.com/a/15642/31933)

Note that this makes any token transfers more expensive as the same check has to be performed every time a transfer/approve is made.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57