Skip to content

Commit

Permalink
Replace getLinkedContractFactory with ethers implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Sep 19, 2023
1 parent 9784366 commit b689987
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 57 deletions.
5 changes: 2 additions & 3 deletions src/contractFactory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {artifacts, ethers} from "hardhat";
import {
deployLibraries,
getLinkedContractFactory,
getManifestFile
} from "./deploy";
import {LinkReferences} from "hardhat/types";
Expand Down Expand Up @@ -129,8 +128,8 @@ export const getContractFactoryAndUpdateManifest = async (contract: string) => {
libraries,
oldLibraries
);
return await getLinkedContractFactory(
return await ethers.getContractFactory(
contract,
libraries
{"libraries": Object.fromEntries(libraries)}
);
};
56 changes: 2 additions & 54 deletions src/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {Manifest, hashBytecode} from "@openzeppelin/upgrades-core";
import {artifacts, ethers} from "hardhat";
import {hexConcat, hexDataSlice} from "ethers/lib/utils";
import {Artifact} from "hardhat/types";
import {SkaleManifestData} from "./types/SkaleManifestData";
import {promises as fs} from "fs";
import {getLibrariesNames} from "./contractFactory";
Expand Down Expand Up @@ -42,56 +40,6 @@ export const deployLibraries = async (libraryNames: string[]) => {
return libraries;
};

const firstByteIndex = 0;

const linkBytecode = (artifact: Artifact, libraries: Map<string, string>) => {
let {bytecode} = artifact;
for (const [, fileReferences] of Object.entries(artifact.linkReferences)) {
for (const [
libName,
fixups
] of Object.entries(fileReferences)) {
const libAddress = libraries.get(libName);
if (typeof libAddress !== "undefined") {
for (const fixup of fixups) {
const bytecodeBefore = hexDataSlice(
bytecode,
firstByteIndex,
fixup.start
);
const bytecodeAfter = hexDataSlice(
bytecode,
fixup.start + fixup.length
);
bytecode = hexConcat([
bytecodeBefore,
libAddress,
bytecodeAfter
]);
}
}
}
}
return bytecode;
};

export const getLinkedContractFactory = async (
contractName: string,
libraries: Map<string, string>
) => {
const
cArtifact = await artifacts.readArtifact(contractName);
const linkedBytecode = linkBytecode(
cArtifact,
libraries
);
const ContractFactory = await ethers.getContractFactory(
cArtifact.abi,
linkedBytecode
);
return ContractFactory;
};

export const getManifestFile = async function getManifestFile () {
return (await Manifest.forNetwork(ethers.provider)).file;
};
Expand Down Expand Up @@ -167,8 +115,8 @@ export const getContractFactory = async (contract: string) => {

await updateManifest(libraryArtifacts);

return await getLinkedContractFactory(
return await ethers.getContractFactory(
contract,
libraries
{"libraries": Object.fromEntries(libraries)}
);
};

0 comments on commit b689987

Please sign in to comment.