Skip to content

test_factory_receives_balance_via_selfdestruct()

Documentation for tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py::test_factory_receives_balance_via_selfdestruct@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

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

SELFDESTRUCT to the factory transfers the originator's balance to the factory address. The factory's other state is untouched: same nonce, same code. Calling the factory after the transfer still works.

Tests that the factory address has no special handling under SELFDESTRUCT — it behaves like any other contract beneficiary.

Source code in tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
def test_factory_receives_balance_via_selfdestruct(
    state_test: StateTestFiller,
    pre: Alloc,
) -> None:
    """
    `SELFDESTRUCT` to the factory transfers the originator's balance to
    the factory address. The factory's other state is untouched: same
    nonce, same code. Calling the factory after the transfer still works.

    Tests that the factory address has no special handling under
    `SELFDESTRUCT` — it behaves like any other contract beneficiary.
    """
    forwarded_value = 1

    sd_actor = pre.deploy_contract(
        Op.SELFDESTRUCT(FACTORY),
        balance=forwarded_value,
    )

    salt = 0x88
    runtime_code = Op.STOP
    initcode = Initcode(deploy_code=runtime_code)
    expected_address = compute_create2_address(FACTORY, salt, initcode)

    storage = Storage()
    caller = pre.deploy_contract(
        Op.POP(Op.CALL(gas=Op.GAS, address=sd_actor))
        + Op.SSTORE(
            storage.store_next(forwarded_value, "factory_balance_after_sd"),
            Op.BALANCE(FACTORY),
        )
        + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
        + Op.SSTORE(
            storage.store_next(1, "factory_call_success"),
            Op.CALL(
                gas=Op.GAS,
                address=FACTORY,
                value=0,
                args_offset=0,
                args_size=Op.CALLDATASIZE,
                ret_offset=0x100,
                ret_size=32,
            ),
        )
        + Op.STOP,
    )

    state_test(
        pre=pre,
        tx=Transaction(
            sender=pre.fund_eoa(),
            to=caller,
            data=Hash(salt) + bytes(initcode),
        ),
        post={
            caller: Account(storage=storage),
            FACTORY: Account(
                nonce=2,
                balance=forwarded_value,
                code=Spec.FACTORY_BYTECODE,
            ),
            expected_address: Account(
                nonce=1,
                code=bytes(runtime_code),
            ),
        },
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.