-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from ethereum_rpc import Address, keccak | ||
|
||
|
||
def get_create2_address(deployer: Address, init_code: bytes, salt: int) -> Address: | ||
""" | ||
Returns the deterministic deployed contract address as produced by ``CREATE2`` opcode. | ||
Here `deployer` is the contract address invoking ``CREATE2`` | ||
(**not** the transaction initiator), | ||
``init_code`` is the deployment code (see :py:attr:`~pons.BoundConstructorCall.data_bytes`), | ||
and ``salt`` is an integer up to 256 bits in length. | ||
""" | ||
salt_bytes = salt.to_bytes(32, byteorder="big") | ||
contract_address = keccak(b"\xff" + bytes(deployer) + salt_bytes + keccak(init_code))[-20:] | ||
return Address(contract_address) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
pragma solidity >=0.8.0 <0.9.0; | ||
|
||
|
||
contract ToDeploy { | ||
uint256 public state; | ||
|
||
constructor(uint256 _state) { | ||
state = _state; | ||
} | ||
|
||
function getState() public view returns (uint256) { | ||
return state; | ||
} | ||
} | ||
|
||
|
||
contract Create2Deployer { | ||
event Deployed( | ||
address deployedAddress | ||
); | ||
|
||
function deploy(bytes memory bytecode, uint256 _salt) public payable { | ||
address addr; | ||
bool success = true; | ||
|
||
assembly { | ||
addr := create2( | ||
callvalue(), | ||
add(bytecode, 0x20), // Skip the first 32 bytes, which is the size of `bytecode` | ||
mload(bytecode), // Load the size of code contained in the first 32 bytes | ||
_salt | ||
) | ||
|
||
if iszero(extcodesize(addr)) { | ||
success := false | ||
} | ||
} | ||
|
||
if (!success) { | ||
revert("Failed to deploy the contract"); | ||
} | ||
|
||
emit Deployed(addr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import secrets | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from pons import EVMVersion, compile_contract_file, get_create2_address | ||
|
||
|
||
@pytest.fixture | ||
def compiled_contracts(): | ||
path = Path(__file__).resolve().parent / "TestUtils.sol" | ||
return compile_contract_file(path, evm_version=EVMVersion.CANCUN) | ||
|
||
|
||
async def test_create2(session, root_signer, compiled_contracts): | ||
compiled_deployer = compiled_contracts["Create2Deployer"] | ||
compiled_to_deploy = compiled_contracts["ToDeploy"] | ||
|
||
deployer = await session.deploy(root_signer, compiled_deployer.constructor()) | ||
|
||
salt = secrets.randbelow(1 << 256) | ||
to_deploy = compiled_to_deploy.constructor(123) | ||
events = await session.transact( | ||
root_signer, | ||
deployer.method.deploy(to_deploy.data_bytes, salt), | ||
return_events=[deployer.event.Deployed], | ||
) | ||
assert len(events[deployer.event.Deployed]) == 1 | ||
assert events[deployer.event.Deployed][0] == dict( | ||
deployedAddress=get_create2_address(deployer.address, to_deploy.data_bytes, salt) | ||
) |