Skip to content

Commit

Permalink
fill in placeholders for default modules
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Mar 11, 2024
1 parent 8817a96 commit 4fea2b3
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/findLibraries.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/runDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ 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 = [
{
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),
},
];
27 changes: 27 additions & 0 deletions packages/cli/src/utils/findPlaceholders.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
),
),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) };
}
File renamed without changes.

0 comments on commit 4fea2b3

Please sign in to comment.