diff --git a/.gas-snapshot b/.gas-snapshot index 68e4054a..c4aada7b 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -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) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b2b85f9..3d1dea68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/README.md b/README.md index d60b4a34..e669758f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/snekmate/utils/Create2Address.vy b/src/snekmate/utils/Create2Address.vy index 0e9bdf18..2fb4cf3c 100644 --- a/src/snekmate/utils/Create2Address.vy +++ b/src/snekmate/utils/Create2Address.vy @@ -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. @@ -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. @@ -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) diff --git a/src/snekmate/utils/mocks/Create2AddressMock.vy b/src/snekmate/utils/mocks/Create2AddressMock.vy new file mode 100644 index 00000000..8eff2e8b --- /dev/null +++ b/src/snekmate/utils/mocks/Create2AddressMock.vy @@ -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) diff --git a/test/utils/Create2Address.t.sol b/test/utils/Create2Address.t.sol index c9b7dbdd..d51a05d0 100644 --- a/test/utils/Create2Address.t.sol +++ b/test/utils/Create2Address.t.sol @@ -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);