generated from mudgen/contracts-starter
-
Notifications
You must be signed in to change notification settings - Fork 84
/
deploy.js
74 lines (64 loc) · 2.39 KB
/
deploy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* global ethers */
/* eslint prefer-const: "off" */
const { getSelectors, FacetCutAction } = require('./libraries/diamond.js')
async function deployDiamond () {
const accounts = await ethers.getSigners()
const contractOwner = accounts[0]
// Deploy DiamondInit
// DiamondInit provides a function that is called when the diamond is upgraded or deployed to initialize state variables
// Read about how the diamondCut function works in the EIP2535 Diamonds standard
const DiamondInit = await ethers.getContractFactory('DiamondInit')
const diamondInit = await DiamondInit.deploy()
await diamondInit.deployed()
console.log('DiamondInit deployed:', diamondInit.address)
// Deploy facets and set the `facetCuts` variable
console.log('')
console.log('Deploying facets')
const FacetNames = [
'DiamondCutFacet',
'DiamondLoupeFacet',
'OwnershipFacet'
]
// The `facetCuts` variable is the FacetCut[] that contains the functions to add during diamond deployment
const facetCuts = []
for (const FacetName of FacetNames) {
const Facet = await ethers.getContractFactory(FacetName)
const facet = await Facet.deploy()
await facet.deployed()
console.log(`${FacetName} deployed: ${facet.address}`)
facetCuts.push({
facetAddress: facet.address,
action: FacetCutAction.Add,
functionSelectors: getSelectors(facet)
})
}
// Creating a function call
// This call gets executed during deployment and can also be executed in upgrades
// It is executed with delegatecall on the DiamondInit address.
let functionCall = diamondInit.interface.encodeFunctionData('init')
// Setting arguments that will be used in the diamond constructor
const diamondArgs = {
owner: contractOwner.address,
init: diamondInit.address,
initCalldata: functionCall
}
// deploy Diamond
const Diamond = await ethers.getContractFactory('Diamond')
const diamond = await Diamond.deploy(facetCuts, diamondArgs)
await diamond.deployed()
console.log()
console.log('Diamond deployed:', diamond.address)
// returning the address of the diamond
return diamond.address
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
if (require.main === module) {
deployDiamond()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})
}
exports.deployDiamond = deployDiamond