-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli,world): register system ABI onchain (#3050)
- Loading branch information
Showing
27 changed files
with
362 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/cli": patch | ||
--- | ||
|
||
In addition to table labels, system labels and ABIs are now registered onchain during deploy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
node_modules | ||
*.db | ||
*.db-journal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
indexer.db | ||
node_modules | ||
|
||
# mud artifacts | ||
.mud | ||
# sqlite indexer data | ||
*.db | ||
*.db-journal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function indent(message: string, indentation = " "): string { | ||
return message.replaceAll(/(^|\n)/g, `$1${indentation}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import createDebug from "debug"; | ||
|
||
export const debug = createDebug("mud:world"); | ||
export const error = createDebug("mud:world"); | ||
|
||
// Pipe debug output to stdout instead of stderr | ||
debug.log = console.debug.bind(console); | ||
|
||
// Pipe error output to stderr | ||
export const error = createDebug("mud:world"); | ||
error.log = console.error.bind(console); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { mkdir, writeFile } from "node:fs/promises"; | ||
import { ResolvedSystem, resolveSystems } from "./resolveSystems"; | ||
import { World } from "../config/v2"; | ||
import { ContractArtifact, systemsManifestFilename } from "./common"; | ||
import { findContractArtifacts } from "./findContractArtifacts"; | ||
import { getOutDirectory as getForgeOutDirectory } from "@latticexyz/common/foundry"; | ||
import path from "node:path"; | ||
import { Abi, Hex, isHex } from "viem"; | ||
import { formatAbi, formatAbiItem } from "abitype"; | ||
import { debug } from "./debug"; | ||
import { type } from "arktype"; | ||
|
||
export const SystemsManifest = type({ | ||
systems: [ | ||
{ | ||
// labels | ||
namespaceLabel: "string", | ||
label: "string", | ||
// resource ID | ||
namespace: "string", | ||
name: "string", | ||
systemId: ["string", ":", (s): s is Hex => isHex(s, { strict: false })], | ||
// abi | ||
abi: "string[]", | ||
worldAbi: "string[]", | ||
}, | ||
"[]", | ||
], | ||
createdAt: "number", | ||
}); | ||
|
||
export async function buildSystemsManifest(opts: { rootDir: string; config: World }): Promise<void> { | ||
// we have to import these at runtime because they may not yet exist at build time | ||
const { default: IBaseWorldAbi } = await import("../../out/IBaseWorld.sol/IBaseWorld.abi.json"); | ||
const { default: SystemAbi } = await import("../../out/System.sol/System.abi.json"); | ||
const excludedAbi = formatAbi([ | ||
...IBaseWorldAbi.filter((item) => item.type === "event" || item.type === "error"), | ||
...SystemAbi, | ||
] as Abi); | ||
|
||
const systems = await resolveSystems(opts); | ||
|
||
// TODO: expose a `cwd` option to make sure this runs relative to `rootDir` | ||
const forgeOutDir = await getForgeOutDirectory(); | ||
const contractArtifacts = await findContractArtifacts({ forgeOutDir }); | ||
|
||
function getSystemArtifact(system: ResolvedSystem): ContractArtifact { | ||
const artifact = contractArtifacts.find((a) => a.sourcePath === system.sourcePath && a.name === system.label); | ||
if (!artifact) { | ||
throw new Error( | ||
`Could not find build artifact for system \`${system.label}\` at \`${system.sourcePath}\`. Did \`forge build\` run successfully?`, | ||
); | ||
} | ||
return artifact; | ||
} | ||
|
||
const manifest = { | ||
systems: systems.map((system): (typeof SystemsManifest)["infer"]["systems"][number] => { | ||
const artifact = getSystemArtifact(system); | ||
const abi = artifact.abi.filter((item) => !excludedAbi.includes(formatAbiItem(item))); | ||
const worldAbi = system.deploy.registerWorldFunctions | ||
? abi.map((item) => (item.type === "function" ? { ...item, name: `${system.namespace}__${item.name}` } : item)) | ||
: []; | ||
return { | ||
// labels | ||
namespaceLabel: system.namespaceLabel, | ||
label: system.label, | ||
// resource ID | ||
namespace: system.namespace, | ||
name: system.name, | ||
systemId: system.systemId, | ||
// abi | ||
abi: formatAbi(abi).sort((a, b) => a.localeCompare(b)), | ||
worldAbi: formatAbi(worldAbi).sort((a, b) => a.localeCompare(b)), | ||
}; | ||
}), | ||
createdAt: Date.now(), | ||
} satisfies typeof SystemsManifest.infer; | ||
|
||
const outFile = path.join(opts.rootDir, systemsManifestFilename); | ||
await mkdir(path.dirname(outFile), { recursive: true }); | ||
await writeFile(outFile, JSON.stringify(manifest, null, 2) + "\n"); | ||
debug("Wrote systems manifest to", systemsManifestFilename); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Abi, Hex } from "viem"; | ||
|
||
// https://eips.ethereum.org/EIPS/eip-170 | ||
export const contractSizeLimit = parseInt("6000", 16); | ||
|
||
// relative to project root dir (`rootDir`) | ||
export const systemsManifestFilename = ".mud/local/systems.json"; | ||
|
||
export type ReferenceIdentifier = { | ||
/** | ||
* Path to source file, e.g. `src/SomeLib.sol` | ||
*/ | ||
sourcePath: string; | ||
/** | ||
* Reference name, e.g. `SomeLib` | ||
*/ | ||
name: string; | ||
}; | ||
|
||
export type PendingBytecode = readonly (Hex | ReferenceIdentifier)[]; | ||
|
||
export type ContractArtifact = { | ||
readonly sourcePath: string; | ||
readonly name: string; | ||
// TODO: rename `createCode` or `creationBytecode` to better differentiate from deployed bytecode? | ||
readonly bytecode: PendingBytecode; | ||
readonly abi: Abi; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { debug as parentDebug } from "../debug"; | ||
|
||
export const debug = parentDebug.extend("codegen"); |
Oops, something went wrong.