Skip to content

Commit

Permalink
Remove functions with many statements
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Sep 14, 2023
1 parent f42b46c commit 1e6d43f
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 88 deletions.
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = {
"never"
],

"max-statements": "warn",
"multiline-comment-style": "warn",
"no-await-in-loop": "warn",
"no-console": "warn",
Expand Down
109 changes: 109 additions & 0 deletions src/contractFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {artifacts, ethers} from "hardhat";
import {promises as fs} from "fs";
import {hashBytecode} from "@openzeppelin/upgrades-core";
import {LinkReferences} from "hardhat/types";
import {SkaleManifestData} from "./types/SkaleManifestData";
import {
deployLibraries,
getLinkedContractFactory,
getManifestFile
} from "./deploy";


const getSkaleManifest = async () => {
const manifest = JSON.parse(await fs.readFile(
await getManifestFile(),
"utf-8"
));
if (manifest.libraries === undefined) {
manifest.libraries = {};
}
return manifest as SkaleManifestData;
};

const updateManifest = async (
manifest: SkaleManifestData,
libraries: Map<string, string>,
oldLibraries: {[k: string]: string}
) => {
for (const [
libraryName,
libraryAddress
] of libraries.entries()) {
const {bytecode} = await artifacts.readArtifact(libraryName);
manifest.libraries[libraryName] = {
"address": libraryAddress,
"bytecodeHash": hashBytecode(bytecode)
};
}
Object.assign(
libraries,
oldLibraries
);
await fs.writeFile(
await getManifestFile(),
JSON.stringify(
manifest,
null,
4
)
);
};

export const getContractFactoryAndUpdateManifest = async (contract: string) => {
const {linkReferences} = await artifacts.readArtifact(contract);
if (!Object.keys(linkReferences).length) {
return await ethers.getContractFactory(contract);
}

const manifest = await getSkaleManifest();

const {
librariesToUpgrade,
oldLibraries
} = await getLibrariesToUpgrade(
manifest,
linkReferences
);
const libraries = await deployLibraries(librariesToUpgrade);
await updateManifest(
manifest,
libraries,
oldLibraries
);
return await getLinkedContractFactory(
contract,
libraries
);
};

const getLibrariesNames =
(linkReferences: LinkReferences) => Object.values(linkReferences).
map((libraryObject) => Object.keys(libraryObject)[0]);


const getLibrariesToUpgrade = async (
manifest: SkaleManifestData,
linkReferences: LinkReferences
) => {
const librariesToUpgrade = [];
const oldLibraries: {[k: string]: string} = {};
for (const libraryName of getLibrariesNames(linkReferences)) {
const {bytecode} = await artifacts.readArtifact(libraryName);
if (manifest.libraries[libraryName] === undefined) {
librariesToUpgrade.push(libraryName);
} else if (
hashBytecode(bytecode) !== manifest.libraries[libraryName].
bytecodeHash
) {
librariesToUpgrade.push(libraryName);
} else {
oldLibraries[libraryName] =
manifest.libraries[libraryName].address;
}
}
return {
librariesToUpgrade,
oldLibraries
};
};
37 changes: 23 additions & 14 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Manifest, hashBytecode} from "@openzeppelin/upgrades-core";
import {artifacts, ethers} from "hardhat";
import {promises as fs} from "fs";
import {SkaleManifestData} from "./types/SkaleManifestData";
import {Artifact} from "hardhat/types";
import {Artifact, LinkReferences} from "hardhat/types";

interface LibraryArtifacts {
[key: string]: unknown
Expand Down Expand Up @@ -99,19 +99,7 @@ const updateManifest = async (libraryArtifacts: LibraryArtifacts) => {
);
};

