/
deploy.js
101 lines (90 loc) · 2.54 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import zkSync from './zkSync'
import { isLocalhost } from './localhost'
export const deployContract = async (
contractNameOrFullyQualifiedNameOrEthersFactory,
deployArgs = [],
deployOptions = { wait: 1 }
) => {
let Factory
if (typeof contractNameOrFullyQualifiedNameOrEthersFactory === 'string') {
const { ethers } = require('hardhat')
Factory = await ethers.getContractFactory(
contractNameOrFullyQualifiedNameOrEthersFactory
)
} else {
Factory = contractNameOrFullyQualifiedNameOrEthersFactory
}
const contract = await Factory.deploy(...deployArgs)
await contract.waitForDeployment(deployOptions.wait)
const { hash } = await contract.deploymentTransaction()
const address = await contract.getAddress()
console.log(address)
console.log(` > contract deployed at : ${address} (tx: ${hash})`)
if (!(await isLocalhost())) {
const verifyArgs = {
address,
deployArgs,
}
if (typeof contractNameOrFullyQualifiedNameOrEthersFactory === 'string') {
verifyArgs.contract = contractNameOrFullyQualifiedNameOrEthersFactory
}
await verify(verifyArgs)
}
return {
contract,
hash,
address,
}
}
export const deployUpgradeableContract = async (
contractNameOrFullyQualifiedName,
deployArgs = [],
deployOptions = {}
) => {
const { ethers, upgrades } = require('hardhat')
const Factory = await ethers.getContractFactory(
contractNameOrFullyQualifiedName
)
const contract = await upgrades.deployProxy(
Factory,
deployArgs,
deployOptions
)
await contract.waitForDeployment()
const { hash } = await contract.deploymentTransaction()
// get addresses
const address = await contract.getAddress()
const implementation = await upgrades.erc1967.getImplementationAddress(
address
)
if (!(await isLocalhost())) {
await verify({ address, deployArgs })
}
return {
contract,
hash,
address,
implementation,
}
}
export const verify = async ({ address, deployArgs, contract }) => {
const { run } = require('hardhat')
try {
await run('verify:verify', {
address,
contract,
constructorArguments: deployArgs,
})
} catch (error) {
console.log(`FAIL: Verification failed for contract at ${address}
with args :${deployArgs.toString()}`)
console.log(error)
}
}
export default {
deployContract: process.env.ZK_SYNC ? zkSync.deployContract : deployContract,
deployUpgradeableContract: process.env.ZK_SYNC
? zkSync.deployUpgradeableContract
: deployUpgradeableContract,
verifyContract: verify,
}