From a93fef9c3cad5de2b56f6cbf9ea2c18f6b28e7a1 Mon Sep 17 00:00:00 2001 From: Goran Vladika Date: Thu, 31 Aug 2023 13:08:33 +0200 Subject: [PATCH] Extract L2 proxy address calculation --- .../ethereum/L1AtomicTokenBridgeCreator.sol | 107 +++++------------- 1 file changed, 28 insertions(+), 79 deletions(-) diff --git a/contracts/tokenbridge/ethereum/L1AtomicTokenBridgeCreator.sol b/contracts/tokenbridge/ethereum/L1AtomicTokenBridgeCreator.sol index fc4bd0d3e7..83d752b6d2 100644 --- a/contracts/tokenbridge/ethereum/L1AtomicTokenBridgeCreator.sol +++ b/contracts/tokenbridge/ethereum/L1AtomicTokenBridgeCreator.sol @@ -355,98 +355,26 @@ contract L1AtomicTokenBridgeCreator is Initializable, OwnableUpgradeable { } function getCanonicalL2RouterAddress() public view returns (address) { - address logicSeedAddress = Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_ROUTER_LOGIC), - keccak256(type(CanonicalAddressSeed).creationCode), - canonicalL2FactoryAddress - ); - - return Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_ROUTER), - keccak256( - abi.encodePacked( - type(TransparentUpgradeableProxy).creationCode, - abi.encode(logicSeedAddress, canonicalL2ProxyAdminAddress, bytes("")) - ) - ), - canonicalL2FactoryAddress - ); + return _getProxyAddress(_getL2Salt(OrbitSalts.L2_ROUTER_LOGIC), _getL2Salt(OrbitSalts.L2_ROUTER)); } function getCanonicalL2StandardGatewayAddress() public view returns (address) { - address logicSeedAddress = Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_STANDARD_GATEWAY_LOGIC), - keccak256(type(CanonicalAddressSeed).creationCode), - canonicalL2FactoryAddress - ); - - return Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_STANDARD_GATEWAY), - keccak256( - abi.encodePacked( - type(TransparentUpgradeableProxy).creationCode, - abi.encode(logicSeedAddress, canonicalL2ProxyAdminAddress, bytes("")) - ) - ), - canonicalL2FactoryAddress + return _getProxyAddress( + _getL2Salt(OrbitSalts.L2_STANDARD_GATEWAY_LOGIC), _getL2Salt(OrbitSalts.L2_STANDARD_GATEWAY) ); } function getCanonicalL2CustomGatewayAddress() public view returns (address) { - address logicSeedAddress = Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_CUSTOM_GATEWAY_LOGIC), - keccak256(type(CanonicalAddressSeed).creationCode), - canonicalL2FactoryAddress - ); - - return Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_CUSTOM_GATEWAY), - keccak256( - abi.encodePacked( - type(TransparentUpgradeableProxy).creationCode, - abi.encode(logicSeedAddress, canonicalL2ProxyAdminAddress, bytes("")) - ) - ), - canonicalL2FactoryAddress - ); + return + _getProxyAddress(_getL2Salt(OrbitSalts.L2_CUSTOM_GATEWAY_LOGIC), _getL2Salt(OrbitSalts.L2_CUSTOM_GATEWAY)); } function getCanonicalL2WethGatewayAddress() public view returns (address) { - address logicSeedAddress = Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_WETH_GATEWAY_LOGIC), - keccak256(type(CanonicalAddressSeed).creationCode), - canonicalL2FactoryAddress - ); - - return Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_WETH_GATEWAY), - keccak256( - abi.encodePacked( - type(TransparentUpgradeableProxy).creationCode, - abi.encode(logicSeedAddress, canonicalL2ProxyAdminAddress, bytes("")) - ) - ), - canonicalL2FactoryAddress - ); + return _getProxyAddress(_getL2Salt(OrbitSalts.L2_WETH_GATEWAY_LOGIC), _getL2Salt(OrbitSalts.L2_WETH_GATEWAY)); } function getCanonicalL2WethAddress() public view returns (address) { - address logicSeedAddress = Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_WETH_LOGIC), - keccak256(type(CanonicalAddressSeed).creationCode), - canonicalL2FactoryAddress - ); - - return Create2.computeAddress( - _getL2Salt(OrbitSalts.L2_WETH), - keccak256( - abi.encodePacked( - type(TransparentUpgradeableProxy).creationCode, - abi.encode(logicSeedAddress, canonicalL2ProxyAdminAddress, bytes("")) - ) - ), - canonicalL2FactoryAddress - ); + return _getProxyAddress(_getL2Salt(OrbitSalts.L2_WETH_LOGIC), _getL2Salt(OrbitSalts.L2_WETH)); } /** @@ -496,6 +424,27 @@ contract L1AtomicTokenBridgeCreator is Initializable, OwnableUpgradeable { return IInbox(inbox).bridge().rollup().owner(); } + /** + * L2 contracts are deployed as proxy with dummy seed logic contracts using CREATE2. That enables + * us to upfront calculate the expected canonical addresses. + */ + function _getProxyAddress(bytes32 logicSalt, bytes32 proxySalt) internal view returns (address) { + address logicSeedAddress = Create2.computeAddress( + logicSalt, keccak256(type(CanonicalAddressSeed).creationCode), canonicalL2FactoryAddress + ); + + return Create2.computeAddress( + proxySalt, + keccak256( + abi.encodePacked( + type(TransparentUpgradeableProxy).creationCode, + abi.encode(logicSeedAddress, canonicalL2ProxyAdminAddress, bytes("")) + ) + ), + canonicalL2FactoryAddress + ); + } + /** * We want to have exactly one set of canonical token bridge contracts for every rollup. For that * reason we make rollup's inbox address part of the salt. It prevents deploying more than one