Skip to content
Open
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
1 change: 1 addition & 0 deletions tests/Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions tests/Create2Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
29 changes: 15 additions & 14 deletions tests/DeployUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -66,18 +71,14 @@ 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) {
Create2Utils.loadCreate2Factory();
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));

Expand Down
27 changes: 26 additions & 1 deletion tests/mocks/DeployWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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));
}
}
4 changes: 2 additions & 2 deletions tests/unit/Hub/Hub.Config.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Spoke/Spoke.Config.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
Loading