Skip to content

test_initcode_calls_with_value()

Documentation for tests/amsterdam/eip7708_eth_transfer_logs/test_transfer_logs.py::test_initcode_calls_with_value@87aba1a3.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7708_eth_transfer_logs/test_transfer_logs.py::test_initcode_calls_with_value --fork Amsterdam

Test that CALL with value during initcode emits correct log.

Initcode performs CALL with value before returning deployed code. Log should show the being-created contract as sender.

Source code in tests/amsterdam/eip7708_eth_transfer_logs/test_transfer_logs.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
@pytest.mark.with_all_create_opcodes
def test_initcode_calls_with_value(
    state_test: StateTestFiller,
    env: Environment,
    pre: Alloc,
    sender: EOA,
    create_opcode: Op,
) -> None:
    """
    Test that CALL with value during initcode emits correct log.

    Initcode performs CALL with value before returning deployed code.
    Log should show the being-created contract as sender.
    """
    recipient = pre.deploy_contract(Op.STOP)

    # Initcode: CALL recipient with value, then RETURN empty code
    initcode = Op.CALL(address=recipient, value=1) + Op.RETURN(0, 0)
    initcode_bytes = bytes(initcode)

    # Use Initcode helper or direct memory setup for longer initcode
    if create_opcode == Op.CREATE:
        # Store initcode in memory using code copy approach
        factory_code = (
            Op.CODECOPY(
                0,
                Op.SUB(Op.CODESIZE, len(initcode_bytes)),
                len(initcode_bytes),
            )
            + Op.CREATE(value=1, offset=0, size=len(initcode_bytes))
            + Op.STOP
        ) + initcode
    else:
        factory_code = (
            Op.CODECOPY(
                0,
                Op.SUB(Op.CODESIZE, len(initcode_bytes)),
                len(initcode_bytes),
            )
            + Op.CREATE2(value=1, offset=0, size=len(initcode_bytes), salt=0)
            + Op.STOP
        ) + initcode

    factory = pre.deploy_contract(factory_code, balance=2)

    # Compute created address
    if create_opcode == Op.CREATE:
        created_address = compute_create_address(address=factory, nonce=1)
    else:
        created_address = compute_create2_address(
            address=factory, salt=0, initcode=initcode_bytes
        )

    tx = Transaction(
        sender=sender,
        to=factory,
        value=0,
        expected_receipt=TransactionReceipt(
            logs=[
                # CREATE transfers value to new contract
                transfer_log(factory, created_address, 1),
                # Initcode CALLs recipient with value
                transfer_log(created_address, recipient, 1),
            ]
        ),
    )

    post = {recipient: Account(balance=1)}
    state_test(env=env, pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.