Skip to content
LogoLogo

Calls, Delegatecall & Create

Evm.call() accepts a 6-variant tagged union (Evm.CallParams) covering every Ethereum message call and contract creation mode.

Inside vm.call(params) — Exact Execution Ordersrc/evm.zig · call()
1Validateinput ≤ 4 MBinitcode ≤ 48 KB2Resolve TargetCREATE/2 addr or tosender = vm.origin3Reset & WarminitTransactionState()preWarmTransaction()4DispatchCREATE, or get_code()→ Precompile / FrameSTOP / RETURN (success = true)Copy output & logs · expose refund_counter · commit stategas_left = frame.gas_remainingREVERT / CallError (success = false)Reverse value transfer · restore snapshots · drop logsREVERT keeps gas_left + output; errors burn gas & set error_info5. End-of-tx cleanup: clear warm sets → clear transient storage (EIP-1153) → delete selfdestructed (EIP-6780) & empty accounts
Step 3 resets per-transaction state (original and transient storage, warm sets, logs, refunds) but keeps balances, nonces, code, and committed storage. Step 4 reads the target code after that reset.

The six call shapes

VariantCode executed fromStorage & ADDRESSCALLER (msg.sender)VALUE (msg.value)State writes
.calltotocallervalueAllowed
.callcodetocallercallervalueAllowed
.delegatecalltoparent frameparent's callerparent's valueAllowed
.staticcalltotocaller0 (no value field)Forbidden
.createinit_codenew addresscallervalueAllowed
.create2init_codenew addresscallervalueAllowed

Regular call vs static call

// State-changing call
const res = vm.call(.{ .call = .{
    .caller = alice,
    .to = contract,
    .value = 0,
    .input = calldata,
    .gas = 200_000,
} });
 
// Read-only call (SSTORE, TSTORE, LOG0-4, CREATE, CREATE2, SELFDESTRUCT, value-bearing CALL all fail)
const view = vm.call(.{ .staticcall = .{
    .caller = alice,
    .to = contract,
    .input = calldata,
    .gas = 50_000,
} });

Contract creation (CREATE and CREATE2)

Pass initialization bytecode in init_code. The EVM executes init_code as a frame, validates the returned runtime bytes, charges the code-deposit fee (200 * runtime.len gas), installs the runtime code at the derived address, and sets result.created_address.

// Init code that deploys 1 byte of runtime code (0x00 = STOP):
// PUSH1 0x00  PUSH1 0x00  MSTORE8  PUSH1 0x01  PUSH1 0x00  RETURN
const init_code = [_]u8{ 0x60, 0x00, 0x60, 0x00, 0x53, 0x60, 0x01, 0x60, 0x00, 0xf3 };
 
// CREATE: address = keccak256(rlp([caller, nonce]))[12..]
const created = vm.call(.{ .create = .{
    .caller = alice,
    .value = 0,
    .init_code = &init_code,
    .gas = 200_000,
} });
 
// CREATE2: address = keccak256(0xff ++ caller ++ salt ++ keccak256(init_code))[12..]
const created2 = vm.call(.{ .create2 = .{
    .caller = alice,
    .value = 0,
    .init_code = &init_code,
    .salt = 0x1234,
    .gas = 200_000,
} });

You can precompute either address without executing bytecode:

const addr1 = try vm.computeCreateAddress(alice, nonce);
const addr2 = try vm.computeCreate2Address(alice, salt, &init_code);

Creation rules enforced by fork

RuleForkLimit / Effect
Max initcode length (EIP-3860)Shanghai+49,152 bytes (48 KB); +2 gas per 32-byte word (+6/word extra for CREATE2 keccak)
Max deployed runtime code (EIP-170)Spurious Dragon+24,576 bytes (24 KB)
Banned 0xEF prefix (EIP-3541)London+Runtime code starting with 0xEF fails creation and consumes gas
Collision check (EIP-684)AllFails if the target address already has a non-zero nonce or non-empty code. Storage is not checked (EIP-7610 is not implemented)
Code deposit out-of-gas (EIP-2)AllEntire creation fails with CodeStoreOutOfGas. The pre-Homestead rule that deploys empty code is not implemented

Nested call mechanics (inner_call)

When bytecode executes CALL, CALLCODE, DELEGATECALL, or STATICCALL, Frame delegates to Evm.inner_call():

  1. Depth limit: Call depth starts at 0 for the top-level frame and fails softly (pushes 0 on the caller stack, returns forwarded gas) once depth exceeds config.max_call_depth (1024).
  2. 63/64 rule (EIP-150, Tangerine Whistle+): A nested call receives at most available - available / 64 of the caller's remaining gas. At least 1/64 stays with the caller.
  3. 2,300 gas stipend: Value-bearing CALL adds a 2,300 gas stipend to the callee on top of forwarded gas so simple fallback receivers can run.
  4. Copy-on-write rollback: Before entering the child frame, Evm pushes snapshots for storage, balances, nonces, code, warm access sets, and log length. On REVERT or error, all of them unwind; on STOP/RETURN, snapshots commit into the parent frame.
  5. EIP-7702 delegation (Prague+): If the target account's code is the 23-byte delegation designator 0xEF0100 || delegate_address, vm.get_code() resolves and executes the delegate's bytecode while preserving the target account's address and storage.

SELFDESTRUCT and EIP-6780

  • Pre-Cancun: Transfers remaining balance to the beneficiary and schedules the contract for deletion at the end of the transaction. Pre-London, grants a 24,000 gas refund (removed in London by EIP-3529).
  • Cancun+ (EIP-6780): Always transfers the contract's balance to the beneficiary, but only deletes code, nonce, and storage if the contract was created in the same transaction (created_accounts.contains(address)). If the contract targets itself as beneficiary in a later transaction, its balance is preserved; if created in the same transaction and self-targeting, its balance is burned.