Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): getWorldContracts #2718

Merged
merged 7 commits into from
Apr 24, 2024
Merged
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
117 changes: 18 additions & 99 deletions packages/cli/src/deploy/ensureWorldFactory.ts
Original file line number Diff line number Diff line change
@@ -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<Transport, Chain | undefined, Account>,
deployerAddress: Hex,
withWorldProxy?: boolean,
): Promise<Address> {
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;
}
79 changes: 79 additions & 0 deletions packages/cli/src/deploy/getWorldContracts.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};
}
27 changes: 27 additions & 0 deletions packages/cli/src/deploy/getWorldFactoryContracts.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};
}
27 changes: 27 additions & 0 deletions packages/cli/src/deploy/getWorldProxyFactoryContracts.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};
}
Loading