You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
4.4 KiB
172 lines
4.4 KiB
4 years ago
|
async function deployImplementation(implementationName, deployArgs = []) {
|
||
|
const Implementation = await ethers.getContractFactory(implementationName);
|
||
|
const implementation = await Implementation.deploy(...deployArgs);
|
||
|
return implementation.deployed();
|
||
|
}
|
||
|
|
||
|
async function deployUpgradeBeaconController() {
|
||
|
const UpgradeBeaconController = await ethers.getContractFactory(
|
||
|
'UpgradeBeaconController',
|
||
|
);
|
||
|
const upgradeBeaconController = await UpgradeBeaconController.deploy();
|
||
|
return upgradeBeaconController.deployed();
|
||
|
}
|
||
|
|
||
|
async function deployUpgradeBeacon(
|
||
|
implementationAddress,
|
||
|
upgradeBeaconControllerAddress,
|
||
|
) {
|
||
|
const UpgradeBeacon = await ethers.getContractFactory('UpgradeBeacon');
|
||
|
const upgradeBeacon = await UpgradeBeacon.deploy(
|
||
|
implementationAddress,
|
||
|
upgradeBeaconControllerAddress,
|
||
|
);
|
||
|
return upgradeBeacon.deployed();
|
||
|
}
|
||
|
|
||
|
async function deployProxy(upgradeBeaconAddress, initializeData = '0x') {
|
||
|
const Proxy = await ethers.getContractFactory('UpgradeBeaconProxy');
|
||
|
const proxy = await Proxy.deploy(upgradeBeaconAddress, initializeData);
|
||
|
return proxy.deployed();
|
||
|
}
|
||
|
|
||
4 years ago
|
async function getInitializeData(
|
||
|
implementationName,
|
||
|
initializeArgs,
|
||
|
initializeIdentifier = 'initialize',
|
||
|
) {
|
||
|
if (initializeArgs.length === 0) {
|
||
|
return '0x';
|
||
|
}
|
||
|
|
||
|
const Implementation = await ethers.getContractFactory(implementationName);
|
||
|
|
||
4 years ago
|
const initializeFunction = Implementation.interface.getFunction(
|
||
|
initializeIdentifier,
|
||
|
);
|
||
4 years ago
|
|
||
|
const initializeData = Implementation.interface.encodeFunctionData(
|
||
|
initializeFunction,
|
||
|
initializeArgs,
|
||
|
);
|
||
|
|
||
|
return initializeData;
|
||
|
}
|
||
|
|
||
4 years ago
|
async function deployProxyWithImplementation(
|
||
|
upgradeBeaconAddress,
|
||
|
implementationName,
|
||
|
initializeArgs = [],
|
||
|
initializeIdentifier = 'initialize',
|
||
|
) {
|
||
4 years ago
|
const initializeData = await getInitializeData(
|
||
|
implementationName,
|
||
|
initializeArgs,
|
||
|
initializeIdentifier,
|
||
|
);
|
||
4 years ago
|
|
||
|
const proxy = await deployProxy(upgradeBeaconAddress, initializeData);
|
||
|
|
||
|
// instantiate proxy with Proxy Contract address + Implementation interface
|
||
4 years ago
|
const Implementation = await ethers.getContractFactory(implementationName);
|
||
4 years ago
|
const [signer] = await ethers.getSigners();
|
||
4 years ago
|
const proxyWithImplementation = new ethers.Contract(
|
||
|
proxy.address,
|
||
|
Implementation.interface,
|
||
|
signer,
|
||
|
);
|
||
|
return { proxy, proxyWithImplementation };
|
||
|
}
|
||
|
|
||
4 years ago
|
async function deployUpgradeSetup(
|
||
4 years ago
|
implementationName,
|
||
4 years ago
|
implementationDeployArgs,
|
||
|
upgradeBeaconController,
|
||
4 years ago
|
) {
|
||
|
// Deploy Implementation
|
||
|
const implementation = await deployImplementation(
|
||
|
implementationName,
|
||
|
implementationDeployArgs,
|
||
|
);
|
||
|
|
||
|
// Deploy UpgradeBeacon
|
||
|
const upgradeBeacon = await deployUpgradeBeacon(
|
||
|
implementation.address,
|
||
|
upgradeBeaconController.address,
|
||
|
);
|
||
|
|
||
4 years ago
|
return { implementation, upgradeBeaconController, upgradeBeacon };
|
||
|
}
|
||
|
|
||
|
async function deployUpgradeSetupAndController(
|
||
|
implementationName,
|
||
|
implementationDeployArgs,
|
||
|
) {
|
||
|
// Deploy UpgradeBeaconController
|
||
|
const upgradeBeaconController = await deployUpgradeBeaconController();
|
||
|
|
||
|
return deployUpgradeSetup(
|
||
|
implementationName,
|
||
|
implementationDeployArgs,
|
||
|
upgradeBeaconController,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
async function deployUpgradeSetupAndProxy(
|
||
|
implementationName,
|
||
4 years ago
|
constructorArgs = [],
|
||
|
initializeArgs = [],
|
||
4 years ago
|
upgradeBeaconController,
|
||
|
implementationInitializeFunctionIdentifier = 'initialize',
|
||
|
) {
|
||
|
let upgradeSetup;
|
||
|
if (upgradeBeaconController) {
|
||
|
upgradeSetup = await deployUpgradeSetup(
|
||
|
implementationName,
|
||
4 years ago
|
constructorArgs,
|
||
4 years ago
|
upgradeBeaconController,
|
||
|
);
|
||
|
} else {
|
||
|
upgradeSetup = await deployUpgradeSetupAndController(
|
||
|
implementationName,
|
||
4 years ago
|
constructorArgs,
|
||
4 years ago
|
);
|
||
|
upgradeBeaconController = upgradeSetup.upgradeBeaconController;
|
||
|
}
|
||
|
|
||
|
const { implementation, upgradeBeacon } = upgradeSetup;
|
||
|
|
||
4 years ago
|
// Construct initialize data
|
||
|
// Deploy Proxy Contract and initialize
|
||
4 years ago
|
const {
|
||
|
proxy,
|
||
|
proxyWithImplementation,
|
||
|
} = await deployProxyWithImplementation(
|
||
|
upgradeBeacon.address,
|
||
|
implementationName,
|
||
|
initializeArgs,
|
||
|
implementationInitializeFunctionIdentifier,
|
||
|
);
|
||
4 years ago
|
|
||
|
return {
|
||
|
contracts: {
|
||
|
implementation,
|
||
|
upgradeBeaconController,
|
||
|
upgradeBeacon,
|
||
|
proxy,
|
||
|
proxyWithImplementation,
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
deployUpgradeBeaconController,
|
||
4 years ago
|
deployUpgradeSetup,
|
||
|
deployImplementation,
|
||
4 years ago
|
deployUpgradeBeacon,
|
||
4 years ago
|
deployUpgradeSetupAndProxy,
|
||
4 years ago
|
deployProxy,
|
||
|
deployProxyWithImplementation,
|
||
4 years ago
|
getInitializeData,
|
||
4 years ago
|
};
|