alloy_zksync/contracts/l2/
contract_deployer.rs

1use alloy::{
2    primitives::{Address, B256, Bytes},
3    sol_types::SolCall,
4};
5
6/// The address of the contract deployer.
7pub const CONTRACT_DEPLOYER_ADDRESS: Address = Address::new([
8    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9    0x00, 0x00, 0x80, 0x06,
10]);
11
12alloy::sol! {
13    /// Function to create a contract.
14    function create(bytes32 salt, bytes32 bytecodeHash, bytes memory constructorInput);
15
16    /// Function to create a contract using create2.
17    function create2(bytes32 salt, bytes32 bytecodeHash, bytes memory constructorInput);
18
19    /// Event emitted when a contract is deployed.
20    event ContractDeployed(
21        address indexed deployerAddress,
22        bytes32 indexed bytecodeHash,
23        address indexed contractAddress
24    );
25}
26
27/// Encodes the calldata for creating a contract.
28///
29/// This function encodes the bytecode hash and constructor input into a `Bytes` object
30/// for use in contract creation operations using ContractDeployer contract.
31///
32/// # Arguments
33///
34/// * `bytecode_hash` - The hash of the contract bytecode.
35/// * `constructor_input` - The constructor input data.
36///
37/// # Returns
38///
39/// The encoded calldata as `Bytes`.
40pub(crate) fn encode_create_calldata(bytecode_hash: B256, constructor_input: Bytes) -> Bytes {
41    // The salt parameter is required as per signature but is not used during create
42    // See: https://github.com/matter-labs/era-contracts/blob/main/system-contracts/contracts/interfaces/IContractDeployer.sol#L65
43    let call = createCall {
44        salt: Default::default(),
45        bytecodeHash: bytecode_hash,
46        constructorInput: constructor_input,
47    };
48
49    call.abi_encode().into()
50}
51
52/// Encodes the calldata for creating a contract using create2.
53///
54/// This function encodes the salt, bytecode hash, and constructor input into a `Bytes` object
55/// for use in contract creation operations using ContractDeployer contract.
56///
57/// # Arguments
58///
59/// * `salt` - The salt value.
60/// * `bytecode_hash` - The hash of the contract bytecode.
61/// * `constructor_input` - The constructor input data.
62///
63/// # Returns
64///
65/// The encoded calldata as `Bytes`.
66pub(crate) fn encode_create2_calldata(
67    salt: B256,
68    bytecode_hash: B256,
69    constructor_input: Bytes,
70) -> Bytes {
71    let call = createCall {
72        salt,
73        bytecodeHash: bytecode_hash,
74        constructorInput: constructor_input,
75    };
76
77    call.abi_encode().into()
78}