diff --git a/tests/Base.t.sol b/tests/Base.t.sol index 146b3755a..b2b08826e 100644 --- a/tests/Base.t.sol +++ b/tests/Base.t.sol @@ -101,6 +101,7 @@ import { // test import {Constants} from 'tests/Constants.sol'; import {DeployUtils} from 'tests/DeployUtils.sol'; +import {Create2Utils} from 'tests/Create2Utils.sol'; import {Utils} from 'tests/Utils.sol'; // mocks diff --git a/tests/Create2Utils.sol b/tests/Create2Utils.sol index 383578260..14934ea46 100644 --- a/tests/Create2Utils.sol +++ b/tests/Create2Utils.sol @@ -7,6 +7,7 @@ import {Vm} from 'forge-std/Vm.sol'; library Create2Utils { error NoCreate2Factory(); error Create2DeploymentFailed(); + error Create2ContractAlreadyDeployed(); // https://github.com/safe-global/safe-singleton-factory address public constant CREATE2_FACTORY = 0x914d7Fec6aaC8cd542e72Bca78B30650d45643d7; @@ -26,19 +27,16 @@ library Create2Utils { require(_isContractDeployed(CREATE2_FACTORY), NoCreate2Factory()); address computed = computeCreate2Address(salt, bytecode); + require(!_isContractDeployed(computed), Create2ContractAlreadyDeployed()); - if (_isContractDeployed(computed)) { - return computed; - } else { - bytes memory creationBytecode = abi.encodePacked(salt, bytecode); - bytes memory returnData; - (, returnData) = CREATE2_FACTORY.call(creationBytecode); + bytes memory creationBytecode = abi.encodePacked(salt, bytecode); + bytes memory returnData; + (, returnData) = CREATE2_FACTORY.call(creationBytecode); - address deployedAt = address(uint160(bytes20(returnData))); - require(deployedAt == computed, Create2DeploymentFailed()); + address deployedAt = address(uint160(bytes20(returnData))); + require(deployedAt == computed, Create2DeploymentFailed()); - return deployedAt; - } + return deployedAt; } function _isContractDeployed(address instance) internal view returns (bool) { diff --git a/tests/DeployUtils.sol b/tests/DeployUtils.sol index e928effd6..62de40b48 100644 --- a/tests/DeployUtils.sol +++ b/tests/DeployUtils.sol @@ -16,7 +16,8 @@ library DeployUtils { address oracle, uint16 maxUserReservesLimit ) internal returns (ISpokeInstance) { - return deploySpokeImplementation(oracle, maxUserReservesLimit, ''); + bytes32 salt = keccak256(abi.encode(oracle, maxUserReservesLimit)); + return deploySpokeImplementation(oracle, maxUserReservesLimit, salt); } function deploySpokeImplementation( @@ -36,24 +37,28 @@ library DeployUtils { uint16 maxUserReservesLimit, address proxyAdminOwner, bytes memory initData + ) internal returns (ISpoke) { + bytes32 salt = keccak256(abi.encode(oracle, maxUserReservesLimit)); + return deploySpoke(oracle, maxUserReservesLimit, proxyAdminOwner, salt, initData); + } + + function deploySpoke( + address oracle, + uint16 maxUserReservesLimit, + address proxyAdminOwner, + bytes32 salt, + bytes memory initData ) internal returns (ISpoke) { return ISpoke( proxify( - address(deploySpokeImplementation(oracle, maxUserReservesLimit, '')), + address(deploySpokeImplementation(oracle, maxUserReservesLimit, salt)), proxyAdminOwner, initData ) ); } - function getDeterministicSpokeInstanceAddress( - address oracle, - uint16 maxUserReservesLimit - ) internal returns (address) { - return getDeterministicSpokeInstanceAddress(oracle, maxUserReservesLimit, ''); - } - function getDeterministicSpokeInstanceAddress( address oracle, uint16 maxUserReservesLimit, @@ -66,7 +71,7 @@ library DeployUtils { } function deployHub(address authority) internal returns (IHub) { - return deployHub(authority, ''); + return IHub(vm.deployCode('src/hub/Hub.sol:Hub', abi.encode(authority))); } function deployHub(address authority, bytes32 salt) internal returns (IHub hub) { @@ -74,10 +79,6 @@ library DeployUtils { return IHub(Create2Utils.create2Deploy(salt, _getHubInitCode(authority))); } - function getDeterministicHubAddress(address authority) internal returns (address) { - return getDeterministicHubAddress(authority, ''); - } - function getDeterministicHubAddress(address authority, bytes32 salt) internal returns (address) { bytes32 initCodeHash = keccak256(_getHubInitCode(authority)); diff --git a/tests/mocks/DeployWrapper.sol b/tests/mocks/DeployWrapper.sol index 49b3b673b..d48001fe6 100644 --- a/tests/mocks/DeployWrapper.sol +++ b/tests/mocks/DeployWrapper.sol @@ -9,7 +9,15 @@ contract DeployWrapper { address oracle, uint16 maxUserReservesLimit ) external returns (address) { - return address(DeployUtils.deploySpokeImplementation(oracle, maxUserReservesLimit, '')); + return address(DeployUtils.deploySpokeImplementation(oracle, maxUserReservesLimit)); + } + + function deploySpokeImplementation( + address oracle, + uint16 maxUserReservesLimit, + bytes32 salt + ) external returns (address) { + return address(DeployUtils.deploySpokeImplementation(oracle, maxUserReservesLimit, salt)); } function deploySpoke( @@ -22,7 +30,24 @@ contract DeployWrapper { address(DeployUtils.deploySpoke(oracle, maxUserReservesLimit, proxyAdminOwner, initData)); } + function deploySpoke( + address oracle, + uint16 maxUserReservesLimit, + address proxyAdminOwner, + bytes32 salt, + bytes calldata initData + ) external returns (address) { + return + address( + DeployUtils.deploySpoke(oracle, maxUserReservesLimit, proxyAdminOwner, salt, initData) + ); + } + function deployHub(address authority) external returns (address) { return address(DeployUtils.deployHub(authority)); } + + function deployHub(address authority, bytes32 salt) external returns (address) { + return address(DeployUtils.deployHub(authority, salt)); + } } diff --git a/tests/unit/Hub/Hub.Config.t.sol b/tests/unit/Hub/Hub.Config.t.sol index 42cfde53b..7d1a32fc4 100644 --- a/tests/unit/Hub/Hub.Config.t.sol +++ b/tests/unit/Hub/Hub.Config.t.sol @@ -23,10 +23,10 @@ contract HubConfigTest is HubBase { ); } - function test_hub_deploy_reverts_on_InvalidConstructorInput() public { + function test_hub_deploy_revertsWith_InvalidAddress() public { DeployWrapper deployer = new DeployWrapper(); - vm.expectRevert(); + vm.expectRevert(IHub.InvalidAddress.selector); deployer.deployHub(address(0)); } diff --git a/tests/unit/Spoke/Spoke.Config.t.sol b/tests/unit/Spoke/Spoke.Config.t.sol index 855ffc245..7238dfbf5 100644 --- a/tests/unit/Spoke/Spoke.Config.t.sol +++ b/tests/unit/Spoke/Spoke.Config.t.sol @@ -25,7 +25,7 @@ contract SpokeConfigTest is SpokeBase { function test_spoke_deploy_reverts_on_InvalidConstructorInput() public { DeployWrapper deployer = new DeployWrapper(); - vm.expectRevert(); + vm.expectRevert(Create2Utils.Create2DeploymentFailed.selector); deployer.deploySpokeImplementation(address(0), Constants.MAX_ALLOWED_USER_RESERVES_LIMIT); } @@ -34,7 +34,7 @@ contract SpokeConfigTest is SpokeBase { address oracle = makeAddr('AaveOracle'); vm.mockCall(oracle, abi.encodeCall(IPriceOracle.DECIMALS, ()), abi.encode(7)); - vm.expectRevert(); + vm.expectRevert(Create2Utils.Create2DeploymentFailed.selector); deployer.deploySpokeImplementation(oracle, Constants.MAX_ALLOWED_USER_RESERVES_LIMIT); } @@ -43,7 +43,7 @@ contract SpokeConfigTest is SpokeBase { address oracle = makeAddr('AaveOracle'); vm.mockCall(oracle, abi.encodeCall(IPriceOracle.DECIMALS, ()), abi.encode(8)); - vm.expectRevert(); + vm.expectRevert(Create2Utils.Create2DeploymentFailed.selector); deployer.deploySpokeImplementation(oracle, 0); }