diff --git a/packages/protocol/genesis/GenerateGenesis.g.sol b/packages/protocol/genesis/GenerateGenesis.g.sol index ba0b6b05722..3f8ab297f28 100644 --- a/packages/protocol/genesis/GenerateGenesis.g.sol +++ b/packages/protocol/genesis/GenerateGenesis.g.sol @@ -81,14 +81,9 @@ contract TestGenerateGenesis is Test, AddressResolver { checkSavedAddress(addressManagerProxy, "ERC1155Vault", "erc1155_vault"); checkSavedAddress(addressManagerProxy, "SignalService", "signal_service"); - AddressManager newAddressManager = new AddressManager(); + vm.startPrank(addressManagerProxy.owner()); - AddressManager addressManager = - AddressManager(getPredeployedContractAddress("SharedAddressManagerImpl")); - - vm.startPrank(addressManager.owner()); - - addressManager.upgradeTo(address(newAddressManager)); + addressManagerProxy.upgradeTo(address(new AddressManager())); vm.stopPrank(); } @@ -102,14 +97,9 @@ contract TestGenerateGenesis is Test, AddressResolver { checkSavedAddress(addressManagerProxy, "TaikoL2", "taiko"); checkSavedAddress(addressManagerProxy, "SignalService", "signal_service"); - AddressManager addressManager = - AddressManager(getPredeployedContractAddress("RollupAddressManagerImpl")); + vm.startPrank(addressManagerProxy.owner()); - AddressManager newAddressManager = new AddressManager(); - - vm.startPrank(addressManager.owner()); - - addressManager.upgradeTo(address(newAddressManager)); + addressManagerProxy.upgradeTo(address(new AddressManager())); vm.stopPrank(); } @@ -143,13 +133,9 @@ contract TestGenerateGenesis is Test, AddressResolver { } vm.stopPrank(); - TaikoL2 taikoL2 = TaikoL2(getPredeployedContractAddress("TaikoL2Impl")); - - vm.startPrank(taikoL2.owner()); + vm.startPrank(taikoL2Proxy.owner()); - TaikoL2 newTaikoL2 = new TaikoL2(); - - taikoL2.upgradeTo(address(newTaikoL2)); + taikoL2Proxy.upgradeTo(address(new TaikoL2())); vm.stopPrank(); } @@ -206,11 +192,7 @@ contract TestGenerateGenesis is Test, AddressResolver { bridgeProxy.unpause(); assertEq(bridgeProxy.paused(), false); - Bridge bridge = Bridge(payable(getPredeployedContractAddress("BridgeImpl"))); - - Bridge newBridge = new Bridge(); - - bridge.upgradeTo(address(newBridge)); + bridgeProxy.upgradeTo(address(new Bridge())); vm.stopPrank(); } @@ -230,13 +212,9 @@ contract TestGenerateGenesis is Test, AddressResolver { addressManager.setAddress(1, "erc20_vault", erc20VaultAddress); vm.stopPrank(); - ERC20Vault erc20Vault = ERC20Vault(getPredeployedContractAddress("ERC20VaultImpl")); - - vm.startPrank(erc20Vault.owner()); - - ERC20Vault newERC20Vault = new ERC20Vault(); + vm.startPrank(erc20VaultProxy.owner()); - erc20Vault.upgradeTo(address(newERC20Vault)); + erc20VaultProxy.upgradeTo(address(new ERC20Vault())); vm.stopPrank(); } @@ -256,12 +234,9 @@ contract TestGenerateGenesis is Test, AddressResolver { addressManager.setAddress(1, "erc721_vault", erc721VaultAddress); vm.stopPrank(); - ERC721Vault erc721Vault = ERC721Vault(getPredeployedContractAddress("ERC721VaultImpl")); + vm.startPrank(erc721VaultProxy.owner()); - vm.startPrank(erc721Vault.owner()); - ERC721Vault newERC721Vault = new ERC721Vault(); - - erc721Vault.upgradeTo(address(newERC721Vault)); + erc721VaultProxy.upgradeTo(address(new ERC721Vault())); vm.stopPrank(); } @@ -283,13 +258,10 @@ contract TestGenerateGenesis is Test, AddressResolver { address erc1155VaultAddress = getPredeployedContractAddress("ERC1155VaultImpl"); - ERC1155Vault erc1155Vault = ERC1155Vault(erc1155VaultAddress); - - vm.startPrank(erc1155Vault.owner()); - ERC1155Vault newERC1155Vault = new ERC1155Vault(); + vm.startPrank(erc1155VaultProxy.owner()); - erc1155Vault.upgradeTo(address(newERC1155Vault)); + erc1155VaultProxy.upgradeTo(address(new ERC1155Vault())); vm.stopPrank(); } @@ -314,9 +286,7 @@ contract TestGenerateGenesis is Test, AddressResolver { SignalService signalService = SignalService(payable(getPredeployedContractAddress("SignalServiceImpl"))); - SignalService newSignalService = new SignalService(); - - signalService.upgradeTo(address(newSignalService)); + signalServiceProxy.upgradeTo(address(new SignalService())); vm.stopPrank(); } @@ -340,7 +310,6 @@ contract TestGenerateGenesis is Test, AddressResolver { assertEq(address(contractAddress).code, vm.parseBytes(deployedCode)); } - function checkProxyImplementation( string memory proxyName, string memory contractName diff --git a/packages/protocol/utils/generate_genesis/taikoL2.ts b/packages/protocol/utils/generate_genesis/taikoL2.ts index 60276374665..ce910680c52 100644 --- a/packages/protocol/utils/generate_genesis/taikoL2.ts +++ b/packages/protocol/utils/generate_genesis/taikoL2.ts @@ -189,6 +189,8 @@ async function generateContractConfigs( const addressMap: any = {}; + const uupsImmutableReferencesMap: any = getUUPSImmutableReferences(); + for (const [contractName, artifact] of Object.entries(contractArtifacts)) { const bytecode = (artifact as any).bytecode; @@ -215,9 +217,14 @@ async function generateContractConfigs( // Shared Contracts SharedAddressManagerImpl: { address: addressMap.SharedAddressManagerImpl, - deployedBytecode: - contractArtifacts.SharedAddressManagerImpl.deployedBytecode - .object, + deployedBytecode: replaceUUPSImmutableVaules( + contractArtifacts.SharedAddressManagerImpl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad( + addressMap.SharedAddressManagerImpl, + 32, + ), + ).deployedBytecode.object, variables: { _owner: ownerSecurityCouncil, }, @@ -270,7 +277,11 @@ async function generateContractConfigs( BridgeImpl: { address: addressMap.BridgeImpl, deployedBytecode: linkContractLibs( - contractArtifacts.BridgeImpl, + replaceUUPSImmutableVaules( + contractArtifacts.BridgeImpl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad(addressMap.BridgeImpl, 32), + ), addressMap, ), variables: { @@ -300,7 +311,11 @@ async function generateContractConfigs( ERC20VaultImpl: { address: addressMap.ERC20VaultImpl, deployedBytecode: linkContractLibs( - contractArtifacts.ERC20VaultImpl, + replaceUUPSImmutableVaules( + contractArtifacts.ERC20VaultImpl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad(addressMap.ERC20VaultImpl, 32), + ), addressMap, ), variables: { @@ -331,7 +346,11 @@ async function generateContractConfigs( ERC721VaultImpl: { address: addressMap.ERC721VaultImpl, deployedBytecode: linkContractLibs( - contractArtifacts.ERC721VaultImpl, + replaceUUPSImmutableVaules( + contractArtifacts.ERC721VaultImpl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad(addressMap.ERC721VaultImpl, 32), + ), addressMap, ), variables: { @@ -362,7 +381,11 @@ async function generateContractConfigs( ERC1155VaultImpl: { address: addressMap.ERC1155VaultImpl, deployedBytecode: linkContractLibs( - contractArtifacts.ERC1155VaultImpl, + replaceUUPSImmutableVaules( + contractArtifacts.ERC1155VaultImpl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad(addressMap.ERC1155VaultImpl, 32), + ), addressMap, ), variables: { @@ -392,8 +415,11 @@ async function generateContractConfigs( }, BridgedERC20: { address: addressMap.BridgedERC20Impl, - deployedBytecode: - contractArtifacts.BridgedERC20Impl.deployedBytecode.object, + deployedBytecode: replaceUUPSImmutableVaules( + contractArtifacts.BridgedERC20Impl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad(addressMap.BridgedERC20Impl, 32), + ).deployedBytecode.object, }, BridgedERC721: { address: addressMap.BridgedERC721Impl, @@ -402,13 +428,20 @@ async function generateContractConfigs( }, BridgedERC1155: { address: addressMap.BridgedERC1155Impl, - deployedBytecode: - contractArtifacts.BridgedERC1155Impl.deployedBytecode.object, + deployedBytecode: replaceUUPSImmutableVaules( + contractArtifacts.BridgedERC1155Impl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad(addressMap.BridgedERC1155Impl, 32), + ).deployedBytecode.object, }, SignalServiceImpl: { address: addressMap.SignalServiceImpl, deployedBytecode: linkContractLibs( - contractArtifacts.SignalServiceImpl, + replaceUUPSImmutableVaules( + contractArtifacts.SignalServiceImpl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad(addressMap.SignalServiceImpl, 32), + ), addressMap, ), variables: { @@ -444,7 +477,11 @@ async function generateContractConfigs( TaikoL2Impl: { address: addressMap.TaikoL2Impl, deployedBytecode: linkContractLibs( - contractArtifacts.TaikoL2Impl, + replaceUUPSImmutableVaules( + contractArtifacts.TaikoL2Impl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad(addressMap.TaikoL2Impl, 32), + ), addressMap, ), variables: { @@ -483,9 +520,14 @@ async function generateContractConfigs( }, RollupAddressManagerImpl: { address: addressMap.RollupAddressManagerImpl, - deployedBytecode: - contractArtifacts.RollupAddressManagerImpl.deployedBytecode - .object, + deployedBytecode: replaceUUPSImmutableVaules( + contractArtifacts.RollupAddressManagerImpl, + uupsImmutableReferencesMap, + ethers.utils.hexZeroPad( + addressMap.RollupAddressManagerImpl, + 32, + ), + ).deployedBytecode.object, variables: { _owner: ownerSecurityCouncil, }, @@ -562,3 +604,52 @@ function getLinkLibs(artifact: any, linkRefs: any, addressMap: any) { return result; } + +function getUUPSImmutableReferences() { + const references: any = {}; + const contractName = "UUPSUpgradeable"; + const immutableValueName = "__self"; + const artifact = require( + path.join(ARTIFACTS_PATH, `./${contractName}.sol/${contractName}.json`), + ); + + for (const node of artifact.ast.nodes) { + if (node.nodeType !== "ContractDefinition") continue; + + for (const subNode of node.nodes) { + if (subNode.name !== immutableValueName) continue; + references[`${contractName}`] = { + name: immutableValueName, + id: subNode.id, + }; + break; + } + } + + return references; +} + +function replaceUUPSImmutableVaules( + artifact: any, + references: any, + value: string, +): any { + const offsets = + artifact.deployedBytecode.immutableReferences[ + `${references.UUPSUpgradeable.id}` + ]; + let deployedBytecodeWithoutPrefix = + artifact.deployedBytecode.object.substring(2); + if (value.startsWith("0x")) value = value.substring(2); + + for (const { start, length } of offsets) { + const prefix = deployedBytecodeWithoutPrefix.substring(0, start * 2); + const suffix = deployedBytecodeWithoutPrefix.substring( + start * 2 + length * 2, + ); + deployedBytecodeWithoutPrefix = `${prefix}${value}${suffix}`; + } + + artifact.deployedBytecode.object = `0x${deployedBytecodeWithoutPrefix}`; + return artifact; +}