Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Make Create2Address Module-Friendly #225

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ BatchDistributorTest:testDistributeTokenRevertWithInsufficientBalance() (gas: 60
BatchDistributorTest:testFuzzDistributeEtherMultipleAddressesSuccess(((address,uint256)[]),uint256) (runs: 257, μ: 1785320, ~: 1662774)
BatchDistributorTest:testFuzzDistributeTokenMultipleAddressesSuccess(((address,uint256)[]),address,uint256) (runs: 257, μ: 1346331, ~: 1307140)
Create2AddressTest:testComputeAddress() (gas: 550587)
Create2AddressTest:testComputeAddressSelf() (gas: 559245)
Create2AddressTest:testComputeAddressSelf() (gas: 559319)
Create2AddressTest:testFuzzComputeAddress(bytes32,address) (runs: 257, μ: 551182, ~: 551182)
Create2AddressTest:testFuzzComputeAddressSelf(bytes32) (runs: 257, μ: 559266, ~: 559266)
Create2AddressTest:testFuzzComputeAddressSelf(bytes32) (runs: 257, μ: 559340, ~: 559340)
CreateAddressTest:testComputeAddressNonce0x00() (gas: 16545)
CreateAddressTest:testComputeAddressNonce0x7f() (gas: 535262)
CreateAddressTest:testComputeAddressNonceUint16() (gas: 535314)
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [`Base64`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/utils/Base64.vy): Make `Base64` module-friendly. ([#222](https://github.com/pcaversaccio/snekmate/pull/222))
- [`BatchDistributor`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/utils/BatchDistributor.vy): Make `BatchDistributor` module-friendly. ([#223](https://github.com/pcaversaccio/snekmate/pull/223))
- [`CreateAddress`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/utils/CreateAddress.vy): Make `CreateAddress` module-friendly. ([#224](https://github.com/pcaversaccio/snekmate/pull/224))
- [`Create2Address`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/src/snekmate/utils/Create2Address.vy): Make `Create2Address` module-friendly. ([#225](https://github.com/pcaversaccio/snekmate/pull/225))
- **Vyper Contract Deployer**
- [`VyperDeployer`](https://github.com/pcaversaccio/snekmate/blob/v0.1.0/lib/utils/VyperDeployer.sol): Improve error message in the event of a Vyper compilation error. ([#219](https://github.com/pcaversaccio/snekmate/pull/219))

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ src
└── mocks
├── Base64Mock — "Base64 Module Reference Implementation"
├── BatchDistributorMock — "BatchDistributor Module Reference Implementation"
└── CreateAddressMock — "CreateAddress Module Reference Implementation"
├── CreateAddressMock — "CreateAddress Module Reference Implementation"
└── Create2AddressMock — "Create2Address Module Reference Implementation"
```

## 🎛 Installation
Expand Down
26 changes: 4 additions & 22 deletions src/snekmate/utils/Create2Address.vy
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def __init__():
pass


@external
@internal
@view
def compute_address_self(salt: bytes32, bytecode_hash: bytes32) -> address:
def _compute_address_self(salt: bytes32, bytecode_hash: bytes32) -> address:
"""
@dev Returns the address where a contract will be stored if
deployed via this contract using the `CREATE2` opcode.
Expand All @@ -46,9 +46,9 @@ def compute_address_self(salt: bytes32, bytecode_hash: bytes32) -> address:
return self._compute_address(salt, bytecode_hash, self)


@external
@internal
@pure
def compute_address(salt: bytes32, bytecode_hash: bytes32, deployer: address) -> address:
def _compute_address(salt: bytes32, bytecode_hash: bytes32, deployer: address) -> address:
"""
@dev Returns the address where a contract will be stored if
deployed via `deployer` using the `CREATE2` opcode.
Expand All @@ -61,24 +61,6 @@ def compute_address(salt: bytes32, bytecode_hash: bytes32, deployer: address) ->
@param deployer The 20-byte deployer address.
@return address The 20-byte address where a contract will be stored.
"""
return self._compute_address(salt, bytecode_hash, deployer)


@internal
@pure
def _compute_address(salt: bytes32, bytecode_hash: bytes32, deployer: address) -> address:
"""
@dev An `internal` helper function that returns the address
where a contract will be stored if deployed via `deployer`
using the `CREATE2` opcode. Any change in the `bytecode_hash`
or `salt` values will result in a new destination address.
@param salt The 32-byte random value used to create the contract
address.
@param bytecode_hash The 32-byte bytecode digest of the contract
creation bytecode.
@param deployer The 20-byte deployer address.
@return address The 20-byte address where a contract will be stored.
"""
data: bytes32 = keccak256(concat(_COLLISION_OFFSET, convert(deployer, bytes20), salt, bytecode_hash))
return self._convert_keccak256_2_address(data)

Expand Down
58 changes: 58 additions & 0 deletions src/snekmate/utils/mocks/Create2AddressMock.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# pragma version ~=0.4.0b5
"""
@title Create2Address Module Reference Implementation
@custom:contract-name Create2AddressMock
@license GNU Affero General Public License v3.0 only
@author pcaversaccio
"""


# @dev We import and initialise the `Create2Address` module.
from .. import Create2Address as c2a
initializes: c2a


@deploy
@payable
def __init__():
"""
@dev To omit the opcodes for checking the `msg.value`
in the creation-time EVM bytecode, the constructor
is declared as `payable`.
"""
c2a.__init__()


@external
@view
def compute_address_self(salt: bytes32, bytecode_hash: bytes32) -> address:
"""
@dev Returns the address where a contract will be stored if
deployed via this contract using the `CREATE2` opcode.
Any change in the `bytecode_hash` or `salt` values will
result in a new destination address.
@param salt The 32-byte random value used to create the contract
address.
@param bytecode_hash The 32-byte bytecode digest of the contract
creation bytecode.
@return address The 20-byte address where a contract will be stored.
"""
return c2a._compute_address_self(salt, bytecode_hash)


@external
@pure
def compute_address(salt: bytes32, bytecode_hash: bytes32, deployer: address) -> address:
"""
@dev Returns the address where a contract will be stored if
deployed via `deployer` using the `CREATE2` opcode.
Any change in the `bytecode_hash` or `salt` values will
result in a new destination address.
@param salt The 32-byte random value used to create the contract
address.
@param bytecode_hash The 32-byte bytecode digest of the contract
creation bytecode.
@param deployer The 20-byte deployer address.
@return address The 20-byte address where a contract will be stored.
"""
return c2a._compute_address(salt, bytecode_hash, deployer)
4 changes: 2 additions & 2 deletions test/utils/Create2Address.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ contract Create2AddressTest is Test {
function setUp() public {
create2Address = ICreate2Address(
vyperDeployer.deployContract(
"src/snekmate/utils/",
"Create2Address"
"src/snekmate/utils/mocks/",
"Create2AddressMock"
)
);
create2AddressAddr = address(create2Address);
Expand Down