Skip to content

Commit

Permalink
change library map interface
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 19, 2024
1 parent c816de9 commit 701b78a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
14 changes: 3 additions & 11 deletions packages/cli/src/deploy/createPrepareDeploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DeterministicContract, LibraryPlaceholder, salt } from "./common";
import { spliceHex } from "@latticexyz/common";
import { Hex, getCreate2Address, Address } from "viem";
import { LibraryMap, getLibraryKey } from "./getLibraryMap";
import { LibraryMap } from "./getLibraryMap";

export function createPrepareDeploy(
bytecodeWithPlaceholders: Hex,
Expand All @@ -10,16 +10,8 @@ export function createPrepareDeploy(
return function prepareDeploy(deployer: Address, libraryMap: LibraryMap) {
let bytecode = bytecodeWithPlaceholders;
for (const placeholder of placeholders) {
const libraryKey = getLibraryKey(placeholder);
const library = libraryMap[libraryKey];
if (!library) {
throw new Error(`Could not find library for bytecode placeholder ${placeholder.path}:${placeholder.name}`);
}

// Store the prepared address in the library map to avoid preparing the same library twice
library.address ??= library.prepareDeploy(deployer, libraryMap).address;

bytecode = spliceHex(bytecode, placeholder.start, placeholder.length, library.address);
const address = libraryMap.getAddress({ name: placeholder.name, path: placeholder.path, deployer });
bytecode = spliceHex(bytecode, placeholder.start, placeholder.length, address);
}
return {
bytecode,
Expand Down
35 changes: 29 additions & 6 deletions packages/cli/src/deploy/getLibraryMap.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
import { Hex } from "viem";
import { Address } from "viem";
import { Library } from "./common";

export type LibraryMap = { [key: string]: Library & { address?: Hex } };
export type LibraryMap = {
getAddress: (opts: { path: string; name: string; deployer: Address }) => Address;
};

export function getLibraryMap(libraries: readonly Library[]): LibraryMap {
return Object.fromEntries(libraries.map((library) => [getLibraryKey(library), library]));
function getLibraryKey({ path, name }: { path: string; name: string }): string {
return `${path}:${name}`;
}

export function getLibraryKey({ path, name }: { path: string; name: string }): string {
return `${path}:${name}`;
type LibraryCache = {
[key: string]: Library & {
address?: {
[deployer: Address]: Address;
};
};
};

export function getLibraryMap(libraries: readonly Library[]): LibraryMap {
const cache: LibraryCache = Object.fromEntries(libraries.map((library) => [getLibraryKey(library), library]));
const libraryMap = {
getAddress: ({ path, name, deployer }) => {
const library = cache[getLibraryKey({ path, name })];
if (!library) {
throw new Error(`Could not find library for bytecode placeholder ${path}:${name}`);
}
library.address ??= {};
// Store the prepared address in the library cache to avoid preparing the same library twice
library.address[deployer] ??= library.prepareDeploy(deployer, libraryMap).address;
return library.address[deployer];
},
} satisfies LibraryMap;
return libraryMap;
}

0 comments on commit 701b78a

Please sign in to comment.