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

Add skale-contracts #233

Merged
merged 45 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
92385a2
Update eslint config
DimaStebaev Sep 13, 2023
9a171c5
Turn on all checks
DimaStebaev Sep 13, 2023
f145eab
Use this in class methods
DimaStebaev Sep 13, 2023
4f18e9a
Use return consistently
DimaStebaev Sep 13, 2023
e01af2d
Use triple =
DimaStebaev Sep 13, 2023
ed9477e
Use function expressions
DimaStebaev Sep 13, 2023
8a0dea2
Use long identifiers
DimaStebaev Sep 13, 2023
b57fb4d
Init on declarations
DimaStebaev Sep 13, 2023
0500b8a
Change comment line position
DimaStebaev Sep 13, 2023
cb25bc1
Configure lines-around-comment
DimaStebaev Sep 13, 2023
3790f0b
Reduce max depth
DimaStebaev Sep 13, 2023
0874959
Remove long lines
DimaStebaev Sep 13, 2023
936ca5f
Remove long functions
DimaStebaev Sep 14, 2023
f42b46c
Remove too many parameters
DimaStebaev Sep 14, 2023
1e6d43f
Remove functions with many statements
DimaStebaev Sep 14, 2023
1f1bca3
Use consistent multiline comments
DimaStebaev Sep 14, 2023
bf9a9c1
Stop using await in loops
DimaStebaev Sep 15, 2023
b647ec2
Stop using continue
DimaStebaev Sep 18, 2023
3fdc4d4
Remove duplicate imports
DimaStebaev Sep 18, 2023
144841c
Stop using magic numbers
DimaStebaev Sep 18, 2023
edb7f37
Add exit codes
DimaStebaev Sep 18, 2023
23f7484
Remove negated conditions
DimaStebaev Sep 18, 2023
6fd5b75
Stop using ternary operator
DimaStebaev Sep 18, 2023
465669c
Stop using undefined value
DimaStebaev Sep 18, 2023
4f9982b
Stop using underscore dangle
DimaStebaev Sep 18, 2023
3683226
Stop using functions before define
DimaStebaev Sep 18, 2023
88ef07f
Set radix
DimaStebaev Sep 18, 2023
22240bf
Sort imports
DimaStebaev Sep 18, 2023
9784366
Sort keys
DimaStebaev Sep 18, 2023
b689987
Replace getLinkedContractFactory with ethers implementation
DimaStebaev Sep 19, 2023
ddada38
Simplify Upgrader interface
DimaStebaev Sep 20, 2023
4edf075
Fix issue with this
DimaStebaev Sep 20, 2023
3a9281c
Add NonceProvider
DimaStebaev Sep 21, 2023
9427935
Set nonce prepareUpgrade
DimaStebaev Sep 21, 2023
aede3cf
Deploy implementations sequentially
DimaStebaev Sep 25, 2023
6b7b9ec
Fix nonce issue
DimaStebaev Sep 25, 2023
0c65c7d
Update skale-contracts
DimaStebaev Oct 12, 2023
1559b4a
Merge with develop
DimaStebaev Jun 17, 2024
34f964b
Move to ethers v6
DimaStebaev Jun 21, 2024
9fab9c2
Fix networks
DimaStebaev Jul 8, 2024
fb5553e
Add semaphore
DimaStebaev Jul 9, 2024
2ede5c8
Fix protection
DimaStebaev Jul 9, 2024
b87cd5b
Reduce the limit
DimaStebaev Jul 10, 2024
aa9ee61
Fix nonce issue
DimaStebaev Jul 10, 2024
0cf5b3b
Remove simultaneous deployments
DimaStebaev Jul 22, 2024
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
45 changes: 30 additions & 15 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
/* eslint-env node */
module.exports = {
extends: [
// 'eslint:all',
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
"extends": [
"eslint:all",
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
rules: {
"object-curly-spacing": [ "error", "always" ],
"padded-blocks": [ "error", "never" ],
"one-var": ["error", "consecutive"]
},
ignorePatterns: [
"ignorePatterns": [
"dist/**",
"typechain-types/**"
]
};
],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"root": true,
"rules": {
"@typescript-eslint/no-shadow": "error",
"lines-around-comment": [
"error",
{"allowBlockStart": true}
],
"no-console": "off",
// Replaced with @typescript-eslint/no-shadow
"no-shadow": "off",
"no-warning-comments": "warn",
"object-curly-spacing": "error",
"one-var": [
"error",
"never"
],
"padded-blocks": [
"error",
"never"
]
}
};
5 changes: 3 additions & 2 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"**/*.egg-info/**",
"**/dist/**",
"**/coverage.json",
"**/*.hbs"
"**/*.hbs",
"**/*.orig"
],
"dictionaries": ["domain-terms", "libraries", "names", "skale-terms", "solidity"],
"dictionaryDefinitions": [
Expand All @@ -27,4 +28,4 @@
{ "name": "names", "path": "./dictionaries/names.txt"},
{ "name": "solidity", "path": "./dictionaries/solidity.txt"}
]
}
}
16 changes: 10 additions & 6 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { HardhatUserConfig } from "hardhat/config";
import '@typechain/hardhat'
import "@nomiclabs/hardhat-ethers";
import "@typechain/hardhat";
import "@nomicfoundation/hardhat-ethers";
import "@openzeppelin/hardhat-upgrades";
import {HardhatUserConfig} from "hardhat/config";


