Skip to content

Commit

Permalink
refactor: renaming and commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
heueristik committed Apr 11, 2024
1 parent d55b604 commit 8248b10
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 81 deletions.
84 changes: 5 additions & 79 deletions packages/contracts/deploy/30_upgrade_repo/31a_upgrade_repo.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import {findPluginRepo, getProductionNetworkName} from '../../utils/helpers';
import {SemVer, skipUpgrade} from './_skipUpgrade';
import {
getLatestNetworkDeployment,
getNetworkNameByAlias,
} from '@aragon/osx-commons-configs';
import {
PLUGIN_REPO_PERMISSIONS,
UnsupportedNetworkError,
} from '@aragon/osx-commons-sdk';
import {PluginRepo__factory} from '@aragon/osx-ethers';
import {BytesLike} from 'ethers';
import {upgradeCommon, skipUpgrade} from './_common';
import {PLUGIN_REPO_PERMISSIONS} from '@aragon/osx-commons-sdk';
import {DeployFunction} from 'hardhat-deploy/types';
import {HardhatRuntimeEnvironment} from 'hardhat/types';

Expand All @@ -18,63 +8,8 @@ import {HardhatRuntimeEnvironment} from 'hardhat/types';
* @param {HardhatRuntimeEnvironment} hre
*/
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const [deployer] = await hre.ethers.getSigners();
const productionNetworkName: string = getProductionNetworkName(hre);
const network = getNetworkNameByAlias(productionNetworkName);
if (network === null) {
throw new UnsupportedNetworkError(productionNetworkName);
}
const networkDeployments = getLatestNetworkDeployment(network);
if (networkDeployments === null) {
throw `Deployments are not available on network ${network}.`;
}

// Get PluginRepo
const {pluginRepo, ensDomain} = await findPluginRepo(hre);
if (pluginRepo === null) {
throw `PluginRepo '${ensDomain}' does not exist yet.`;
}

console.log(
`Upgrading plugin repo '${ensDomain}' (${pluginRepo.address})...`
);

// Get the latest `PluginRepo` implementation as the upgrade target
const latestPluginRepoImplementation = PluginRepo__factory.connect(
networkDeployments.PluginRepoBase.address,
deployer
);

// Get the current OSX protocol version from the current plugin repo implementation
let current: SemVer;
try {
current = await pluginRepo.protocolVersion();
} catch {
current = [1, 0, 0];
}

// Get the OSX protocol version from the latest plugin repo implementation
const latest: SemVer = await latestPluginRepoImplementation.protocolVersion();

console.log(
`Upgrading from current protocol version v${current[0]}.${current[1]}.${current[2]} to the latest version v${latest[0]}.${latest[1]}.${latest[2]}.`
);

// NOTE: The following code can be uncommented and `initData` can be filled
// with arguments in case re-initialization of the `PluginRepo` should become necessary.
// Re-initialization will happen through a call to `function initializeFrom(uint8[3] calldata _previousProtocolVersion, bytes calldata _initData)`
// that Aragon might add to the `PluginRepo` contract once it's required.
/*
// Define the `_initData` arguments
const initData: BytesLike[] = [];
// Encode the call to `function initializeFrom(uint8[3] calldata _previousProtocolVersion, bytes calldata _initData)` with `initData`
const initializeFromCalldata =
latestPluginRepoImplementation.interface.encodeFunctionData('initializeFrom', [
current,
initData,
]);
*/
const initializeFromCalldata: BytesLike = [];
const {deployer, pluginRepo, latestPluginRepoImplementation, current} =

Check failure on line 11 in packages/contracts/deploy/30_upgrade_repo/31a_upgrade_repo.ts

View workflow job for this annotation

GitHub Actions / checks

'current' is assigned a value but never used. Allowed unused vars must match /_/u

Check failure on line 11 in packages/contracts/deploy/30_upgrade_repo/31a_upgrade_repo.ts

View workflow job for this annotation

GitHub Actions / formatting-linting / checks

'current' is assigned a value but never used. Allowed unused vars must match /_/u

Check failure on line 11 in packages/contracts/deploy/30_upgrade_repo/31a_upgrade_repo.ts

View workflow job for this annotation

GitHub Actions / formatting-linting / checks

'current' is assigned a value but never used. Allowed unused vars must match /_/u
await upgradeCommon(hre);

