1

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 2

4

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)

1
  • But why the resultant bytecodes are different since both get the bytecode excluding init code ?
    – Xirexor
    Commented Dec 2, 2023 at 7:54
0

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.

3
  • I noticed a difference in the resultant bytecodes. Why so ?
    – Xirexor
    Commented Dec 2, 2023 at 7:55
  • @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
    – Foxxxey
    Commented Dec 4, 2023 at 11:18
  • In general both return runtime code right ?
    – Xirexor
    Commented Dec 5, 2023 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.