Skip to content

Instantly share code, notes, and snippets.

@KardanovIR
Created September 22, 2020 15:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KardanovIR/fe98661df9338c842b4a30306d507fbd to your computer and use it in GitHub Desktop.
Save KardanovIR/fe98661df9338c842b4a30306d507fbd to your computer and use it in GitHub Desktop.
library GetCode {
function at(address _addr) public view returns (bytes memory o_code) {
assembly {
// retrieve the size of the code, this needs assembly
let size := extcodesize(_addr)
// allocate output byte array - this could also be done without assembly
// by using o_code = new bytes(size)
o_code := mload(0x40)
// new "memory end" including padding
mstore(0x40, add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f))))
// store length in memory
mstore(o_code, size)
// actually retrieve the code, this needs assembly
extcodecopy(_addr, add(o_code, 0x20), 0, size)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment