From 378d7200ffea0c5b54f8e994953abb4a5bd51b3b Mon Sep 17 00:00:00 2001 From: yonada Date: Wed, 24 Apr 2024 19:08:31 +0100 Subject: [PATCH] refactor(cli): getWorldContracts (#2718) Co-authored-by: Kevin Ingersoll --- packages/cli/src/deploy/ensureWorldFactory.ts | 117 +++--------------- packages/cli/src/deploy/getWorldContracts.ts | 79 ++++++++++++ .../src/deploy/getWorldFactoryContracts.ts | 27 ++++ .../deploy/getWorldProxyFactoryContracts.ts | 27 ++++ 4 files changed, 151 insertions(+), 99 deletions(-) create mode 100644 packages/cli/src/deploy/getWorldContracts.ts create mode 100644 packages/cli/src/deploy/getWorldFactoryContracts.ts create mode 100644 packages/cli/src/deploy/getWorldProxyFactoryContracts.ts diff --git a/packages/cli/src/deploy/ensureWorldFactory.ts b/packages/cli/src/deploy/ensureWorldFactory.ts index 9eb684bd62..3343755def 100644 --- a/packages/cli/src/deploy/ensureWorldFactory.ts +++ b/packages/cli/src/deploy/ensureWorldFactory.ts @@ -1,111 +1,30 @@ -import accessManagementSystemBuild from "@latticexyz/world/out/AccessManagementSystem.sol/AccessManagementSystem.json" assert { type: "json" }; -import balanceTransferSystemBuild from "@latticexyz/world/out/BalanceTransferSystem.sol/BalanceTransferSystem.json" assert { type: "json" }; -import batchCallSystemBuild from "@latticexyz/world/out/BatchCallSystem.sol/BatchCallSystem.json" assert { type: "json" }; -import registrationSystemBuild from "@latticexyz/world/out/RegistrationSystem.sol/RegistrationSystem.json" assert { type: "json" }; -import initModuleBuild from "@latticexyz/world/out/InitModule.sol/InitModule.json" assert { type: "json" }; -import initModuleAbi from "@latticexyz/world/out/InitModule.sol/InitModule.abi.json" assert { type: "json" }; -import worldFactoryBuild from "@latticexyz/world/out/WorldFactory.sol/WorldFactory.json" assert { type: "json" }; -import worldFactoryAbi from "@latticexyz/world/out/WorldFactory.sol/WorldFactory.abi.json" assert { type: "json" }; -import worldProxyFactoryBuild from "@latticexyz/world/out/WorldProxyFactory.sol/WorldProxyFactory.json" assert { type: "json" }; -import worldProxyFactoryAbi from "@latticexyz/world/out/WorldProxyFactory.sol/WorldProxyFactory.abi.json" assert { type: "json" }; -import { Client, Transport, Chain, Account, Hex, getCreate2Address, encodeDeployData, size, Address } from "viem"; -import { salt } from "./common"; +import { Client, Transport, Chain, Account, Hex, Address } from "viem"; import { ensureContractsDeployed } from "./ensureContractsDeployed"; -import { Contract } from "./ensureContract"; +import { getWorldFactoryContracts } from "./getWorldFactoryContracts"; +import { getWorldProxyFactoryContracts } from "./getWorldProxyFactoryContracts"; export async function ensureWorldFactory( client: Client, deployerAddress: Hex, withWorldProxy?: boolean, ): Promise
{ - const accessManagementSystemDeployedBytecodeSize = size(accessManagementSystemBuild.deployedBytecode.object as Hex); - const accessManagementSystemBytecode = accessManagementSystemBuild.bytecode.object as Hex; - const accessManagementSystem = getCreate2Address({ - from: deployerAddress, - bytecode: accessManagementSystemBytecode, - salt, - }); - - const balanceTransferSystemDeployedBytecodeSize = size(balanceTransferSystemBuild.deployedBytecode.object as Hex); - const balanceTransferSystemBytecode = balanceTransferSystemBuild.bytecode.object as Hex; - const balanceTransferSystem = getCreate2Address({ - from: deployerAddress, - bytecode: balanceTransferSystemBytecode, - salt, - }); - - const batchCallSystemDeployedBytecodeSize = size(batchCallSystemBuild.deployedBytecode.object as Hex); - const batchCallSystemBytecode = batchCallSystemBuild.bytecode.object as Hex; - const batchCallSystem = getCreate2Address({ from: deployerAddress, bytecode: batchCallSystemBytecode, salt }); - - const registrationDeployedBytecodeSize = size(registrationSystemBuild.deployedBytecode.object as Hex); - const registrationBytecode = registrationSystemBuild.bytecode.object as Hex; - const registration = getCreate2Address({ - from: deployerAddress, - bytecode: registrationBytecode, - salt, - }); - - const initModuleDeployedBytecodeSize = size(initModuleBuild.deployedBytecode.object as Hex); - const initModuleBytecode = encodeDeployData({ - bytecode: initModuleBuild.bytecode.object as Hex, - abi: initModuleAbi, - args: [accessManagementSystem, balanceTransferSystem, batchCallSystem, registration], - }); - - const initModule = getCreate2Address({ from: deployerAddress, bytecode: initModuleBytecode, salt }); - - const build = withWorldProxy ? worldProxyFactoryBuild : worldFactoryBuild; - const abi = withWorldProxy ? worldProxyFactoryAbi : worldFactoryAbi; - - const worldFactoryDeployedBytecodeSize = size(build.deployedBytecode.object as Hex); - const worldFactoryBytecode = encodeDeployData({ - bytecode: build.bytecode.object as Hex, - abi: abi, - args: [initModule], - }); - - const worldFactory = getCreate2Address({ from: deployerAddress, bytecode: worldFactoryBytecode, salt }); - - const worldFactoryContracts: readonly Contract[] = [ - { - bytecode: accessManagementSystemBytecode, - deployedBytecodeSize: accessManagementSystemDeployedBytecodeSize, - label: "access management system", - }, - { - bytecode: balanceTransferSystemBytecode, - deployedBytecodeSize: balanceTransferSystemDeployedBytecodeSize, - label: "balance transfer system", - }, - { - bytecode: batchCallSystemBytecode, - deployedBytecodeSize: batchCallSystemDeployedBytecodeSize, - label: "batch call system", - }, - { - bytecode: registrationBytecode, - deployedBytecodeSize: registrationDeployedBytecodeSize, - label: "core registration system", - }, - { - bytecode: initModuleBytecode, - deployedBytecodeSize: initModuleDeployedBytecodeSize, - label: "core module", - }, - { - bytecode: worldFactoryBytecode, - deployedBytecodeSize: worldFactoryDeployedBytecodeSize, - label: "world factory", - }, - ]; - - // WorldFactory constructor doesn't call InitModule, only sets its address, so we can do these in parallel since the address is deterministic + if (withWorldProxy) { + const contracts = getWorldProxyFactoryContracts(deployerAddress); + // We can deploy these contracts in parallel because they do not call each other at this point. + await ensureContractsDeployed({ + client, + deployerAddress, + contracts: Object.values(contracts), + }); + return contracts.WorldProxyFactory.address; + } + + const contracts = getWorldFactoryContracts(deployerAddress); + // We can deploy these contracts in parallel because they do not call each other at this point. await ensureContractsDeployed({ client, deployerAddress, - contracts: worldFactoryContracts, + contracts: Object.values(contracts), }); - - return worldFactory; + return contracts.WorldFactory.address; } diff --git a/packages/cli/src/deploy/getWorldContracts.ts b/packages/cli/src/deploy/getWorldContracts.ts new file mode 100644 index 0000000000..cb39d5400e --- /dev/null +++ b/packages/cli/src/deploy/getWorldContracts.ts @@ -0,0 +1,79 @@ +import accessManagementSystemBuild from "@latticexyz/world/out/AccessManagementSystem.sol/AccessManagementSystem.json" assert { type: "json" }; +import balanceTransferSystemBuild from "@latticexyz/world/out/BalanceTransferSystem.sol/BalanceTransferSystem.json" assert { type: "json" }; +import batchCallSystemBuild from "@latticexyz/world/out/BatchCallSystem.sol/BatchCallSystem.json" assert { type: "json" }; +import registrationSystemBuild from "@latticexyz/world/out/RegistrationSystem.sol/RegistrationSystem.json" assert { type: "json" }; +import initModuleBuild from "@latticexyz/world/out/InitModule.sol/InitModule.json" assert { type: "json" }; +import initModuleAbi from "@latticexyz/world/out/InitModule.sol/InitModule.abi.json" assert { type: "json" }; +import { Hex, getCreate2Address, encodeDeployData, size } from "viem"; +import { salt } from "./common"; + +export function getWorldContracts(deployerAddress: Hex) { + const accessManagementSystemDeployedBytecodeSize = size(accessManagementSystemBuild.deployedBytecode.object as Hex); + const accessManagementSystemBytecode = accessManagementSystemBuild.bytecode.object as Hex; + const accessManagementSystem = getCreate2Address({ + from: deployerAddress, + bytecode: accessManagementSystemBytecode, + salt, + }); + + const balanceTransferSystemDeployedBytecodeSize = size(balanceTransferSystemBuild.deployedBytecode.object as Hex); + const balanceTransferSystemBytecode = balanceTransferSystemBuild.bytecode.object as Hex; + const balanceTransferSystem = getCreate2Address({ + from: deployerAddress, + bytecode: balanceTransferSystemBytecode, + salt, + }); + + const batchCallSystemDeployedBytecodeSize = size(batchCallSystemBuild.deployedBytecode.object as Hex); + const batchCallSystemBytecode = batchCallSystemBuild.bytecode.object as Hex; + const batchCallSystem = getCreate2Address({ from: deployerAddress, bytecode: batchCallSystemBytecode, salt }); + + const registrationDeployedBytecodeSize = size(registrationSystemBuild.deployedBytecode.object as Hex); + const registrationBytecode = registrationSystemBuild.bytecode.object as Hex; + const registration = getCreate2Address({ + from: deployerAddress, + bytecode: registrationBytecode, + salt, + }); + + const initModuleDeployedBytecodeSize = size(initModuleBuild.deployedBytecode.object as Hex); + const initModuleBytecode = encodeDeployData({ + bytecode: initModuleBuild.bytecode.object as Hex, + abi: initModuleAbi, + args: [accessManagementSystem, balanceTransferSystem, batchCallSystem, registration], + }); + const initModule = getCreate2Address({ from: deployerAddress, bytecode: initModuleBytecode, salt }); + + return { + AccessManagementSystem: { + bytecode: accessManagementSystemBytecode, + deployedBytecodeSize: accessManagementSystemDeployedBytecodeSize, + label: "access management system", + address: accessManagementSystem, + }, + BalanceTransferSystem: { + bytecode: balanceTransferSystemBytecode, + deployedBytecodeSize: balanceTransferSystemDeployedBytecodeSize, + label: "balance transfer system", + address: balanceTransferSystem, + }, + BatchCallSystem: { + bytecode: batchCallSystemBytecode, + deployedBytecodeSize: batchCallSystemDeployedBytecodeSize, + label: "batch call system", + address: batchCallSystem, + }, + RegistrationSystem: { + bytecode: registrationBytecode, + deployedBytecodeSize: registrationDeployedBytecodeSize, + label: "core registration system", + address: registration, + }, + InitModule: { + bytecode: initModuleBytecode, + deployedBytecodeSize: initModuleDeployedBytecodeSize, + label: "core module", + address: initModule, + }, + }; +} diff --git a/packages/cli/src/deploy/getWorldFactoryContracts.ts b/packages/cli/src/deploy/getWorldFactoryContracts.ts new file mode 100644 index 0000000000..7934655a3e --- /dev/null +++ b/packages/cli/src/deploy/getWorldFactoryContracts.ts @@ -0,0 +1,27 @@ +import worldFactoryBuild from "@latticexyz/world/out/WorldFactory.sol/WorldFactory.json" assert { type: "json" }; +import worldFactoryAbi from "@latticexyz/world/out/WorldFactory.sol/WorldFactory.abi.json" assert { type: "json" }; +import { Hex, getCreate2Address, encodeDeployData, size } from "viem"; +import { salt } from "./common"; +import { getWorldContracts } from "./getWorldContracts"; + +export function getWorldFactoryContracts(deployerAddress: Hex) { + const worldContracts = getWorldContracts(deployerAddress); + + const worldFactoryDeployedBytecodeSize = size(worldFactoryBuild.deployedBytecode.object as Hex); + const worldFactoryBytecode = encodeDeployData({ + bytecode: worldFactoryBuild.bytecode.object as Hex, + abi: worldFactoryAbi, + args: [worldContracts.InitModule.address], + }); + const worldFactory = getCreate2Address({ from: deployerAddress, bytecode: worldFactoryBytecode, salt }); + + return { + ...worldContracts, + WorldFactory: { + bytecode: worldFactoryBytecode, + deployedBytecodeSize: worldFactoryDeployedBytecodeSize, + label: "world factory", + address: worldFactory, + }, + }; +} diff --git a/packages/cli/src/deploy/getWorldProxyFactoryContracts.ts b/packages/cli/src/deploy/getWorldProxyFactoryContracts.ts new file mode 100644 index 0000000000..af292d2141 --- /dev/null +++ b/packages/cli/src/deploy/getWorldProxyFactoryContracts.ts @@ -0,0 +1,27 @@ +import worldProxyFactoryBuild from "@latticexyz/world/out/WorldProxyFactory.sol/WorldProxyFactory.json" assert { type: "json" }; +import worldProxyFactoryAbi from "@latticexyz/world/out/WorldProxyFactory.sol/WorldProxyFactory.abi.json" assert { type: "json" }; +import { Hex, getCreate2Address, encodeDeployData, size } from "viem"; +import { salt } from "./common"; +import { getWorldContracts } from "./getWorldContracts"; + +export function getWorldProxyFactoryContracts(deployerAddress: Hex) { + const worldContracts = getWorldContracts(deployerAddress); + + const worldProxyFactoryDeployedBytecodeSize = size(worldProxyFactoryBuild.deployedBytecode.object as Hex); + const worldProxyFactoryBytecode = encodeDeployData({ + bytecode: worldProxyFactoryBuild.bytecode.object as Hex, + abi: worldProxyFactoryAbi, + args: [worldContracts.InitModule.address], + }); + const worldProxyFactory = getCreate2Address({ from: deployerAddress, bytecode: worldProxyFactoryBytecode, salt }); + + return { + ...worldContracts, + WorldProxyFactory: { + bytecode: worldProxyFactoryBytecode, + deployedBytecodeSize: worldProxyFactoryDeployedBytecodeSize, + label: "world proxy factory", + address: worldProxyFactory, + }, + }; +}