Skip to content

Commit

Permalink
Add createFactoryAddress option for Defender (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau authored Nov 13, 2023
1 parent 1be0edd commit 6b92cb0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
6 changes: 6 additions & 0 deletions packages/plugin-hardhat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

- Add `createFactoryAddress` option for Defender deployments. ([#920](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/920))

**Note**: OpenZeppelin Defender deployments is in beta and its functionality is subject to change.

## 2.3.3 (2023-10-12)

- Update OpenZeppelin Defender deployments to use Defender SDK ([#888](https://github.com/OpenZeppelin/openzeppelin-upgrades/issues/888))
Expand Down
32 changes: 20 additions & 12 deletions packages/plugin-hardhat/src/defender/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CompilerInput, CompilerOutputContract, HardhatRuntimeEnvironment } from

import { parseFullyQualifiedName } from 'hardhat/utils/contract-names';

import { DeploymentResponse, SourceCodeLicense } from '@openzeppelin/defender-sdk-deploy-client';
import { DeploymentResponse, SourceCodeLicense, DeployContractRequest } from '@openzeppelin/defender-sdk-deploy-client';
import {
Deployment,
RemoteDeploymentId,
Expand Down Expand Up @@ -54,6 +54,11 @@ type CompilerOutputWithMetadata = CompilerOutputContract & {
metadata?: string;
};

type DeployRequest = DeployContractRequest & {
// TODO: remove this when defender-sdk-deploy-client dependency is updated
createFactoryAddress?: string;
};

export async function defenderDeploy(
hre: HardhatRuntimeEnvironment,
factory: ContractFactory,
Expand All @@ -80,19 +85,22 @@ export async function defenderDeploy(
debug(`Salt: ${opts.salt}`);
}

const deploymentRequest: DeployRequest = {
contractName: contractInfo.contractName,
contractPath: contractInfo.sourceName,
network: network,
artifactPayload: JSON.stringify(contractInfo.buildInfo),
licenseType: license as SourceCodeLicense | undefined, // cast without validation but catch error from API below
constructorInputs: constructorArgs,
verifySourceCode: verifySourceCode,
relayerId: opts.relayerId,
salt: opts.salt,
createFactoryAddress: opts.createFactoryAddress,
};

let deploymentResponse: DeploymentResponse;
try {
deploymentResponse = await client.deployContract({
contractName: contractInfo.contractName,
contractPath: contractInfo.sourceName,
network: network,
artifactPayload: JSON.stringify(contractInfo.buildInfo),
licenseType: license as SourceCodeLicense | undefined, // cast without validation but catch error from API below
constructorInputs: constructorArgs,
verifySourceCode: verifySourceCode,
relayerId: opts.relayerId,
salt: opts.salt,
});
deploymentResponse = await client.deployContract(deploymentRequest);
} catch (e: any) {
if (e.response?.data?.message?.includes('licenseType should be equal to one of the allowed values')) {
throw new UpgradesError(
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-hardhat/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type DefenderDeployOptions = DefenderDeploy & {
verifySourceCode?: boolean;
relayerId?: string;
salt?: string;
createFactoryAddress?: string;
};

/**
Expand Down
36 changes: 36 additions & 0 deletions packages/plugin-hardhat/test/defender-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const TX_RESPONSE = 'mocked response';
const ETHERSCAN_API_KEY = 'fakeKey';
const RELAYER_ID = '123-abc';
const SALT = 'customsalt';
const CREATE_FACTORY = '0x0000000000000000000000000000000000000010';

const LOGIC_ADDRESS = '0x0000000000000000000000000000000000000003';
const ADMIN_ADDRESS = '0x0000000000000000000000000000000000000004';
Expand Down Expand Up @@ -99,6 +100,7 @@ test('calls defender deploy', async t => {
verifySourceCode: true,
relayerId: undefined,
salt: undefined,
createFactoryAddress: undefined,
});

assertResult(t, result);
Expand All @@ -124,6 +126,7 @@ test('calls defender deploy with relayerId', async t => {
verifySourceCode: true,
relayerId: RELAYER_ID,
salt: undefined,
createFactoryAddress: undefined,
});

assertResult(t, result);
Expand All @@ -149,6 +152,33 @@ test('calls defender deploy with salt', async t => {
verifySourceCode: true,
relayerId: undefined,
salt: SALT,
createFactoryAddress: undefined,
});

assertResult(t, result);
});

test('calls defender deploy with createFactoryAddress', async t => {
const { spy, deploy, fakeHre, fakeChainId } = t.context;

const contractPath = 'contracts/Greeter.sol';
const contractName = 'Greeter';

const factory = await ethers.getContractFactory(contractName);
const result = await deploy.defenderDeploy(fakeHre, factory, { createFactoryAddress: CREATE_FACTORY });

const buildInfo = await hre.artifacts.getBuildInfo(`${contractPath}:${contractName}`);
sinon.assert.calledWithExactly(spy, {
contractName: contractName,
contractPath: contractPath,
network: fakeChainId,
artifactPayload: JSON.stringify(buildInfo),
licenseType: 'None',
constructorInputs: [],
verifySourceCode: true,
relayerId: undefined,
salt: undefined,
createFactoryAddress: CREATE_FACTORY,
});

assertResult(t, result);
Expand All @@ -174,6 +204,7 @@ test('calls defender deploy with license', async t => {
verifySourceCode: true,
relayerId: undefined,
salt: undefined,
createFactoryAddress: undefined,
});

assertResult(t, result);
Expand All @@ -199,6 +230,7 @@ test('calls defender deploy with constructor args', async t => {
verifySourceCode: true,
relayerId: undefined,
salt: undefined,
createFactoryAddress: undefined,
});

assertResult(t, result);
Expand All @@ -224,6 +256,7 @@ test('calls defender deploy with verify false', async t => {
verifySourceCode: false,
relayerId: undefined,
salt: undefined,
createFactoryAddress: undefined,
});

assertResult(t, result);
Expand All @@ -249,6 +282,7 @@ test('calls defender deploy with ERC1967Proxy', async t => {
verifySourceCode: true,
relayerId: undefined,
salt: undefined,
createFactoryAddress: undefined,
});
});

Expand All @@ -272,6 +306,7 @@ test('calls defender deploy with BeaconProxy', async t => {
verifySourceCode: true,
relayerId: undefined,
salt: undefined,
createFactoryAddress: undefined,
});
});

Expand All @@ -295,5 +330,6 @@ test('calls defender deploy with TransparentUpgradeableProxy', async t => {
verifySourceCode: true,
relayerId: undefined,
salt: undefined,
createFactoryAddress: undefined,
});
});

0 comments on commit 6b92cb0

Please sign in to comment.