How to find out if an Ethereum address is a contract?

后端 未结 6 837
谎友^
谎友^ 2020-12-24 13:31

An address in Solidity can be an account or a contract (or other things, such as a transaction). When I have a variable x, holding an address, how can I test if it is a cont

6条回答
  •  爱一瞬间的悲伤
    2020-12-24 14:25

    Yes you can, by using some EVM assembly code to get the address' code size:

    function isContract(address addr) returns (bool) {
      uint size;
      assembly { size := extcodesize(addr) }
      return size > 0;
    }
    

提交回复
热议问题