From 4fea2b3f9110f8b8c201194926658934802439ad Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Mon, 11 Mar 2024 14:10:26 +0000 Subject: [PATCH] fill in placeholders for default modules --- packages/cli/src/deploy/findLibraries.ts | 2 +- packages/cli/src/deploy/resolveConfig.ts | 2 +- packages/cli/src/runDeploy.ts | 2 +- ...constants.ts => defaultModuleContracts.ts} | 7 ++--- packages/cli/src/utils/findPlaceholders.ts | 27 +++++++++++++++++++ .../src/utils/{utils => }/getContractData.ts | 24 +++-------------- .../cli/src/utils/{utils => }/postDeploy.ts | 0 7 files changed, 37 insertions(+), 27 deletions(-) rename packages/cli/src/utils/{modules/constants.ts => defaultModuleContracts.ts} (80%) create mode 100644 packages/cli/src/utils/findPlaceholders.ts rename packages/cli/src/utils/{utils => }/getContractData.ts (68%) rename packages/cli/src/utils/{utils => }/postDeploy.ts (100%) diff --git a/packages/cli/src/deploy/findLibraries.ts b/packages/cli/src/deploy/findLibraries.ts index 53a07041b6..160134b510 100644 --- a/packages/cli/src/deploy/findLibraries.ts +++ b/packages/cli/src/deploy/findLibraries.ts @@ -1,6 +1,6 @@ import { readFileSync } from "fs"; import glob from "glob"; -import { LinkReferences } from "../utils/utils/getContractData"; +import { LinkReferences } from "../utils/getContractData"; import { orderByDependencies } from "./orderByDependencies"; export function findLibraries(forgeOutDir: string): readonly { diff --git a/packages/cli/src/deploy/resolveConfig.ts b/packages/cli/src/deploy/resolveConfig.ts index 16fca6cdb3..a79d857775 100644 --- a/packages/cli/src/deploy/resolveConfig.ts +++ b/packages/cli/src/deploy/resolveConfig.ts @@ -8,7 +8,7 @@ import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema- import { Hex, hexToBytes, bytesToHex, toFunctionSelector, toFunctionSignature } from "viem"; import { getExistingContracts } from "../utils/getExistingContracts"; import { defaultModuleContracts } from "../utils/modules/constants"; -import { getContractData } from "../utils/utils/getContractData"; +import { getContractData } from "../utils/getContractData"; import { configToTables } from "./configToTables"; import { groupBy } from "@latticexyz/common/utils"; import { findLibraries } from "./findLibraries"; diff --git a/packages/cli/src/runDeploy.ts b/packages/cli/src/runDeploy.ts index cd0b971540..837026c9da 100644 --- a/packages/cli/src/runDeploy.ts +++ b/packages/cli/src/runDeploy.ts @@ -12,7 +12,7 @@ import chalk from "chalk"; import { MUDError } from "@latticexyz/common/errors"; import { resolveConfig } from "./deploy/resolveConfig"; import { getChainId } from "viem/actions"; -import { postDeploy } from "./utils/utils/postDeploy"; +import { postDeploy } from "./utils/postDeploy"; import { WorldDeploy } from "./deploy/common"; import { build } from "./build"; diff --git a/packages/cli/src/utils/modules/constants.ts b/packages/cli/src/utils/defaultModuleContracts.ts similarity index 80% rename from packages/cli/src/utils/modules/constants.ts rename to packages/cli/src/utils/defaultModuleContracts.ts index 569d4a6073..debb7b6828 100644 --- a/packages/cli/src/utils/modules/constants.ts +++ b/packages/cli/src/utils/defaultModuleContracts.ts @@ -2,6 +2,7 @@ import KeysWithValueModuleData from "@latticexyz/world-modules/out/KeysWithValue import KeysInTableModuleData from "@latticexyz/world-modules/out/KeysInTableModule.sol/KeysInTableModule.json" assert { type: "json" }; import UniqueEntityModuleData from "@latticexyz/world-modules/out/UniqueEntityModule.sol/UniqueEntityModule.json" assert { type: "json" }; import { Abi, Hex, size } from "viem"; +import { findPlaceholders } from "./findPlaceholders"; // These modules are always deployed export const defaultModuleContracts = [ @@ -9,21 +10,21 @@ export const defaultModuleContracts = [ name: "KeysWithValueModule", abi: KeysWithValueModuleData.abi as Abi, bytecode: KeysWithValueModuleData.bytecode.object as Hex, - placeholders: [], // TODO + placeholders: findPlaceholders(KeysWithValueModuleData.bytecode.linkReferences), deployedBytecodeSize: size(KeysWithValueModuleData.deployedBytecode.object as Hex), }, { name: "KeysInTableModule", abi: KeysInTableModuleData.abi as Abi, bytecode: KeysInTableModuleData.bytecode.object as Hex, - placeholders: [], // TODO + placeholders: findPlaceholders(KeysInTableModuleData.bytecode.linkReferences), deployedBytecodeSize: size(KeysInTableModuleData.deployedBytecode.object as Hex), }, { name: "UniqueEntityModule", abi: UniqueEntityModuleData.abi as Abi, bytecode: UniqueEntityModuleData.bytecode.object as Hex, - placeholders: [], // TODO + placeholders: findPlaceholders(UniqueEntityModuleData.bytecode.linkReferences), deployedBytecodeSize: size(UniqueEntityModuleData.deployedBytecode.object as Hex), }, ]; diff --git a/packages/cli/src/utils/findPlaceholders.ts b/packages/cli/src/utils/findPlaceholders.ts new file mode 100644 index 0000000000..5006624774 --- /dev/null +++ b/packages/cli/src/utils/findPlaceholders.ts @@ -0,0 +1,27 @@ +import { LibraryPlaceholder } from "../deploy/common"; + +// TODO: move this to a broader solc artifact type +/** From `artifact.bytecode.linkReferences` where `artifact` is the solc JSON output of a compiled Solidity contract */ +export type LinkReferences = { + [filename: string]: { + [name: string]: { + start: number; + length: number; + }[]; + }; +}; + +export function findPlaceholders(linkReferences: LinkReferences): readonly LibraryPlaceholder[] { + return Object.entries(linkReferences).flatMap(([path, contracts]) => + Object.entries(contracts).flatMap(([contractName, locations]) => + locations.map( + (location): LibraryPlaceholder => ({ + path, + name: contractName, + start: location.start, + length: location.length, + }), + ), + ), + ); +} diff --git a/packages/cli/src/utils/utils/getContractData.ts b/packages/cli/src/utils/getContractData.ts similarity index 68% rename from packages/cli/src/utils/utils/getContractData.ts rename to packages/cli/src/utils/getContractData.ts index 5fdbd20594..c55e2c9a11 100644 --- a/packages/cli/src/utils/utils/getContractData.ts +++ b/packages/cli/src/utils/getContractData.ts @@ -2,16 +2,8 @@ import { readFileSync } from "fs"; import path from "path"; import { MUDError } from "@latticexyz/common/errors"; import { Abi, Hex, size } from "viem"; -import { LibraryPlaceholder } from "../../deploy/common"; - -export type LinkReferences = { - [filename: string]: { - [name: string]: { - start: number; - length: number; - }[]; - }; -}; +import { LibraryPlaceholder } from "../deploy/common"; +import { findPlaceholders } from "./findPlaceholders"; /** * Load the contract's abi and bytecode from the file system @@ -39,17 +31,7 @@ export function getContractData( const abi = data?.abi; if (!abi) throw new MUDError(`No ABI found in ${contractDataPath}`); - const placeholders = Object.entries((data?.bytecode?.linkReferences ?? {}) as LinkReferences).flatMap( - ([path, contracts]) => - Object.entries(contracts).flatMap(([contractName, locations]) => - locations.map((location) => ({ - path, - name: contractName, - start: location.start, - length: location.length, - })), - ), - ); + const placeholders = findPlaceholders(data?.bytecode?.linkReferences ?? {}); return { abi, bytecode, placeholders, deployedBytecodeSize: size(deployedBytecode as Hex) }; } diff --git a/packages/cli/src/utils/utils/postDeploy.ts b/packages/cli/src/utils/postDeploy.ts similarity index 100% rename from packages/cli/src/utils/utils/postDeploy.ts rename to packages/cli/src/utils/postDeploy.ts