export const getContractFactory = async (contract: string) => {
const {linkReferences} = await artifacts.readArtifact(contract);
if (!Object.keys(linkReferences).length) {
return await ethers.getContractFactory(contract);
}

const libraryNames = [];
for (const key of Object.keys(linkReferences)) {
const libraryName = Object.keys(linkReferences[key])[0];
libraryNames.push(libraryName);
}

const libraries = await deployLibraries(libraryNames);
const getLibraryArtifacts = async (libraries: Map<string, string>) => {
const libraryArtifacts: LibraryArtifacts = {};
for (const [
libraryName,
Expand All @@ -123,6 +111,27 @@ export const getContractFactory = async (contract: string) => {
"bytecodeHash": hashBytecode(bytecode)
};
}
return libraryArtifacts;
};

const getLibraryNames = (linkReferences: LinkReferences) => {
const libraryNames = [];
for (const key of Object.keys(linkReferences)) {
const libraryName = Object.keys(linkReferences[key])[0];
libraryNames.push(libraryName);
}
return libraryNames;
};

export const getContractFactory = async (contract: string) => {
const {linkReferences} = await artifacts.readArtifact(contract);
if (!Object.keys(linkReferences).length) {
return await ethers.getContractFactory(contract);
}

const libraryNames = getLibraryNames(linkReferences);
const libraries = await deployLibraries(libraryNames);
const libraryArtifacts = await getLibraryArtifacts(libraries);

await updateManifest(libraryArtifacts);

Expand Down
2 changes: 1 addition & 1 deletion src/types/SkaleManifestData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ManifestData} from "@openzeppelin/upgrades-core";

export interface SkaleManifestData extends ManifestData {
libraries?: {
libraries: {
[libraryName: string]: {
address: string
bytecodeHash: string
Expand Down
113 changes: 68 additions & 45 deletions src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,54 @@ export abstract class Upgrader {
// Public

async upgrade () {
const proxyAdmin = await getManifestAdmin(hre) as unknown as ProxyAdmin;
const version = await this.prepareVersion();

const version = await getVersion();
await this.checkVersion(version);
console.log(`Will mark updated version as ${version}`);

if (this.deployNewContracts !== undefined) {
// Deploy new contracts
await this.deployNewContracts();
}
await this.callDeployNewContracts();

const contractsToUpgrade = await this.deployNewImplementations();

this.switchToNewImplementations(
contractsToUpgrade,
proxyAdmin
await getManifestAdmin(hre) as unknown as ProxyAdmin
);

await this.callInitialize();

// Write version
await this.setVersion(version);

await this.writeTransactions(version);

await this.submitter.submit(this.transactions);

await Upgrader.verify(contractsToUpgrade);

console.log("Done");
}

// Private

private async callInitialize () {
if (this.initialize !== undefined) {
await this.initialize();
}
}

// Write version
await this.setVersion(version);
private async callDeployNewContracts () {
if (this.deployNewContracts !== undefined) {
// Deploy new contracts
await this.deployNewContracts();
}
}

private async prepareVersion () {
const version = await getVersion();
await this.checkVersion(version);
console.log(`Will mark updated version as ${version}`);
return version;
}

private async writeTransactions (version: string) {
await fs.writeFile(
`data/transactions-${version}-${network.name}.json`,
JSON.stringify(
Expand All @@ -107,16 +130,8 @@ export abstract class Upgrader {
4
)
);

await this.submitter.submit(this.transactions);

await Upgrader.verify(contractsToUpgrade);

console.log("Done");
}

// Private

private static async verify (contractsToUpgrade: ContractToUpgrade[]) {
if (process.env.NO_VERIFY) {
console.log("Skip verification");
Expand Down Expand Up @@ -158,36 +173,44 @@ export abstract class Upgrader {
private async deployNewImplementations () {
const contractsToUpgrade: ContractToUpgrade[] = [];
for (const contract of this.contractNamesToUpgrade) {
const contractFactory =
const updatedContract =
await this.deployNewImplementation(contract);
if (updatedContract !== undefined) {
contractsToUpgrade.push(updatedContract);
}
}
return contractsToUpgrade;
}

private async deployNewImplementation (contract: string) {
const contractFactory =
await getContractFactoryAndUpdateManifest(contract);
const proxyAddress =
const proxyAddress =
(await this.instance.getContract(contract)).address;

console.log(`Prepare upgrade of ${contract}`);
const
currentImplementationAddress = await getImplementationAddress(
network.provider,
proxyAddress
);
const newImplementationAddress = await upgrades.prepareUpgrade(
proxyAddress,
contractFactory,
{
"unsafeAllowLinkedLibraries": true,
"unsafeAllowRenames": true
}
) as string;
if (newImplementationAddress !== currentImplementationAddress) {
contractsToUpgrade.push({
proxyAddress,
"implementationAddress": newImplementationAddress,
"name": contract
});
} else {
console.log(chalk.gray(`Contract ${contract} is up to date`));
console.log(`Prepare upgrade of ${contract}`);
const
currentImplementationAddress = await getImplementationAddress(
network.provider,
proxyAddress
);
const newImplementationAddress = await upgrades.prepareUpgrade(
proxyAddress,
contractFactory,
{
"unsafeAllowLinkedLibraries": true,
"unsafeAllowRenames": true
}
) as string;
if (newImplementationAddress !== currentImplementationAddress) {
return {
proxyAddress,
"implementationAddress": newImplementationAddress,
"name": contract
};
}
return contractsToUpgrade;
console.log(chalk.gray(`Contract ${contract} is up to date`));
return undefined;
}

private async getNormalizedDeployedVersion () {
Expand Down
Loading

0 comments on commit 1e6d43f

Please sign in to comment.