Skip to content

test_factory_deploys_contract()

Documentation for tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py::test_factory_deploys_contract@2119b382.

Generate fixtures for these test cases for Amsterdam with:

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

Calling the factory with salt || initcode deploys a contract at the expected CREATE2 address and returns that address. When the call forwards a non-zero value, the deployed contract receives that balance.

Source code in tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
@pytest.mark.parametrize(
    "forwarded_value",
    [
        pytest.param(0, id="no_value"),
        pytest.param(1, id="with_value"),
    ],
)
def test_factory_deploys_contract(
    state_test: StateTestFiller,
    pre: Alloc,
    forwarded_value: int,
) -> None:
    """
    Calling the factory with `salt || initcode` deploys a contract at the
    expected `CREATE2` address and returns that address. When the call
    forwards a non-zero value, the deployed contract receives that
    balance.
    """
    salt = 0x42
    runtime_code = Op.PUSH1(0x01) + Op.PUSH1(0x00) + Op.RETURN
    initcode = Initcode(deploy_code=runtime_code)
    expected_address = compute_create2_address(FACTORY, salt, initcode)

    storage = Storage()
    caller = pre.deploy_contract(
        Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
        + Op.SSTORE(
            storage.store_next(1, "factory_call_success"),
            Op.CALL(
                gas=Op.GAS,
                address=FACTORY,
                value=forwarded_value,
                args_offset=0,
                args_size=Op.CALLDATASIZE,
                ret_offset=12,
                ret_size=20,
            ),
        )
        + Op.SSTORE(
            storage.store_next(expected_address, "returned_address"),
            Op.MLOAD(0),
        )
        + Op.STOP,
        balance=forwarded_value,
    )

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

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.