Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Deployments #273

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
10 changes: 6 additions & 4 deletions packages/plugin-hardhat/src/deploy-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {

import { getProxyFactory, getProxyAdminFactory } from './proxy-factory';
import { readValidations } from './validations';
import { deploy } from './utils/deploy';
import { defaultDeploy, TxExecutor } from './utils/deploy';

export interface DeployFunction {
(ImplFactory: ContractFactory, args?: unknown[], opts?: DeployOptions): Promise<Contract>;
Expand All @@ -22,6 +22,7 @@ export interface DeployFunction {

export interface DeployOptions extends ValidationOptions {
initializer?: string | false;
executor?: TxExecutor;
}

export function makeDeployProxy(hre: HardhatRuntimeEnvironment): DeployFunction {
Expand All @@ -35,6 +36,7 @@ export function makeDeployProxy(hre: HardhatRuntimeEnvironment): DeployFunction
args = [];
}

const deploy = opts.executor || defaultDeploy;
const { provider } = hre.network;
const validations = await readValidations(hre);

Expand All @@ -43,17 +45,17 @@ export function makeDeployProxy(hre: HardhatRuntimeEnvironment): DeployFunction
assertUpgradeSafe(validations, version, opts);

const impl = await fetchOrDeploy(version, provider, async () => {
const deployment = await deploy(ImplFactory);
const deployment = await deploy(ImplFactory, []);
const layout = getStorageLayout(validations, version);
return { ...deployment, layout };
});

const AdminFactory = await getProxyAdminFactory(hre, ImplFactory.signer);
const adminAddress = await fetchOrDeployAdmin(provider, () => deploy(AdminFactory));
const adminAddress = await fetchOrDeployAdmin(provider, async () => await deploy(AdminFactory, []));

const data = getInitializerData(ImplFactory, args, opts.initializer);
const ProxyFactory = await getProxyFactory(hre, ImplFactory.signer);
const proxy = await ProxyFactory.deploy(impl, adminAddress, data);
const proxy = await deploy(ProxyFactory, [impl, adminAddress, data]);

const inst = ImplFactory.attach(proxy.address);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
15 changes: 10 additions & 5 deletions packages/plugin-hardhat/src/upgrade-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,32 @@ import {

import { getProxyAdminFactory } from './proxy-factory';
import { readValidations } from './validations';
import { deploy } from './utils/deploy';
import { defaultDeploy, TxExecutor } from './utils/deploy';

export interface UpgradeOptions extends ValidationOptions {
executor?: TxExecutor;
}

export type PrepareUpgradeFunction = (
proxyAddress: string,
ImplFactory: ContractFactory,
opts?: ValidationOptions,
opts?: UpgradeOptions,
) => Promise<string>;

export type UpgradeFunction = (
proxyAddress: string,
ImplFactory: ContractFactory,
opts?: ValidationOptions,
opts?: UpgradeOptions,
) => Promise<Contract>;

async function prepareUpgradeImpl(
hre: HardhatRuntimeEnvironment,
manifest: Manifest,
proxyAddress: string,
ImplFactory: ContractFactory,
opts: ValidationOptions,
opts: UpgradeOptions,
): Promise<string> {
const deploy = opts.executor || defaultDeploy;
const { provider } = hre.network;
const validations = await readValidations(hre);

Expand All @@ -51,7 +56,7 @@ async function prepareUpgradeImpl(
assertStorageUpgradeSafe(deployment.layout, layout, opts.unsafeAllowCustomTypes);

return await fetchOrDeploy(version, provider, async () => {
const deployment = await deploy(ImplFactory);
const deployment = await deploy(ImplFactory, []);
return { ...deployment, layout };
});
}
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-hardhat/src/utils/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { Deployment } from '@openzeppelin/upgrades-core';
import type { ContractFactory } from 'ethers';
import type { Deployment } from '@openzeppelin/upgrades-core';
export interface TxExecutor {
(factory: ContractFactory, args: unknown[]): Promise<Deployment>;
}

export async function deploy(factory: ContractFactory): Promise<Deployment> {
const { address, deployTransaction } = await factory.deploy();
export async function defaultDeploy(factory: ContractFactory, args: unknown[]): Promise<Deployment> {
const { address, deployTransaction } = await factory.deploy(...args);
const txHash = deployTransaction.hash;
return { address, txHash };
}