Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #79 from AngelProtocolFinance/recompile-and-update…
Browse files Browse the repository at this point in the history
…-diamond-deploy

Refactor and fix issues in diamond deploy script
  • Loading branch information
SovereignAndrey authored Jun 1, 2023
2 parents 016be68 + d313656 commit 78d52a9
Show file tree
Hide file tree
Showing 23 changed files with 323 additions and 361 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ yarn-error.log
.env
.prettierrc
.nvmrc
contract-address-local.json
dist

#Hardhat files
Expand Down
44 changes: 21 additions & 23 deletions contract-address.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"diamondCutFacet": "",
"diamondInitFacet": "",
"diamondLoupeFacet": "",
"ownershipFacet": "",
"reentrancyGuardFacet": ""
"ownershipFacet": ""
}
},
"charityApplication": {
Expand Down Expand Up @@ -153,30 +152,29 @@
},
"80001": {
"accounts": {
"diamond": "0x6878A6A4892299184d16Ef181acE84C6A2076ac1",
"diamond": "0xFCbE277d74027455A92fA67253FA16385F0579F4",
"facets": {
"accountDeployContract": "",
"accountDepositWithdrawEndowments": "",
"accountDonationMatch": "",
"accountsAllowance": "",
"accountsCreateEndowment": "",
"accountsDAOEndowments": "",
"accountsQueryEndowments": "",
"accountsSwapEndowments": "",
"accountsUpdate": "",
"accountsUpdateEndowments": "",
"accountsUpdateEndowmentSettingsController": "",
"accountsUpdateStatusEndowments": "",
"accountsVaultFacet": "",
"diamondCutFacet": "",
"diamondInitFacet": "",
"diamondLoupeFacet": "",
"ownershipFacet": "",
"reentrancyGuardFacet": ""
"accountDeployContract": "0xCebb4FbC3b20042d0D7cCf6658e4150B4A680DCd",
"accountDepositWithdrawEndowments": "0xb43421526bB09ECe4Fd8Ee58C4F33F19162c1E3c",
"accountDonationMatch": "0x8d1200931Ff1e176e2eA033C3CDB2C8d0B7b4e29",
"accountsAllowance": "0x76d7E8D8BA2D488a0f532D59EE0C7E43CCA2a509",
"accountsCreateEndowment": "0xB432B1CEf376B77c0B1bdBFa54d84A103435f226",
"accountsDAOEndowments": "0x04EffDae32A5dDFf2CC8B80efdbcB4898E26864C",
"accountsQueryEndowments": "0xB402E09EDEB25eF21f7c9a0a58A1a576Afd2853a",
"accountsSwapEndowments": "0x66E476a818ADd9D3BE6AbD949F476DEBD07BdEFb",
"accountsUpdate": "0x01023ffDFaE05b3b213430C84Dd383AA5236473F",
"accountsUpdateEndowments": "0xBF4965f8B7cB1c4acfA4dE9c5C289c6C6f820798",
"accountsUpdateEndowmentSettingsController": "0x0EA69a77087798526ccF59fF862e0Ed29E18f0f4",
"accountsUpdateStatusEndowments": "0x2EdB4a8E0b8d5f3D024c3175525Eb8F31FaaF0b2",
"accountsVaultFacet": "0x715Eb3f116de163af10DdbF4eD231916893833a9",
"diamondCutFacet": "0xB5602EB190943fb897999C9Df49c92faa27F9672",
"diamondInitFacet": "0x9908870F15E1626FB2944c622791f2e929EC2f58",
"diamondLoupeFacet": "0x76bd4D306B494287b3d87164bAC6665f275D7359",
"ownershipFacet": "0x9934602ad547c33e9B4615be9b824caF563368d2"
}
},
"charityApplication": {
"implementation": "",
"implementation": "0x79eB828d1bBdfBC2c8415435676433572E1b2C55",
"proxy": ""
},
"donationMatch": {
Expand Down Expand Up @@ -303,4 +301,4 @@
"weth": ""
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers";
import {HardhatRuntimeEnvironment} from "hardhat/types";
import {DiamondCutFacet__factory, DiamondInit} from "typechain-types";
import {FacetCut} from "./types";
import {logger} from "utils";

export default async function updateDiamond(
export default async function cutDiamond(
address: string,
diamondInit: DiamondInit,
admin: SignerWithAddress,
Expand All @@ -12,20 +13,20 @@ export default async function updateDiamond(
facetCuts: FacetCut[],
hre: HardhatRuntimeEnvironment
) {
console.log("Updating Diamond facets...");
logger.out("Cutting the Diamond with new facets...");

const diamondCut = DiamondCutFacet__factory.connect(address, admin);
const calldata = diamondInit.interface.encodeFunctionData("init", [owner, registrar]);

const cuts = facetCuts.map((x) => x.cut);
const tx = await diamondCut.diamondCut(cuts, diamondInit.address, calldata);
console.log("Cutting diamond tx: ", tx.hash);
logger.out(`Cutting Diamond tx: ${tx.hash}`);

const receipt = await hre.ethers.provider.waitForTransaction(tx.hash);

if (!receipt.status) {
throw Error(`Diamond upgrade failed: ${tx.hash}`);
throw new Error(`Diamond cut failed: ${tx.hash}`);
}

console.log("Completed diamond cut.");
logger.out("Completed Diamond cut.");
}
68 changes: 34 additions & 34 deletions contracts/core/accounts/scripts/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,73 @@ import {
Diamond__factory,
} from "typechain-types";
import deployFacets from "./deployFacets";
import updateDiamond from "./updateDiamond";
import cutDiamond from "./cutDiamond";
import verify from "./verify";
import {getSigners, logger, updateAddresses} from "utils";

export async function deployDiamond(
export async function deployAccountsDiamond(
owner: string,
registrar: string,
ANGEL_CORE_STRUCT: string,
STRING_LIBRARY: string,
hre: HardhatRuntimeEnvironment,
verify_contracts = false
verify_contracts: boolean,
hre: HardhatRuntimeEnvironment
) {
try {
const {proxyAdmin: diamondAdmin} = await getSigners(hre.ethers);
const diamondCut = await deployDiamondCutFacet(diamondAdmin);
logger.out("Deploying and setting up Accounts Diamond and all its facets...");

const diamond = await _deployDiamond(diamondAdmin, diamondCut.address, hre);
const {proxyAdmin} = await getSigners(hre.ethers);

const diamondInit = await deployDiamondInit(diamondAdmin);
const {diamond, diamondCutFacet} = await deployDiamond(proxyAdmin, hre);

const cuts = await deployFacets(diamondAdmin, ANGEL_CORE_STRUCT, STRING_LIBRARY);
const diamondInit = await deployDiamondInit(proxyAdmin, hre);

await updateDiamond(diamond.address, diamondInit, diamondAdmin, owner, registrar, cuts, hre);
const cuts = await deployFacets(proxyAdmin, ANGEL_CORE_STRUCT, hre);

if (verify_contracts) {
await verify(diamond.address, diamondCut.address, cuts, diamondAdmin, hre);
}
await cutDiamond(diamond.address, diamondInit, proxyAdmin, owner, registrar, cuts, hre);

return Promise.resolve(diamond.address);
} catch (error) {
return Promise.reject(error);
if (verify_contracts) {
await verify(diamond.address, diamondCutFacet.address, cuts, proxyAdmin, hre);
}

return diamond;
}

async function deployDiamondCutFacet(admin: SignerWithAddress): Promise<DiamondCutFacet> {
async function deployDiamond(
admin: SignerWithAddress,
hre: HardhatRuntimeEnvironment
): Promise<{diamond: Diamond; diamondCutFacet: DiamondCutFacet}> {
const DiamondCutFacet = new DiamondCutFacet__factory(admin);
const diamondCutFacet = await DiamondCutFacet.deploy();
await diamondCutFacet.deployed();
console.log("DiamondCutFacet deployed:", diamondCutFacet.address);
return diamondCutFacet;
}
logger.out(`DiamondCutFacet deployed at: ${diamondCutFacet.address}`);

async function _deployDiamond(
admin: SignerWithAddress,
diamondCut: string,
hre: HardhatRuntimeEnvironment
): Promise<Diamond> {
const Diamond = new Diamond__factory(admin);
const diamond = await Diamond.deploy(admin.address, diamondCut);
const diamond = await Diamond.deploy(admin.address, diamondCutFacet.address);
await diamond.deployed();
console.log("Diamond deployed:", diamond.address);
logger.out(`Diamond deployed at: ${diamond.address}`);

logger.out("Saving address to contract-address.json...");
await updateAddresses({accounts: {diamond: diamond.address}}, hre);
await updateAddresses(
{accounts: {diamond: diamond.address, facets: {diamondCutFacet: diamondCutFacet.address}}},
hre
);

return diamond;
return {diamond, diamondCutFacet};
}

/**
* DiamondInit provides a function that is called when the diamond is upgraded to initialize state variables
* Read about how the diamondCut function works here: https://eips.ethereum.org/EIPS/eip-2535#addingreplacingremoving-functions
* @param admin signer representing administrator of the contract
*/
async function deployDiamondInit(admin: SignerWithAddress): Promise<DiamondInit> {
async function deployDiamondInit(
admin: SignerWithAddress,
hre: HardhatRuntimeEnvironment
): Promise<DiamondInit> {
const DiamondInit = new DiamondInit__factory(admin);
const diamondInit = await DiamondInit.deploy();
await diamondInit.deployed();
console.log("DiamondInit deployed:", diamondInit.address, "\n");
logger.out(`DiamondInit deployed at: ${diamondInit.address}`);

await updateAddresses({accounts: {facets: {diamondInitFacet: diamondInit.address}}}, hre);

return diamondInit;
}
92 changes: 12 additions & 80 deletions contracts/core/accounts/scripts/deploy/deployFacets.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers";
import {ContractFactory} from "ethers";
import {
AccountDeployContract__factory,
AccountDepositWithdrawEndowments__factory,
AccountDonationMatch__factory,
AccountsAllowance__factory,
AccountsCreateEndowment__factory,
AccountsDAOEndowments__factory,
AccountsQueryEndowments__factory,
AccountsSwapEndowments__factory,
AccountsUpdateEndowmentSettingsController__factory,
AccountsUpdateEndowments__factory,
AccountsUpdateStatusEndowments__factory,
AccountsUpdate__factory,
AccountsVaultFacet__factory,
DiamondLoupeFacet__factory,
OwnershipFacet__factory,
} from "typechain-types";
import {logger} from "utils";
import {HardhatRuntimeEnvironment} from "hardhat/types";
import {getContractName, getFacetFactoryEntries, logger, updateAddresses} from "utils";
import {FacetCutAction, getSelectors} from "../libraries/diamond";
import {FacetCut} from "./types";

export default async function deployFacets(
diamondOwner: SignerWithAddress,
corestruct: string,
stringlib: string
hre: HardhatRuntimeEnvironment
): Promise<FacetCut[]> {
console.log("Deploying facets...");
logger.out("Deploying facets...");

const cuts: FacetCut[] = [];

console.log("Instantiating factories...");
const factoryEntries = await getFacetFactoryEntries(diamondOwner, corestruct);

const factories = await getFactories(diamondOwner, corestruct);

for (const Factory of factories) {
const contractName = Factory.constructor.name.replace("__factory", "");
for (const entry of factoryEntries) {
const contractName = getContractName(entry.factory);
try {
const facet = await Factory.deploy();
const facet = await entry.factory.deploy();
await facet.deployed();
console.log(`${contractName} deployed: ${facet.address}`);
logger.out(`${contractName} deployed at: ${facet.address}`);

await updateAddresses({accounts: {facets: {[entry.addressField]: facet.address}}}, hre);

cuts.push({
facetName: contractName,
cut: {
Expand All @@ -53,57 +37,5 @@ export default async function deployFacets(
}
}

console.log("\nDiamond Cut:", cuts);

return cuts;
}

// Getting factories instantiated in bulk as they share the deploy/cut creation logic.
async function getFactories(
diamondOwner: SignerWithAddress,
corestruct: string
): Promise<ContractFactory[]> {
const factories = [
// no lib
new AccountDeployContract__factory(diamondOwner),
new AccountsDAOEndowments__factory(diamondOwner),
new AccountDonationMatch__factory(diamondOwner),
new AccountsUpdate__factory(diamondOwner),
new AccountsQueryEndowments__factory(diamondOwner),
new AccountsUpdateStatusEndowments__factory(diamondOwner),
// core lib
new AccountsVaultFacet__factory(
{"contracts/core/struct.sol:AngelCoreStruct": corestruct},
diamondOwner
),
new AccountsAllowance__factory(
{"contracts/core/struct.sol:AngelCoreStruct": corestruct},
diamondOwner
),
new AccountsCreateEndowment__factory(
{"contracts/core/struct.sol:AngelCoreStruct": corestruct},
diamondOwner
),
new AccountsSwapEndowments__factory(
{"contracts/core/struct.sol:AngelCoreStruct": corestruct},
diamondOwner
),
new AccountsUpdateEndowments__factory(
{"contracts/core/struct.sol:AngelCoreStruct": corestruct},
diamondOwner
),
new AccountsUpdateEndowmentSettingsController__factory(
{"contracts/core/struct.sol:AngelCoreStruct": corestruct},
diamondOwner
),
new AccountDepositWithdrawEndowments__factory(
{
"contracts/core/struct.sol:AngelCoreStruct": corestruct,
},
diamondOwner
),
new DiamondLoupeFacet__factory(diamondOwner),
new OwnershipFacet__factory(diamondOwner),
];
return factories;
}
9 changes: 5 additions & 4 deletions contracts/core/accounts/scripts/deploy/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ export default async function verify(
admin: SignerWithAddress,
hre: HardhatRuntimeEnvironment
): Promise<void> {
logger.out("Verifying newly deployed facets...");
logger.out("Verifying newly deployed contracts:");

for (const {facetName, cut} of facetCuts) {
try {
logger.out(`Verifying ${facetName}...`);

await hre.run("verify:verify", {
address: cut.facetAddress,
constructorArguments: [],
});
logger.out(`${facetName} verified.`);
} catch (error) {
logger.out(
`Failed to verify ${facetName} at ${cut.facetAddress}. Error: ${error}`,
Expand All @@ -27,9 +28,9 @@ export default async function verify(
}
}

logger.out("Verifying the Diamond...");

try {
logger.out("Verifying the Diamond...");

await hre.run("verify:verify", {
address: diamondAddress,
constructorArguments: [admin.address, diamondCutAddress],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"format": "prettier --write './**/*.{ts,tsx,js}' --write contracts/**/*.sol",
"test": "hardhat test",
"compile": "hardhat compile && yarn format",
"deploy": "hardhat compile && hardhat run ./scripts/deploy.ts",
"clean-compile": "set -x && mv ./tasks/index.ts ./tasks/temp_index.ts && touch ./tasks/index.ts && hardhat clean && yarn compile && mv ./tasks/temp_index.ts ./tasks/index.ts && set +x",
"deploy": "yarn compile && hardhat run ./scripts/deploy.ts",
"abi": "hardhat export-abi"
},
"engines": {
Expand Down
Loading

0 comments on commit 78d52a9

Please sign in to comment.