What happens during contract creation

Recall that during contract initialization, the following happens:

1. First, a user or contract sends a transaction to the Ethereum network. This transaction contains data, but no recipient address. This format indicates to the EVM that is a contract creation , not a regular send/call transaction.

2. Second, the EVM compiles the contract code in Solidity (a high level, human readable language) into bytecode (a low level, machine readable language). This bytecode directly translates into opcodes, which are executed in a single call stack.

Important to note: contract creation bytecode contains both 1) initialization code and 2) the contract’s actual runtime code , concatenated in sequential order.

3. During contract creation, the EVM only executes the initialization code until it reaches the first STOP or RETURN instruction in the stack. During this stage, the contract’s constructor() function is run, and the contract has an address.

3.1. After this initialization code is run, only the runtime code remains on the stack. These opcodes are then copied into memory and returned to the EVM.

5. Finally, the EVM stores this returned, surplus code in the state storage, in association with the new contract address. This is the runtime code that will be executed by the stack in all future calls to the new contract.

Put simply

To solve this level, you need 2 sets of opcodes:

Initialization opcodes : to be run immediately by the EVM to create your contract and store your future runtime opcodes, and

: to be run immediately by the EVM to create your contract and store your future runtime opcodes, and Runtime opcodes : to contain the actual execution logic you want. This is the main part of your code that should return 0x 0x42 and be under 10 opcodes.

At this point, to independently solve this level, you can read in depth about opcodes and smart contract deconstruction (from the author of this Ethernaut level).

For a bit more guidance, let’s press on…