Skip to content

Commit

Permalink
Add NonceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Sep 21, 2023
1 parent 4edf075 commit 3a9281c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Manifest, hashBytecode} from "@openzeppelin/upgrades-core";
import {artifacts, ethers} from "hardhat";
import {NonceProvider} from "./nonceProvider";
import {SkaleManifestData} from "./types/SkaleManifestData";
import {promises as fs} from "fs";
import {getLibrariesNames} from "./contractFactory";
Expand All @@ -9,23 +10,31 @@ interface LibraryArtifacts {
[key: string]: unknown
}

const deployLibrary = async (libraryName: string, nonce: number) => {
const deployLibrary = async (
libraryName: string,
nonceProvider: NonceProvider
) => {
const Library = await ethers.getContractFactory(libraryName);
const library = await Library.deploy({nonce});
const library = await Library.
deploy({"nonce": nonceProvider.reserveNonce()});
await library.deployed();
return library.address;
};

export const deployLibraries = async (libraryNames: string[]) => {
export const deployLibraries = async (
libraryNames: string[],
nonceProvider?: NonceProvider
) => {
const [deployer] = await ethers.getSigners();
const nonce = await deployer.getTransactionCount();
const initializedNonceProvider = nonceProvider ??
await NonceProvider.createForWallet(deployer);
const libraries = new Map<string, string>();

(await Promise.all(libraryNames.map((libraryName, index) => (async () => [
(await Promise.all(libraryNames.map((libraryName) => (async () => [
libraryName,
await deployLibrary(
libraryName,
nonce + index
initializedNonceProvider
)
])()))).forEach(([
libraryName,
Expand Down
19 changes: 19 additions & 0 deletions src/nonceProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Signer} from "ethers";

export class NonceProvider {
currentNonce: number;

constructor (nonce: number) {
this.currentNonce = nonce;
}

static async createForWallet (signer: Signer) {
return new NonceProvider(await signer.getTransactionCount());
}

reserveNonce () {
const nonce = this.currentNonce;
this.currentNonce += 1;
return nonce;
}
}

0 comments on commit 3a9281c

Please sign in to comment.