Skip to content

Instantly share code, notes, and snippets.

@finxter
Forked from yassesh/isItContract.sol
Created January 1, 2022 12:51
Show Gist options
  • Save finxter/00c7e011b60321422d6bdcdc4c0681a3 to your computer and use it in GitHub Desktop.
Save finxter/00c7e011b60321422d6bdcdc4c0681a3 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
pragma solidity ^0.8.0;
contract isItContract{
// this function gets the address of a contract
function contractAddress() public view returns (address) {
address theAddress = address(this); //contract address
return theAddress;
}
// these functions check if the address is a smartcontract
// method 1
function checkContract(address addr) public view returns (bool) {
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
bytes32 codehash;
assembly {
codehash := extcodehash(addr)
}
return (codehash != 0x0 && codehash != accountHash);
}
// method 2
function isContract(address addr) public view returns (bool) {
uint size;
assembly { size := extcodesize(addr) }
return size > 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment