-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove functions with many statements
- Loading branch information
1 parent
f42b46c
commit 1e6d43f
Showing
6 changed files
with
250 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.