|
| 1 | +// const { getSelectors, FacetCutAction } = require("./libraries/diamond.js"); |
| 2 | +import { getSelectors, FacetCutAction } from "./libraries/diamond"; |
| 3 | +const { ethers } = require("hardhat"); |
| 4 | + |
| 5 | +async function deployDiamond() { |
| 6 | + const accounts = await ethers.getSigners(); |
| 7 | + const contractOwner = accounts[0]; |
| 8 | + |
| 9 | + // deploy DiamondCutFacet |
| 10 | + const DiamondCutFacet = await ethers.getContractFactory("DiamondCutFacet"); |
| 11 | + const diamondCutFacet = await DiamondCutFacet.deploy(); |
| 12 | + await diamondCutFacet.deployed(); |
| 13 | + console.log("DiamondCutFacet deployed:", diamondCutFacet.address); |
| 14 | + |
| 15 | + // deploy Diamond |
| 16 | + const Diamond = await ethers.getContractFactory("Diamond"); |
| 17 | + const diamond = await Diamond.deploy( |
| 18 | + contractOwner.address, |
| 19 | + diamondCutFacet.address |
| 20 | + ); |
| 21 | + await diamond.deployed(); |
| 22 | + console.log("Diamond deployed:", diamond.address); |
| 23 | + |
| 24 | + // deploy DiamondInit |
| 25 | + // DiamondInit provides a function that is called when the diamond is upgraded to initialize state variables |
| 26 | + // Read about how the diamondCut function works here: https://eips.ethereum.org/EIPS/eip-2535#addingreplacingremoving-functions |
| 27 | + const DiamondInit = await ethers.getContractFactory("DiamondInit"); |
| 28 | + const diamondInit = await DiamondInit.deploy(); |
| 29 | + await diamondInit.deployed(); |
| 30 | + console.log("DiamondInit deployed:", diamondInit.address); |
| 31 | + |
| 32 | + // deploy facets |
| 33 | + console.log(""); |
| 34 | + console.log("Deploying facets"); |
| 35 | + const FacetNames = ["DiamondLoupeFacet", "OwnershipFacet"]; |
| 36 | + const cut = []; |
| 37 | + for (const FacetName of FacetNames) { |
| 38 | + const Facet = await ethers.getContractFactory(FacetName); |
| 39 | + const facet = await Facet.deploy(); |
| 40 | + await facet.deployed(); |
| 41 | + console.log(`${FacetName} deployed: ${facet.address}`); |
| 42 | + cut.push({ |
| 43 | + facetAddress: facet.address, |
| 44 | + action: FacetCutAction.Add, |
| 45 | + functionSelectors: getSelectors(facet), |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + // upgrade diamond with facets |
| 50 | + console.log("Diamond Cut:", cut); |
| 51 | + const diamondCut = await ethers.getContractAt("IDiamondCut", diamond.address); |
| 52 | + let tx; |
| 53 | + let receipt; |
| 54 | + // call to init function |
| 55 | + let functionCall = diamondInit.interface.encodeFunctionData("init"); |
| 56 | + tx = await diamondCut.diamondCut(cut, diamondInit.address, functionCall); |
| 57 | + console.log("Diamond cut tx: ", tx.hash); |
| 58 | + receipt = await tx.wait(); |
| 59 | + if (!receipt.status) { |
| 60 | + throw Error(`Diamond upgrade failed: ${tx.hash}`); |
| 61 | + } |
| 62 | + console.log("Completed diamond cut"); |
| 63 | + return diamond.address; |
| 64 | +} |
| 65 | + |
| 66 | +// We recommend this pattern to be able to use async/await everywhere |
| 67 | +// and properly handle errors. |
| 68 | +if (require.main === module) { |
| 69 | + deployDiamond() |
| 70 | + .then(() => process.exit(0)) |
| 71 | + .catch((error) => { |
| 72 | + console.error(error); |
| 73 | + process.exit(1); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +exports.deployDiamond = deployDiamond; |
0 commit comments