Verified contractExactMIT
Hook2Factory
Address0x013a18b67438da2ae3a2bed4f2129d1e2db5445c
- Balance
0 ETH
- Transactions
5
- Compiler
solc 0.8.30
- Same code
Only this contract
Details
- Compiler
- solc 0.8.30+commit.73712a01
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,071,804
- Creation transaction
- 0x203a0c6cd1…91d1ef751a
BBBBBBBBBBBBBBBB/Hook2Factory.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/* ─────────────────────────────────────────────────────────────
Hook2Factory.sol
通用 CREATE2 工厂 — 部署任何 Uniswap V4 Hook 合约到挖好盐的地址
为什么需要这个:
V4 协议规定 hook 合约地址的最低 14 bits = 该 hook 启用的 permission flags
普通 CREATE 部署给随机地址, 不会匹配
CREATE2 + 挖好的 salt 可以预测地址, 保证 bitmap 正确
设计:
这个 Factory 是「通用」的, 部署一次就能用一辈子
之后部署任何 V4 hook 合约都用同一个 Factory
deploy(salt, initcode) 接受任意 hook 的完整 initcode (bytecode + 编码后的构造参数)
流程:
1. 一次性部署这个 Factory (这次)
2. 之后对于每个新 hook 项目:
a. 编译合约, 拿 bytecode
b. 跑挖盐脚本得到 salt
c. 调 deploy(salt, initcode)
d. 拿到的 hook 地址末位 = bitmap, 自动匹配
注意:
- 这个 Factory 没有 owner, 任何人都能用它部署东西
- 但不影响安全性 — salt 决定地址, 别人挖的 salt 给别人的地址
- 你自己的项目只要用你挖的 salt, 别人无法抢
───────────────────────────────────────────────────────────── */
contract Hook2Factory {
event Deployed(address indexed deployed, address indexed deployer, bytes32 salt);
error DeployFailed();
error AddressMismatch(address expected, address actual);
/// @notice Deploy any contract via CREATE2.
/// @param salt Pre-mined salt that produces the desired address suffix.
/// @param initcode Full creation bytecode + ABI-encoded constructor args.
/// @param expectedHook The address you expect to get. Reverts if mismatch.
/// @return deployed The deployed contract address.
function deploy(
bytes32 salt,
bytes memory initcode,
address expectedHook
) external returns (address deployed) {
assembly {
deployed := create2(0, add(initcode, 0x20), mload(initcode), salt)
}
if (deployed == address(0)) revert DeployFailed();
if (expectedHook != address(0) && deployed != expectedHook) {
revert AddressMismatch(expectedHook, deployed);
}
emit Deployed(deployed, msg.sender, salt);
}
/// @notice Same as deploy(), but skips the address-match check.
/// @dev Useful if you trust your salt-mining and just want the cheapest path.
function deployUnchecked(
bytes32 salt,
bytes memory initcode
) external returns (address deployed) {
assembly {
deployed := create2(0, add(initcode, 0x20), mload(initcode), salt)
}
if (deployed == address(0)) revert DeployFailed();
emit Deployed(deployed, msg.sender, salt);
}
/// @notice Pure view to compute what address a salt+initcode would produce.
/// @dev Use this off-chain or in scripts to verify before deploying.
function computeAddress(
bytes32 salt,
bytes memory initcode
) external view returns (address) {
bytes32 hash = keccak256(
abi.encodePacked(
bytes1(0xff),
address(this),
salt,
keccak256(initcode)
)
);
return address(uint160(uint256(hash)));
}
/// @notice Same as computeAddress but takes pre-hashed initcode (saves gas/time
/// when the initcode is very large).
function computeAddressFromHash(
bytes32 salt,
bytes32 initcodeHash
) external view returns (address) {
bytes32 hash = keccak256(
abi.encodePacked(
bytes1(0xff),
address(this),
salt,
initcodeHash
)
);
return address(uint160(uint256(hash)));
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.