-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtomicDeployment.s.sol
More file actions
69 lines (57 loc) · 2.44 KB
/
AtomicDeployment.s.sol
File metadata and controls
69 lines (57 loc) · 2.44 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {DeployHelper} from "../../src/DeployHelper.sol";
import {Versionable} from "../../src/Versionable.sol";
import {Ownable} from "solady/auth/Ownable.sol";
/**
* @title AtomicContract
* @notice Example contract using post-deploy initialization (not constructor-based)
* @dev Owner is set via initializeOwner() after deployment, NOT in the constructor.
* This pattern requires atomic deploy+init to prevent front-running.
*/
contract AtomicContract is Versionable, Ownable {
uint256 public value;
constructor(string memory evmSuffix_) Versionable(evmSuffix_) {}
function initializeOwner(address _owner) external {
require(owner() == address(0), "Already initialized");
_initializeOwner(_owner);
}
function setValue(uint256 _value) external onlyOwner {
value = _value;
}
function _baseVersion() internal pure override returns (string memory) {
return "1.0.0-AtomicContract";
}
}
/**
* @title AtomicDeployment
* @notice Example deployment script demonstrating atomic deploy+init
* @dev This demonstrates:
* - Atomic deployment and initialization in a single transaction
* - Front-running prevention via CreateX's deployCreate3AndInit
* - Overriding _getPostDeployInitData() to enable atomic init
* - Overriding _getDeployValues() to send ETH during deployment
*
* Without atomic init, there is a window between deployment and the
* initializeOwner() call where an attacker could front-run and claim
* ownership. Atomic init closes this window by executing both in one tx.
*/
contract AtomicDeployment is DeployHelper {
function setUp() public override {
address broadcaster = vm.envOr("DEPLOYER", msg.sender);
_setUp("examples", broadcaster);
}
/// @notice Atomic init: calls initializeOwner in the same tx as deployment
function _getPostDeployInitData() internal virtual override returns (bytes memory) {
return abi.encodeWithSignature("initializeOwner(address)", _deployer);
}
function run() public {
vm.startBroadcast(_deployer);
_assertBroadcastSenderMatchesDeployer();
bytes memory creationCode = abi.encodePacked(type(AtomicContract).creationCode, abi.encode(_getEvmSuffix()));
address deployed = deploy(creationCode);
_checkChainAndSetOwner(deployed);
_afterAll();
vm.stopBroadcast();
}
}