const coreArtifacts =
"node_modules/@openzeppelin/upgrades-core/artifacts/[!b]*.json";

const config: HardhatUserConfig = {
typechain: {
target: "ethers-v5",
externalArtifacts: ['node_modules/@openzeppelin/upgrades-core/artifacts/[!b]*.json']
"typechain": {
"externalArtifacts": [coreArtifacts],
"target": "ethers-v6"
}
};

Expand Down
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,34 @@
"devDependencies": {
"@openzeppelin/contracts-upgradeable": "^4.4.2",
"@tsconfig/recommended": "^1.0.2",
"@typechain/ethers-v5": "^9.0.0",
"@typechain/hardhat": "^8.0.0",
"@typechain/ethers-v6": "^0.5.1",
"@typechain/hardhat": "^9.1.0",
"@types/node": "^20.6.0",
"@typescript-eslint/eslint-plugin": "^6.6.0",
"@typescript-eslint/parser": "^6.6.0",
"cspell": "^8.8.3",
"eslint": "^8.15.0",
"install-peers-cli": "^2.2.0",
"ts-node": "^10.5.0",
"typechain": "^8.2.0",
"typechain": "^8.3.2",
"typescript": "^5.1.6"
},
"dependencies": {
"@safe-global/api-kit": "^1.3.0",
"@safe-global/protocol-kit": "^1.2.0",
"@safe-global/safe-core-sdk-types": "^2.2.0",
"@skalenetwork/skale-contracts-ethers-v5": "0.1.0-develop.0",
"@safe-global/api-kit": "^2.4.1",
"@safe-global/protocol-kit": "^4.0.1",
"@safe-global/safe-core-sdk-types": "^5.0.1",
"@skalenetwork/skale-contracts-ethers-v6": "^1.0.0",
"axios": "^1.4.0",
"ethereumjs-util": "^7.1.4"
"ethereumjs-util": "^7.1.4",
"semaphore-async-await": "^1.5.1"
},
"peerDependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.0",
"@nomicfoundation/hardhat-verify": "^1.1.1",
"@nomiclabs/hardhat-ethers": "^2.0.4",
"@openzeppelin/hardhat-upgrades": "^1.14.0",
"@openzeppelin/hardhat-upgrades": "^3.1.1",
"@openzeppelin/upgrades-core": "^1.27.1",
"@types/mocha": "^9.1.0",
"ethers": "^5.7.2",
"hardhat": "^2.16.1"
"ethers": "^6.1.0",
"hardhat": "^2.9.9"
}
}
25 changes: 18 additions & 7 deletions src/abi.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { Interface } from "ethers/lib/utils";
import {Interface} from "ethers";

