Is a reliable way to identify the caller with the following code?

bool isContract = msg.sender != tx.origin;
  • 1
    Please don't exclude contracts from interacting with your contract. If it's to avoid some security vulnerability, it's probably ineffective, and it means that no one using a multisig wallet can use your contract. tx.origin may also stop working in future Ethereum versions, especially as account abstraction work comes in. – user19510 Jan 5 '19 at 21:55

This should work. Also you can check the size of the code in the address if it is bigger than zero, is a contract

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

Hope this helps you

| improve this answer | |
  • 1
    Note that during execution of a contract's constructor, that address's code size will be 0. – user19510 Jan 5 '19 at 21:53

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