Skip to content

Commit

Permalink
feat: refactor getBuildArtifact
Browse files Browse the repository at this point in the history
  • Loading branch information
juliopavila committed Sep 6, 2024
1 parent 88ffc63 commit f8bbc29
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/artifact/internal/getBuildArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ export function resolveLinksInBytecode(
mastercopies: Record<string, Record<string, MastercopyArtifact>>
): string {
let bytecode = artifact.bytecode;

for (const libraryPath of Object.keys(artifact.linkReferences)) {
for (const libraryName of Object.keys(
artifact.linkReferences[libraryPath]
)) {
console.log(`libraryPath ${libraryPath} libraryName ${libraryName}`);

if (
!mastercopies[libraryName] ||
!mastercopies[libraryName][contractVersion]
Expand All @@ -71,21 +73,41 @@ export function resolveLinksInBytecode(
`Could not link ${libraryName} for ${artifact.contractName}`
);
}
const { address: libraryAddress } =

let { address: libraryAddress } =
mastercopies[libraryName][contractVersion];

libraryAddress = libraryAddress.toLowerCase().replace(/^0x/, "");

if (libraryAddress.length !== 40) {
throw new Error(`Invalid library address: ${libraryAddress}`);
}

for (const { length, start } of artifact.linkReferences[libraryPath][
libraryName
]) {
const left = bytecode.slice(0, start);
const right = bytecode.slice(start + length);
bytecode = `${left}${libraryAddress.slice(2).toLowerCase()}${right}`;
console.log(`start ${start} length ${length}`);
if (length !== 20) {
throw new Error(
`Library reference length mismatch: expected 20, got ${length}`
);
}

const bytecodeArray = bytecode.split("");
const addressArray = libraryAddress.split("");

for (let i = 0; i < addressArray.length; i++) {
bytecodeArray[start * 2 + i] = addressArray[i];
}

bytecode = bytecodeArray.join("");
console.log(
`Replaced library reference at ${start} with address ${libraryAddress}`
);
}
}
}

return bytecode;
return bytecode.replace("__", "");
}

/**
Expand Down

0 comments on commit f8bbc29

Please sign in to comment.