Can anyone please clear my doubt about the difference between address.code and type(Contract).runtimeCode in solidity. Shouldn't they be same.
2 Answers
They have very little in common, actually.
address.code
is done on chain, it corresponds to the EXTCODECOPY opcode, and it allows you to retrieve the code stored at an address (i.e, a deployed contract).
type(Contract).runtimeCode
is done by the compiler, it doesnt load the code from an arbitrary address, it computes the runtime code of a contract definition it knows (that is in your imports or in your contract file)
-
But why the resultant bytecodes are different since both get the bytecode excluding init code ?– XirexorCommented Dec 2, 2023 at 7:54
There is a difference between the two
address.code
gives the code stores at the account belonging to address.
This could be any address (EOA's will return zero as they don't contain any code). For this, the address needs to exist on the blockchain.
type(Contract).runtimeCode
takes in a contract and computes the runtime code of the contract. Since this only computes it based on the contract definition, the contract doesn't have to be on the mainnet.
-
-
@Xirexor could be a number of reasons. Two most obvious that comes to mind are different compiler version/flags, and immutable variables initialized during contract construction. But could be anything else too– FoxxxeyCommented Dec 4, 2023 at 11:18
-