Skip to content

Instantly share code, notes, and snippets.

@0xPhaze
Created February 6, 2025 15:56
Show Gist options
  • Save 0xPhaze/26027125a03ca674bb0199e91c3ce660 to your computer and use it in GitHub Desktop.
Save 0xPhaze/26027125a03ca674bb0199e91c3ce660 to your computer and use it in GitHub Desktop.
function create2Address(address deployer, bytes32 salt, bytes32 codeHash) pure returns (address) {
return address(uint160(uint256(keccak256(abi.encodePacked(hex"ff", deployer, salt, codeHash)))));
}
contract Factory {
address public caller;
function fetch(bytes32 salt) public {
caller = msg.sender;
new Funder{salt: salt}();
caller = address(0);
}
function getFunderAddress(bytes32 salt) public view returns (address) {
bytes32 codeHash = keccak256(abi.encodePacked(type(Funder).creationCode));
return create2Address(address(this), salt, codeHash);
}
}
contract Funder {
constructor() {
(bool ok,) = Factory(msg.sender).caller().call{value: 1 ether}("");
require(ok);
selfdestruct(payable(0));
}
}
contract ABCTest is Test {
Factory factory = new Factory();
bytes32 salt;
address funder = factory.getFunderAddress(salt);
constructor() {
deal(funder, 2 ether);
address(0).call{value: address(this).balance}("");
}
function test_xxx() public {
factory.fetch(salt);
}
receive() external payable {
if (address(this).balance < 2 ether) {
callFactory();
}
}
function callFactory() public {
factory.fetch(bytes32(0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment