From 65151608b37ab59fe373025ce8ac34646e5c5445 Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 15 Aug 2024 23:20:08 +0100 Subject: [PATCH 1/3] Add skipIfAlreadyDeployed to deployAndGetContractWithCreate3 + tests --- src/utils.ts | 12 +++++++++++- test/utils.test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 1c595bc..9a461d6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -49,7 +49,7 @@ export interface DeployContractOptions { * @param create3Deployer Address of the create3 deployer contract, which related to `contracts/interfaces/ICreate3Deployer.sol`. * @param salt Salt value for create3 deployment. */ -interface DeployContractOptionsWithCreate3 extends Omit { +interface DeployContractOptionsWithCreate3 extends Omit { txSigner?: Wallet | SignerWithAddress, create3Deployer: string, salt: string, @@ -125,6 +125,7 @@ export async function deployAndGetContract(options: DeployContractOptions): Prom * - txSigner: first signer in the environment * - deploymentName: contractName * - skipVerify: false + * - skipIfAlreadyDeployed: true * - waitConfirmations: 1 on dev chains, 6 on others * @returns The deployed contract instance. */ @@ -141,12 +142,21 @@ export async function deployAndGetContractWithCreate3( txSigner = (await ethers.getSigners())[0], deploymentName = contractName, skipVerify = false, + skipIfAlreadyDeployed = true, gasPrice, maxPriorityFeePerGas, maxFeePerGas, waitConfirmations = constants.DEV_CHAINS.includes(hre.network.name) ? 1 : 6, } = options; + const contractDeployment = await deployments.getOrNull(contractName); + if (skipIfAlreadyDeployed && contractDeployment != null && + (await deployments.getArtifact(contractName)).deployedBytecode === contractDeployment.deployedBytecode + ) { + console.log(`Contract ${contractName} is already deployed at ${contractDeployment.address}`); + return await ethers.getContractAt(contractName, contractDeployment.address); + } + const deployer = await ethers.getContractAt('ICreate3Deployer', create3Deployer) as unknown as ICreate3Deployer; const CustomContract = await ethers.getContractFactory(contractName); const deployData = (await CustomContract.getDeployTransaction( diff --git a/test/utils.test.ts b/test/utils.test.ts index ed25156..cb32851 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -231,6 +231,7 @@ describe('utils', function () { constructorArgs: [tokenName, 'STM'], deployments, skipVerify: true, + skipIfAlreadyDeployed: false, }); expect(await create3Deployer.addressOf(salt)).to.be.eq(await token.getAddress()); expect(await token.name()).to.be.eq(tokenName); @@ -241,6 +242,29 @@ describe('utils', function () { bytecode: (await hre.artifacts.readArtifact('TokenMock')).bytecode, }); }); //.timeout(200000); If this test needs to be run on a test chain, the timeout should be increased + + it('should skip if set skipIfAlreadyDeployed', async function () { + const create3Deployer = await deployContract('Create3Mock') as Create3Mock; + const salt = ethers.keccak256(ethers.toUtf8Bytes('SOME SALT HERE')); + const tokenName = 'SomeToken'; + const token1 = await deployAndGetContractWithCreate3({ + create3Deployer: await create3Deployer.getAddress(), + salt, + contractName: 'TokenMock', + constructorArgs: [tokenName, 'STM'], + deployments, + skipVerify: true, + }); + const token2 = await deployAndGetContractWithCreate3({ + create3Deployer: await create3Deployer.getAddress(), + salt, + contractName: 'TokenMock', + constructorArgs: [tokenName, 'STM'], + deployments, + skipVerify: true, + }); + expect(await token1.getAddress()).to.be.eq(await token2.getAddress()); + }); //.timeout(200000); If this test needs to be run on a test chain, the timeout should be increased }); describe('saveContractWithCreate3Deployment', function () { @@ -255,6 +279,7 @@ describe('utils', function () { constructorArgs: [tokenName, 'STM'], deployments, skipVerify: true, + skipIfAlreadyDeployed: false, }); await saveContractWithCreate3Deployment( From e88c5ff8da038b155929abdd480a1991ca145af1 Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 15 Aug 2024 23:37:53 +0100 Subject: [PATCH 2/3] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 71bb75b..4e58330 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@1inch/solidity-utils", - "version": "5.2.2", + "version": "5.2.3", "main": "dist/src/index.js", "types": "dist/src/index.d.ts", "exports": { From 3d02e7183879d37b32684342e21e9dbd4adef2d0 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 16 Aug 2024 12:35:25 +0100 Subject: [PATCH 3/3] Patch tests --- test/utils.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/utils.test.ts b/test/utils.test.ts index cb32851..1b9468c 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -254,6 +254,7 @@ describe('utils', function () { constructorArgs: [tokenName, 'STM'], deployments, skipVerify: true, + skipIfAlreadyDeployed: false, }); const token2 = await deployAndGetContractWithCreate3({ create3Deployer: await create3Deployer.getAddress(), @@ -262,6 +263,7 @@ describe('utils', function () { constructorArgs: [tokenName, 'STM'], deployments, skipVerify: true, + skipIfAlreadyDeployed: true, }); expect(await token1.getAddress()).to.be.eq(await token2.getAddress()); }); //.timeout(200000); If this test needs to be run on a test chain, the timeout should be increased