1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! CREATE3 deployment-address derivation.
//!
//! CREATE3 makes a contract's deployed address depend only on the deploying
//! factory and a salt, independent of the contract's init code. This module
//! reproduces that derivation off-chain: it computes the CREATE2 proxy address
//! the factory would create from `(deployer, salt)`, then the CREATE address
//! the proxy deploys to. This lets callers predict an address before a
//! transaction is sent.
use ;
/// Address of the widely deployed `CREATE3Factory` (the ZeframLou / Solmate-style
/// implementation, deterministically deployed cross-chain — e.g. by LiFi).
///
/// This is the canonical cross-chain address at which this factory has been
/// deployed on many EVM networks. Its derivation salts as
/// `keccak256(abi.encodePacked(deployer, salt))` over the standard Solady/Solmate
/// CREATE3 proxy init code whose hash is `CREATE3_PROXY_INITCODE_HASH`, which is
/// what [`derive_universal_create3_address`] reproduces.
///
/// **This is not pcaversaccio's CreateX** (`0xba5Ed0...28ba5Ed`). CreateX applies
/// a guarded-salt transformation (permissioned-deploy / cross-chain-redeploy
/// protection, chain-ID mixing) that differs from the plain
/// `keccak256(deployer, salt)` used here, so this module does **not** predict
/// CreateX deployment addresses.
///
/// Callers must verify the factory is actually deployed at this address on
/// their target chain before relying on a derived address: if the factory is
/// absent (or a chain hosts a different factory implementation), the derived
/// address will not correspond to any real deployment.
pub const UNIVERSAL_CREATE3_FACTORY: Address = address!;
// Standard Solady/Solmate CREATE3 proxy initcode used by this factory.
// keccak256(0x67363d3d37363d34f03d5260086018f3)
const CREATE3_PROXY_INITCODE_HASH: B256 =
b256!;
/// Derive CREATE3 deployment address for the universal factory implementation.
///
/// CREATE3 deploys in two hops: the factory first `CREATE2`-deploys a tiny
/// fixed proxy, then that proxy `CREATE`s the actual contract as its first
/// (nonce-1) deployment. Because both hops use only the factory, the salt, and
/// a fixed proxy init code, the final address depends solely on `factory`,
/// `deployer`, and `salt` — it is **independent of the deployed contract's
/// bytecode**. Two different contracts deployed with the same inputs land at
/// the same address.
///
/// Formula:
/// 1. `mixedSalt = keccak256(abi.encodePacked(deployer, salt))` — binds the
/// salt to the logical deployer.
/// 2. `proxy = create2(factory, mixedSalt, CREATE3_PROXY_INITCODE_HASH)` —
/// the CREATE2 address of the proxy. `CREATE3_PROXY_INITCODE_HASH` is the
/// keccak256 of the fixed proxy init code, so the proxy address is fully
/// determined by the factory and mixed salt.
/// 3. `deployed = address(keccak256(rlp([proxy, 1])))` — the CREATE address of
/// the proxy's first deployment (nonce 1). The RLP framing bytes encode the
/// short list `[proxy, 1]`: `0xd6` is the RLP list header for the 22-byte
/// payload that follows, `0x94` introduces the 20-byte `proxy` address, and
/// `0x01` is the RLP encoding of the proxy's nonce (1), since a fresh
/// contract account's first `CREATE` uses nonce 1.
///
/// The address is returned as the low 20 bytes of each keccak256 hash, matching
/// the EVM's address-from-hash convention.
///
/// `factory` lets you derive against a non-canonical factory deployment; for
/// the canonical address use [`derive_universal_create3_address`].
///
/// ```
/// use evm_fork_cache::create3::derive_create3_address;
/// use alloy_primitives::{Address, B256, address, b256};
///
/// let factory: Address = address!("93FEC2C00BfE902F733B57c5a6CeeD7CD1384AE1");
/// let deployer: Address = address!("00000000000000000000000000000000000000aa");
/// let salt: B256 =
/// b256!("1111111111111111111111111111111111111111111111111111111111111111");
///
/// // The derivation is a pure function of (factory, deployer, salt): identical
/// // inputs always yield the same address.
/// let a = derive_create3_address(factory, deployer, salt);
/// let b = derive_create3_address(factory, deployer, salt);
/// assert_eq!(a, b);
///
/// // Changing the salt changes the derived address.
/// let other_salt: B256 =
/// b256!("2222222222222222222222222222222222222222222222222222222222222222");
/// assert_ne!(a, derive_create3_address(factory, deployer, other_salt));
/// ```
/// Derive CREATE3 deployment address via the universal factory.
///
/// Convenience wrapper around [`derive_create3_address`] that uses
/// [`UNIVERSAL_CREATE3_FACTORY`] as the factory. As with the general form, the
/// result depends only on `deployer` and `salt`, not on the deployed bytecode,
/// and is only meaningful on chains where that factory is actually deployed.
///
/// ```
/// use evm_fork_cache::create3::derive_universal_create3_address;
/// use alloy_primitives::{Address, B256, address, b256};
///
/// let deployer: Address = address!("00000000000000000000000000000000000000aa");
/// let salt: B256 =
/// b256!("1111111111111111111111111111111111111111111111111111111111111111");
///
/// // Deterministic: the same (deployer, salt) always derive the same address.
/// assert_eq!(
/// derive_universal_create3_address(deployer, salt),
/// derive_universal_create3_address(deployer, salt),
/// );
/// ```