export function getAbi(contractInterface: Interface) {
const abi = JSON.parse(contractInterface.format("json") as string) as [];
export const getAbi = (contractInterface: Interface) => {
const abi = JSON.parse(contractInterface.formatJson()) as [];

abi.forEach((obj: {type: string}) => {
if (obj.type === "function") {
const func = obj as {name: string, type: string, inputs: object[], outputs: object[]};
const func = obj as {
name: string,
type: string,
inputs: object[],
outputs: object[]
};
func.inputs.concat(func.outputs).forEach((output: object) => {
Object.assign(output, Object.assign({ name: "" }, output));
})
Object.assign(
output,
{
"name": "",
...output
}
);
});
}
});

return abi;
}
};
142 changes: 142 additions & 0 deletions src/contractFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import {artifacts, ethers} from "hardhat";
import {
deployLibraries,
getManifestFile
} from "./deploy";
import {LinkReferences} from "hardhat/types";
import {NonceProvider} from "./nonceProvider";
import {SkaleManifestData} from "./types/SkaleManifestData";
import {promises as fs} from "fs";
import {hashBytecode} from "@openzeppelin/upgrades-core";


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

const loadBytesCodes = async (libraryNames: string[]) => {
const byteCodes = new Map<string, string>();

(await Promise.
all(libraryNames.map((libraryName) => (async () => {
const {bytecode} = await artifacts.readArtifact(libraryName);
return [
libraryName,
bytecode
];
})()))).forEach(([
libraryName,
bytecode
]) => {
byteCodes.set(
libraryName,
bytecode
);
});
return byteCodes;
};

const updateManifest = async (
manifest: SkaleManifestData,
libraries: Map<string, string>,
oldLibraries: {[k: string]: string}
) => {
const byteCodes = await loadBytesCodes(Array.from(libraries.keys()));
for (const [
libraryName,
libraryAddress
] of libraries.entries()) {
manifest.libraries[libraryName] = {
"address": libraryAddress,
"bytecodeHash": hashBytecode(byteCodes.get(libraryName) as string)
};
}
Object.assign(
libraries,
oldLibraries
);
const indentation = 4;
await fs.writeFile(
await getManifestFile(),
JSON.stringify(
manifest,
null,
indentation
)
);
};

export const getLibrariesNames = (linkReferences: LinkReferences) => {
const libraryNames = [];
for (const libraryFile of Object.values(linkReferences)) {
libraryNames.push(...Object.keys(libraryFile));
}
return libraryNames;
};

const getLibrariesToUpgrade = async (
manifest: SkaleManifestData,
linkReferences: LinkReferences
) => {
const librariesToUpgrade = [];
const oldLibraries: {[k: string]: string} = {};
const librariesNames = getLibrariesNames(linkReferences);
const byteCodes = await loadBytesCodes(librariesNames);
for (const libraryName of librariesNames) {
if (typeof manifest.libraries[libraryName] === "undefined") {
librariesToUpgrade.push(libraryName);
} else if (
hashBytecode(byteCodes.get(libraryName) as string) ===
manifest.libraries[libraryName].bytecodeHash
) {
oldLibraries[libraryName] =
manifest.libraries[libraryName].address;
} else {
librariesToUpgrade.push(libraryName);
}
}
return {
librariesToUpgrade,
oldLibraries
};
};

export const getContractFactoryAndUpdateManifest = async (
contract: string,
nonceProvider?: NonceProvider
) => {
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,
nonceProvider
);
await updateManifest(
manifest,
libraries,
oldLibraries
);
return await ethers.getContractFactory(
contract,
{"libraries": Object.fromEntries(libraries)}
);
};
Loading
Loading