Skip to content

test_factory_in_caller_context()

Documentation for tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py::test_factory_in_caller_context@5c024cbb.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py::test_factory_in_caller_context --fork Amsterdam

Under DELEGATECALL or CALLCODE, the factory's bytecode runs in the caller's context, so CREATE2's deployer is the caller — not the factory. The contract is deployed at the address derived from the caller, and the factory-derived address is empty.

Source code in tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
@pytest.mark.parametrize("call_opcode", [Op.DELEGATECALL, Op.CALLCODE])
def test_factory_in_caller_context(
    state_test: StateTestFiller,
    pre: Alloc,
    call_opcode: Op,
) -> None:
    """
    Under `DELEGATECALL` or `CALLCODE`, the factory's bytecode runs in the
    caller's context, so `CREATE2`'s deployer is the caller — not the
    factory. The contract is deployed at the address derived from the
    caller, and the factory-derived address is empty.
    """
    salt = 0x44
    runtime_code = Op.STOP
    initcode = Initcode(deploy_code=runtime_code)
    factory_derived = compute_create2_address(FACTORY, salt, initcode)

    call_op = call_opcode(
        gas=Op.GAS,
        address=FACTORY,
        args_offset=0,
        args_size=Op.CALLDATASIZE,
        ret_offset=0x10C,
        ret_size=20,
    )

    storage = Storage()
    call_success_slot = storage.store_next(1, "delegated_call_success")
    derived_addr_slot = storage.store_next(0, "caller_derived_address")

    caller = pre.deploy_contract(
        Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
        + Op.SSTORE(call_success_slot, call_op)
        + Op.SSTORE(derived_addr_slot, Op.MLOAD(0x100))
        + Op.STOP,
    )
    caller_derived = compute_create2_address(caller, salt, initcode)
    storage[derived_addr_slot] = caller_derived

    state_test(
        pre=pre,
        tx=Transaction(
            sender=pre.fund_eoa(),
            to=caller,
            data=Hash(salt) + bytes(initcode),
        ),
        post={
            caller: Account(storage=storage),
            caller_derived: Account(nonce=1, code=bytes(runtime_code)),
            factory_derived: Account.NONEXISTENT,
        },
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.