Skip to content

Instantly share code, notes, and snippets.

@Amirhb
Last active March 22, 2021 11:52
Show Gist options
  • Save Amirhb/8c3856e051dd95c4f4b406cf202dd695 to your computer and use it in GitHub Desktop.
Save Amirhb/8c3856e051dd95c4f4b406cf202dd695 to your computer and use it in GitHub Desktop.
Solidity library to check if an address pointing to a contract
// source: uniswap
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* This test is non-exhaustive, and there may be false-negatives: during the
* execution of a contract's constructor, its address will be reported as
* not containing a contract.
*
* > It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*/
function isContract(address account) internal view returns (bool) {
// This method relies in extcodesize, which returns 0 for contracts in
// construction, since the code is only stored at the end of the
// constructor execution.
uint256 size;
// solhint-disable-next-line no-inline-assembly
assembly { size := extcodesize(account) }
return size > 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment