-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhardhat.util.ts
96 lines (87 loc) · 2.35 KB
/
hardhat.util.ts
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
import { Fragment } from 'ethers/lib/utils';
import fs from 'fs';
import hre from 'hardhat';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { DeployOptions } from 'hardhat-deploy/types';
export const deployUpgradableContract = async (
deployments: HardhatRuntimeEnvironment['deployments'],
from: string,
owner: string,
name: string,
args: any[],
) => {
const { deploy } = deployments;
const contract = await deploy(name, {
from,
log: true,
proxy: {
owner,
proxyContract: 'UUPSProxy',
execute: {
init: { methodName: 'initialize', args },
},
},
estimateGasExtra: 1000000,
});
if (hre.hardhatArguments.network === 'hardhat') return contract;
const proxyAbiPath = `${__dirname}/deployments/${hre.hardhatArguments.network}/${name}_Proxy.json`;
const proxyContractAbi = JSON.parse(fs.readFileSync(proxyAbiPath, 'utf8'));
if (!proxyContractAbi.abi.find((i: Fragment) => i.name && i.name === 'upgradeTo')) {
proxyContractAbi.abi.push(
{
inputs: [
{
internalType: 'address',
name: 'newImplementation',
type: 'address',
},
],
name: 'upgradeTo',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{
internalType: 'address',
name: 'newImplementation',
type: 'address',
},
{
internalType: 'bytes',
name: 'data',
type: 'bytes',
},
],
name: 'upgradeToAndCall',
outputs: [],
stateMutability: 'payable',
type: 'function',
},
);
fs.writeFileSync(proxyAbiPath, JSON.stringify(proxyContractAbi, null, 2));
}
return contract;
};
export const deployPermanentContract = async (
deployments: HardhatRuntimeEnvironment['deployments'],
from: string,
name: string,
args: any[],
additionalOptions: Partial<DeployOptions> = {},
) => {
const { deploy } = deployments;
const result = await deploy(name, {
from,
args,
log: true,
estimateGasExtra: 1000000,
// nonce: 'pending',
...additionalOptions,
});
return hre.ethers.getContractAt(
additionalOptions && additionalOptions.contract ? additionalOptions.contract.toString() : name,
result.address,
);
};