Skip to content

Commit

Permalink
Merge pull request #152 from 1inch/feature/patch-deploy-create3
Browse files Browse the repository at this point in the history
[SC-1242] skipIfAlreadyDeployed in deploy method via create3
  • Loading branch information
zZoMROT authored Aug 21, 2024
2 parents 1bb2620 + 3d02e71 commit 98f381f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 11 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeployContractOptions, 'deployer' | 'skipIfAlreadyDeployed'> {
interface DeployContractOptionsWithCreate3 extends Omit<DeployContractOptions, 'deployer'> {
txSigner?: Wallet | SignerWithAddress,
create3Deployer: string,
salt: string,
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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(
Expand Down
27 changes: 27 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -241,6 +242,31 @@ 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,
skipIfAlreadyDeployed: false,
});
const token2 = await deployAndGetContractWithCreate3({
create3Deployer: await create3Deployer.getAddress(),
salt,
contractName: 'TokenMock',
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
});

describe('saveContractWithCreate3Deployment', function () {
Expand All @@ -255,6 +281,7 @@ describe('utils', function () {
constructorArgs: [tokenName, 'STM'],
deployments,
skipVerify: true,
skipIfAlreadyDeployed: false,
});

await saveContractWithCreate3Deployment(
Expand Down

0 comments on commit 98f381f

Please sign in to comment.