|
| 1 | +import path from "node:path"; |
| 2 | +import { Module } from "./common"; |
| 3 | +import { resolveWithContext } from "@latticexyz/config/library"; |
| 4 | +import { encodeField } from "@latticexyz/protocol-parser/internal"; |
| 5 | +import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type/internal"; |
| 6 | +import { bytesToHex, hexToBytes } from "viem"; |
| 7 | +import { createPrepareDeploy } from "./createPrepareDeploy"; |
| 8 | +import { World } from "@latticexyz/world"; |
| 9 | +import { getContractArtifact } from "../utils/getContractArtifact"; |
| 10 | +import { knownModuleArtifacts } from "../utils/knownModuleArtifacts"; |
| 11 | + |
| 12 | +export async function configToModules<config extends World>( |
| 13 | + config: config, |
| 14 | + forgeOutDir: string, |
| 15 | +): Promise<readonly Module[]> { |
| 16 | + // this expects a namespaced table name when used with `resolveTableId` |
| 17 | + const resolveContext = { |
| 18 | + tableIds: Object.fromEntries( |
| 19 | + Object.entries(config.tables).map(([tableName, table]) => [tableName, hexToBytes(table.tableId)]), |
| 20 | + ), |
| 21 | + }; |
| 22 | + |
| 23 | + const modules = await Promise.all( |
| 24 | + config.modules.map(async (mod): Promise<Module> => { |
| 25 | + let artifactPath = mod.artifactPath; |
| 26 | + |
| 27 | + // Backwards compatibility |
| 28 | + // TODO: move this up a level so we don't need `forgeOutDir` in here? |
| 29 | + if (!artifactPath) { |
| 30 | + if (mod.name) { |
| 31 | + artifactPath = |
| 32 | + knownModuleArtifacts[mod.name as keyof typeof knownModuleArtifacts] ?? |
| 33 | + path.join(forgeOutDir, `${mod.name}.sol`, `${mod.name}.json`); |
| 34 | + console.warn( |
| 35 | + [ |
| 36 | + "", |
| 37 | + `⚠️ Your \`mud.config.ts\` is using a module with a \`name\`, but this option is deprecated.`, |
| 38 | + "", |
| 39 | + "To resolve this, you can replace this:", |
| 40 | + "", |
| 41 | + ` name: ${JSON.stringify(mod.name)}`, |
| 42 | + "", |
| 43 | + "with this:", |
| 44 | + "", |
| 45 | + ` artifactPath: ${JSON.stringify(artifactPath)}`, |
| 46 | + "", |
| 47 | + ].join("\n"), |
| 48 | + ); |
| 49 | + } else { |
| 50 | + throw new Error("No `artifactPath` provided for module."); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const name = path.basename(artifactPath, ".json"); |
| 55 | + const artifact = await getContractArtifact({ artifactPath }); |
| 56 | + |
| 57 | + // TODO: replace args with something more strongly typed |
| 58 | + const installArgs = mod.args |
| 59 | + .map((arg) => resolveWithContext(arg, resolveContext)) |
| 60 | + .map((arg) => { |
| 61 | + const value = arg.value instanceof Uint8Array ? bytesToHex(arg.value) : arg.value; |
| 62 | + return encodeField(arg.type as SchemaAbiType, value as SchemaAbiTypeToPrimitiveType<SchemaAbiType>); |
| 63 | + }); |
| 64 | + |
| 65 | + if (installArgs.length > 1) { |
| 66 | + throw new Error(`${name} module should only have 0-1 args, but had ${installArgs.length} args.`); |
| 67 | + } |
| 68 | + |
| 69 | + return { |
| 70 | + name, |
| 71 | + installAsRoot: mod.root, |
| 72 | + installData: installArgs.length === 0 ? "0x" : installArgs[0], |
| 73 | + prepareDeploy: createPrepareDeploy(artifact.bytecode, artifact.placeholders), |
| 74 | + deployedBytecodeSize: artifact.deployedBytecodeSize, |
| 75 | + abi: artifact.abi, |
| 76 | + }; |
| 77 | + }), |
| 78 | + ); |
| 79 | + |
| 80 | + return modules; |
| 81 | +} |
0 commit comments