Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: suggestioin refactor bytecode resolution #17

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
juliopavila marked this conversation as resolved.
Show resolved Hide resolved
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) {
juliopavila marked this conversation as resolved.
Show resolved Hide resolved
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
Loading