Skip to content

Commit

Permalink
refactor: deployUpgradable() utility and proxy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybuidl committed Oct 16, 2023
1 parent bd0cb24 commit c98a69d
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 157 deletions.
2 changes: 1 addition & 1 deletion contracts/deploy/00-home-chain-arbitrable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003"; // General court, 3 jurors
const weth = await deployments.get("WETH");

const disputeTemplateRegistry = await deployUpgradable(hre, "DisputeTemplateRegistry", {
const disputeTemplateRegistry = await deployUpgradable(deployments, "DisputeTemplateRegistry", {
from: deployer,
args: [deployer],
log: true,
Expand Down
14 changes: 9 additions & 5 deletions contracts/deploy/00-home-chain-arbitration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
randomizerByChain.set(HomeChains[HomeChains[chainId]], randomizerMock.address);
}

await deployUpgradable(hre, "PolicyRegistry", { from: deployer, args: [deployer], log: true });
await deployUpgradable(deployments, "PolicyRegistry", { from: deployer, args: [deployer], log: true });

const randomizer = randomizerByChain.get(Number(await getChainId())) ?? AddressZero;
const rng = await deployUpgradable(hre, "RandomizerRNG", { from: deployer, args: [randomizer, deployer], log: true });
const rng = await deployUpgradable(deployments, "RandomizerRNG", {
from: deployer,
args: [randomizer, deployer],
log: true,
});

const disputeKit = await deployUpgradable(hre, "DisputeKitClassic", {
const disputeKit = await deployUpgradable(deployments, "DisputeKitClassic", {
from: deployer,
args: [deployer, AddressZero],
log: true,
Expand All @@ -72,7 +76,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
const devnet = isDevnet(hre.network);
const minStakingTime = devnet ? 180 : 1800;
const maxFreezingTime = devnet ? 600 : 1800;
const sortitionModule = await deployUpgradable(hre, "SortitionModule", {
const sortitionModule = await deployUpgradable(deployments, "SortitionModule", {
from: deployer,
args: [deployer, klerosCoreAddress, minStakingTime, maxFreezingTime, rng.address, RNG_LOOKAHEAD],
log: true,
Expand All @@ -84,7 +88,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
const minStake = BigNumber.from(10).pow(20).mul(2);
const alpha = 10000;
const feeForJuror = BigNumber.from(10).pow(17);
const klerosCore = await deployUpgradable(hre, "KlerosCore", {
const klerosCore = await deployUpgradable(deployments, "KlerosCore", {
from: deployer,
args: [
deployer,
Expand Down
2 changes: 1 addition & 1 deletion contracts/deploy/00-rng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
}

const randomizer = randomizerByChain.get(Number(await getChainId())) ?? AddressZero;
await deployUpgradable(hre, "RandomizerRNG", { from: deployer, args: [randomizer, deployer], log: true });
await deployUpgradable(deployments, "RandomizerRNG", { from: deployer, args: [randomizer, deployer], log: true });
const rng = await deploy("BlockHashRNG", {
from: deployer,
args: [],
Expand Down
2 changes: 1 addition & 1 deletion contracts/deploy/01-foreign-gateway-on-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironme

const homeChainId = (await homeChainProvider.getNetwork()).chainId;
const homeChainIdAsBytes32 = hexZeroPad(hexlify(homeChainId), 32);
await deployUpgradable(hre, "ForeignGatewayOnEthereum", {
await deployUpgradable(deployments, "ForeignGatewayOnEthereum", {
from: deployer,
contract: "ForeignGateway",
args: [deployer, veaOutbox.address, homeChainIdAsBytes32, homeGatewayAddress],
Expand Down
2 changes: 1 addition & 1 deletion contracts/deploy/01-foreign-gateway-on-gnosis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironme

const homeChainId = (await homeChainProvider.getNetwork()).chainId;
const homeChainIdAsBytes32 = hexZeroPad(hexlify(homeChainId), 32);
await deployUpgradable(hre, "ForeignGatewayOnGnosis", {
await deployUpgradable(deployments, "ForeignGatewayOnGnosis", {
from: deployer,
contract: "ForeignGateway",
args: [deployer, veaOutbox.address, homeChainIdAsBytes32, homeGatewayAddress],
Expand Down
2 changes: 1 addition & 1 deletion contracts/deploy/02-home-gateway-to-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
const foreignChainName = await hre.companionNetworks.foreignGoerli.deployments.getNetworkName();
console.log("Using ForeignGateway %s on chainId %s (%s)", foreignGateway.address, foreignChainId, foreignChainName);

await deployUpgradable(hre, "HomeGatewayToEthereum", {
await deployUpgradable(deployments, "HomeGatewayToEthereum", {
from: deployer,
contract: "HomeGateway",
args: [
Expand Down
2 changes: 1 addition & 1 deletion contracts/deploy/02-home-gateway-to-gnosis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
const foreignChainName = await hre.companionNetworks.foreignChiado.deployments.getNetworkName();
console.log("Using ForeignGateway %s on chainId %s (%s)", foreignGateway.address, foreignChainId, foreignChainName);

await deployUpgradable(hre, "HomeGatewayToGnosis", {
await deployUpgradable(deployments, "HomeGatewayToGnosis", {
from: deployer,
contract: "HomeGateway",
args: [deployer, klerosCore.address, veaInbox.address, foreignChainId, foreignGateway.address, dai.address],
Expand Down
6 changes: 3 additions & 3 deletions contracts/deploy/03-vea-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
console.log("Calculated future HomeGatewayToEthereum address for nonce %d: %s", nonce, homeGatewayAddress);

const homeChainIdAsBytes32 = hexZeroPad(hexlify(HardhatChain.HARDHAT), 32);
const foreignGateway = await deployUpgradable(hre, "ForeignGatewayOnEthereum", {
const foreignGateway = await deployUpgradable(deployments, "ForeignGatewayOnEthereum", {
from: deployer,
contract: "ForeignGateway",
args: [deployer, vea.address, homeChainIdAsBytes32, homeGatewayAddress],
Expand All @@ -39,7 +39,7 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
}); // nonce (implementation), nonce+1 (proxy)
console.log("foreignGateway.address: ", foreignGateway.address);

await deployUpgradable(hre, "HomeGatewayToEthereum", {
await deployUpgradable(deployments, "HomeGatewayToEthereum", {
from: deployer,
contract: "HomeGateway",
args: [
Expand All @@ -62,7 +62,7 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
await execute("ForeignGatewayOnEthereum", { from: deployer, log: true }, "changeCourtJurorFee", Courts.GENERAL, fee);
// TODO: set up the correct fees for the lower courts

const disputeTemplateRegistry = await deployUpgradable(hre, "DisputeTemplateRegistry", {
const disputeTemplateRegistry = await deployUpgradable(deployments, "DisputeTemplateRegistry", {
from: deployer,
args: [deployer],
log: true,
Expand Down
2 changes: 1 addition & 1 deletion contracts/deploy/upgrade-kleros-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const deployUpgradeKlerosCore: DeployFunction = async (hre: HardhatRuntimeEnviro
const sortitionModule = await deployments.get("SortitionModule");

console.log("Upgrading the KlerosCore...");
await deployUpgradable(hre, "KlerosCore", {
await deployUpgradable(deployments, "KlerosCore", {
from: deployer,
args: [
deployer,
Expand Down
2 changes: 1 addition & 1 deletion contracts/deploy/upgrade-sortition-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const deployUpgradeSortitionModule: DeployFunction = async (hre: HardhatRuntimeE
const klerosCoreAddress = klerosCore.address;

console.log("Upgrading the SortitionModule...");
await deployUpgradable(hre, "SortitionModule", {
await deployUpgradable(deployments, "SortitionModule", {
from: deployer,
args: [
deployer,
Expand Down
140 changes: 53 additions & 87 deletions contracts/deploy/utils/deployUpgradable.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,70 @@
import { DeployResult, DeployOptions, DeploymentsExtension } from "hardhat-deploy/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import {
DeployResult,
DeployOptions,
DeploymentsExtension,
DeployOptionsBase,
ProxyOptions,
} from "hardhat-deploy/types";

export const deployUpgradable = async (
hre: HardhatRuntimeEnvironment,
contract: string,
options: DeployOptions
): Promise<DeployResult> => {
const { deploy } = hre.deployments;
const { args, ...otherOptions } = options;
// Rationale: https://github.com/kleros/kleros-v2/pull/1214#issue-1879116629
return deploy(contract, {
proxy: {
proxyContract: "UUPSProxy",
proxyArgs: ["{implementation}", "{data}"],
checkProxyAdmin: false, // Not relevant for UUPSProxy
checkABIConflict: false, // Not relevant for UUPSProxy
upgradeFunction: {
methodName: "upgradeToAndCall",
upgradeArgs: ["{implementation}", "{data}"],
},
execute: {
init: {
methodName: "initialize",
args: args ?? [],
},
onUpgrade: {
methodName: "initialize",
args: args ?? [],
},
},
},
...otherOptions,
});
// Rationale: https://github.com/kleros/kleros-v2/pull/1214#issue-1879116629
const PROXY_OPTIONS: ProxyOptions = {
proxyContract: "UUPSProxy",
proxyArgs: ["{implementation}", "{data}"],
checkProxyAdmin: false, // Not relevant for UUPSProxy
checkABIConflict: false, // Not relevant for UUPSProxy
upgradeFunction: {
methodName: "upgradeToAndCall",
upgradeArgs: ["{implementation}", "{data}"],
},
};

export const deployUpgradableEXPERIMENTAL = async (
deployments: DeploymentsExtension,
contract: string,
options: DeployOptions
): Promise<DeployResult> => {
const { deploy } = deployments;
const { args, ...otherOptions } = options;
// Rationale: https://github.com/kleros/kleros-v2/pull/1214#issue-1879116629
return deploy(contract, {
proxy: {
proxyContract: "UUPSProxy",
proxyArgs: ["{implementation}", "{data}"],
checkProxyAdmin: false, // Not relevant for UUPSProxy
checkABIConflict: false, // Not relevant for UUPSProxy
upgradeFunction: {
methodName: "upgradeToAndCall",
upgradeArgs: ["{implementation}", "{data}"],
},
execute: {
init: {
methodName: "initialize",
args: args ?? [],
},
onUpgrade: {
methodName: "initialize",
args: args ?? [],
},
},
},
...otherOptions,
});
};
type DeployUpgradableOptions = {
newImplementation?: string;
initializer?: string;
} & DeployOptionsBase;

export const deployUpgradableEXPERIMENTAL2 = async (
export const deployUpgradable = async (
deployments: DeploymentsExtension,
proxy: string,
newImplementation: string,
initializer = "initialize",
options: DeployOptions
options: DeployUpgradableOptions
): Promise<DeployResult> => {
const { deploy } = deployments;
const { args, ...otherOptions } = options;
return await deploy(proxy, {
contract: newImplementation,
const { newImplementation, initializer, args: initializerArgs, ...otherOptions } = options;

const methodName = initializer ?? "initialize";
const args = initializerArgs ?? [];

const contract: Partial<DeployOptions> = newImplementation
? {
contract: newImplementation,
}
: {};

const implementationName: Partial<ProxyOptions> = newImplementation
? {
implementationName: newImplementation + "_Implementation",
}
: {};

let fullOptions: DeployOptions = {
...otherOptions,
...contract,
proxy: {
implementationName: newImplementation + "_Implementation",
proxyContract: "UUPSProxy",
proxyArgs: ["{implementation}", "{data}"],
checkProxyAdmin: false, // Not relevant for UUPSProxy
checkABIConflict: false, // Not relevant for UUPSProxy
upgradeFunction: {
methodName: "upgradeToAndCall",
upgradeArgs: ["{implementation}", "{data}"],
},
...PROXY_OPTIONS,
...implementationName,
execute: {
init: {
methodName: initializer,
args: args ?? [],
methodName,
args,
},
onUpgrade: {
methodName: initializer,
args: args ?? [],
methodName,
args,
},
},
},
...otherOptions,
});
};

// console.debug("fullOptions: ", JSON.stringify(fullOptions));
return await deploy(proxy, fullOptions);
};
Loading

0 comments on commit c98a69d

Please sign in to comment.