// Check if deployer has the permission to upgrade the plugin repo
if (
Expand All @@ -85,16 +20,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
[]
)
) {
// Use `upgradeToAndCall` if the new implementation must be re-initialized by calling
// on the `PluginRepo` proxy. If not, we use `upgradeTo`.
if (initializeFromCalldata.length > 0) {
await pluginRepo.upgradeToAndCall(
latestPluginRepoImplementation.address,
initializeFromCalldata
);
} else {
await pluginRepo.upgradeTo(latestPluginRepoImplementation.address);
}
await pluginRepo.upgradeTo(latestPluginRepoImplementation.address);
} else {
throw Error(
`The new version cannot be published because the deployer ('${deployer.address}')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {upgradeCommon as commonUpgradeSteps, skipUpgrade} from './_common';
import {PLUGIN_REPO_PERMISSIONS} from '@aragon/osx-commons-sdk';
import {BytesLike} from 'ethers';
import {DeployFunction} from 'hardhat-deploy/types';
import {HardhatRuntimeEnvironment} from 'hardhat/types';

/**
* Upgrades the plugin repo to the latest implementation and reinitializes the proxy.
* @param {HardhatRuntimeEnvironment} hre
*/
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const {deployer, pluginRepo, latestPluginRepoImplementation, current} =
await commonUpgradeSteps(hre);

// Define the `_initData` arguments
const initData: BytesLike[] = [];

// Encode the call to `function initializeFrom(uint8[3] calldata _previousProtocolVersion, bytes calldata _initData)` with `initData`.
const initializeFromCalldata =
latestPluginRepoImplementation.interface.encodeFunctionData(
// Re-initialization will happen through a call to `function initializeFrom(uint8[3] calldata _previousProtocolVersion, bytes calldata _initData)`
// that Aragon will add to the `PluginRepo` contract once it's required.
'initializeFrom',
[current, initData]
);

// Check if deployer has the permission to upgrade the plugin repo
if (
await pluginRepo.isGranted(
pluginRepo.address,
deployer.address,
PLUGIN_REPO_PERMISSIONS.UPGRADE_REPO_PERMISSION_ID,
[]
)
) {
// Use `upgradeToAndCall` to reinitialize the new `PluginRepo` implementation after the update.
if (initializeFromCalldata.length > 0) {
await pluginRepo.upgradeToAndCall(
latestPluginRepoImplementation.address,
initializeFromCalldata
);
} else {
throw Error(
`Initialization data is missing for 'upgradeToAndCall'. Stopping repo upgrade and reinitialization...`
);
}
} else {
throw Error(
`The new version cannot be published because the deployer ('${deployer.address}')
is lacking the ${PLUGIN_REPO_PERMISSIONS.UPGRADE_REPO_PERMISSION_ID} permission.`
);
}
};
export default func;
func.tags = ['UpgradeAndReinitializeRepo'];
func.skip = skipUpgrade;
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,77 @@ import {
getNetworkNameByAlias,
} from '@aragon/osx-commons-configs';
import {UnsupportedNetworkError} from '@aragon/osx-commons-sdk';
import {PluginRepo__factory} from '@aragon/osx-ethers';
import {PluginRepo, PluginRepo__factory} from '@aragon/osx-ethers';
import {SignerWithAddress} from '@nomiclabs/hardhat-ethers/signers';
import {HardhatRuntimeEnvironment} from 'hardhat/types';
import path from 'path';

export type SemVer = [number, number, number];

type Result = {
deployer: SignerWithAddress;
pluginRepo: PluginRepo;
latestPluginRepoImplementation: PluginRepo;
current: SemVer;
latest: SemVer;
};

export async function upgradeCommon(
hre: HardhatRuntimeEnvironment
): Promise<Result> {
const [deployer] = await hre.ethers.getSigners();
const productionNetworkName: string = getProductionNetworkName(hre);
const network = getNetworkNameByAlias(productionNetworkName);
if (network === null) {
throw new UnsupportedNetworkError(productionNetworkName);
}
const networkDeployments = getLatestNetworkDeployment(network);
if (networkDeployments === null) {
throw `Deployments are not available on network ${network}.`;
}

// Get PluginRepo
const {pluginRepo, ensDomain} = await findPluginRepo(hre);
if (pluginRepo === null) {
throw `PluginRepo '${ensDomain}' does not exist yet.`;
}

console.log(
`Upgrading plugin repo '${ensDomain}' (${pluginRepo.address})...`
);

// Get the latest `PluginRepo` implementation as the upgrade target
const latestPluginRepoImplementation = PluginRepo__factory.connect(
networkDeployments.PluginRepoBase.address,
deployer
);

// Get the current OSX protocol version from the current plugin repo implementation
let current: SemVer;
try {
current = await pluginRepo.protocolVersion();
} catch {
current = [1, 0, 0];
}

// Get the OSX protocol version from the latest plugin repo implementation
const latest: SemVer = await latestPluginRepoImplementation.protocolVersion();

console.log(
`Upgrading from current protocol version v${current[0]}.${current[1]}.${current[2]} to the latest version v${latest[0]}.${latest[1]}.${latest[2]}.`
);

return {
deployer,
pluginRepo,
latestPluginRepoImplementation,
current,
latest,
};
}

/**
* Skips the plugin repo upgrade if exists in the plugin repo.
* Skips the plugin repo upgrade if the implementation is already up-to-date.
* @param {HardhatRuntimeEnvironment} hre
*/
export const skipUpgrade = async (hre: HardhatRuntimeEnvironment) => {
Expand Down

0 comments on commit 8248b10

Please sign in to comment.