Skip to content

Commit

Permalink
Fix nonce issue
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Jul 10, 2024
1 parent b87cd5b commit aa9ee61
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
24 changes: 21 additions & 3 deletions src/nonceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@ import {Signer} from "ethers";

export class NonceProvider {
currentNonce: number;
releasedNonces: number[];

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

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

reserveNonce () {
const nonce = this.currentNonce;
this.currentNonce += 1;
return nonce;
if (!this.releasedNonces) {
const nonce = this.currentNonce;
this.currentNonce += 1;
return nonce;
}
return this.releasedNonces.shift();
}

releaseNonce (nonce: number) {
if (NonceProvider.next(nonce) === this.currentNonce) {
this.currentNonce -= 1;
} else {
this.releasedNonces.push(nonce);
}
}

private static next (nonce: number) {
const nextDiff = 1;
return nonce + nextDiff;
}
}
21 changes: 16 additions & 5 deletions src/upgrader.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ContractFactory, Transaction} from "ethers";
import {Manifest, getImplementationAddress} from "@openzeppelin/upgrades-core";
import {ethers, network, upgrades} from "hardhat";
import {AutoSubmitter} from "./submitters/auto-submitter";
Expand All @@ -7,7 +8,6 @@ import {NonceProvider} from "./nonceProvider";
import {ProxyAdmin} from "../typechain-types";
import Semaphore from 'semaphore-async-await';
import {Submitter} from "./submitters/submitter";
import {Transaction} from "ethers";
import chalk from "chalk";
import {promises as fs} from "fs";
import {getContractFactoryAndUpdateManifest} from "./contractFactory";
Expand All @@ -31,7 +31,7 @@ interface Project {
const withoutNull = <T>(array: Array<T | null>) => array.
filter((element) => element !== null) as Array<T>;

const maxSimultaneousDeployments = 5;
const maxSimultaneousDeployments = 8;
// 10 minutes
const deployTimeout = 60e4;

Expand Down Expand Up @@ -221,17 +221,25 @@ export abstract class Upgrader {
(await this.instance.getContract(contract)).getAddress();

console.log(`Prepare upgrade of ${contract}`);

return this.prepareUpgrade(contract, proxyAddress, contractFactory);
}

private async prepareUpgrade(contractName: string, proxyAddress: string, contractFactory: ContractFactory) {
const currentImplementationAddress = await getImplementationAddress(
network.provider,
proxyAddress
);

const nonce = this.nonceProvider?.reserveNonce();

const newImplementationAddress = await upgrades.prepareUpgrade(
proxyAddress,
contractFactory,
{
"timeout": deployTimeout,
"txOverrides": {
"nonce": this.nonceProvider?.reserveNonce()
nonce
},
"unsafeAllowLinkedLibraries": true,
"unsafeAllowRenames": true
Expand All @@ -240,11 +248,14 @@ export abstract class Upgrader {
if (newImplementationAddress !== currentImplementationAddress) {
return {
"implementationAddress": newImplementationAddress,
"name": contract,
"name": contractName,
proxyAddress
};
}
console.log(chalk.gray(`Contract ${contract} is up to date`));
console.log(chalk.gray(`Contract ${contractName} is up to date`));
if (nonce) {
this.nonceProvider?.releaseNonce(nonce);
}
return null;
}

Expand Down

0 comments on commit aa9ee61

Please sign in to comment.