From f2d0304537616dc5c13e038935939bfdb813f17c Mon Sep 17 00:00:00 2001 From: Julian R <56316686+julianmrodri@users.noreply.github.com> Date: Mon, 13 Jul 2020 19:13:30 -0300 Subject: [PATCH] feat: use extcodesize for isContract to reduce gas --- contracts/utils/Address.sol | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/contracts/utils/Address.sol b/contracts/utils/Address.sol index 1d952cc0777..fe3ba653709 100644 --- a/contracts/utils/Address.sol +++ b/contracts/utils/Address.sol @@ -24,14 +24,14 @@ library Address { * ==== */ function isContract(address account) internal view returns (bool) { - // According to EIP-1052, 0x0 is the value returned for not-yet created accounts - // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned - // for accounts without code, i.e. `keccak256('')` - bytes32 codehash; - bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470; + // 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 { codehash := extcodehash(account) } - return (codehash != accountHash && codehash != 0x0); + assembly { size := extcodesize(account) } + return size > 0; } /**