forked from midas-apps/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
212 lines (185 loc) · 5.37 KB
/
utils.ts
File metadata and controls
212 lines (185 loc) · 5.37 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { mine } from '@nomicfoundation/hardhat-network-helpers';
import { getImplementationAddress } from '@openzeppelin/upgrades-core';
import { ethers } from 'ethers';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import {
chainIds,
MTokenName,
MTokenNameEnum,
Network,
PaymentTokenName,
PaymentTokenNameEnum,
rpcUrls,
} from '../config';
export const DAY = 86400;
export function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export const isMTokenName = (name: string): name is MTokenName => {
return Object.values(MTokenNameEnum).includes(name as MTokenNameEnum);
};
export const isPaymentTokenName = (name: string): name is PaymentTokenName => {
return Object.values(PaymentTokenNameEnum).includes(
name as PaymentTokenNameEnum,
);
};
export const forkNetwork = async (
hre: HardhatRuntimeEnvironment,
network: Network,
blockNumber?: number,
) => {
if (
hre.network.name === network &&
hre.network.config.chainId === chainIds[network]
)
return;
await hre.network.provider.request({
method: 'hardhat_reset',
params: [{ forking: { jsonRpcUrl: rpcUrls[network], blockNumber } }],
});
await mine();
hre.network.name = network;
hre.network.config.chainId = chainIds[network];
};
export const getChainOrThrow = (hre: HardhatRuntimeEnvironment) => {
const { chainId } = hre.network.config;
const { name } = hre.network;
if (!chainId || name === 'hardhat' || name === 'localhost') {
throw new Error('Please provide a valid --network argument');
}
return { chainId, networkName: name };
};
export const getMTokenOrThrow = (hre: HardhatRuntimeEnvironment) => {
const mToken = hre.mtoken;
if (!mToken) {
throw new Error('MToken parameter not found');
}
return mToken;
};
export const getOriginalNetwork = (hre: HardhatRuntimeEnvironment) => {
const originalNetwork = hre.layerZero?.originalNetwork;
return originalNetwork;
};
export const getOriginalNetworkOrThrow = (hre: HardhatRuntimeEnvironment) => {
const originalNetwork = hre.layerZero?.originalNetwork;
if (!originalNetwork) {
throw new Error('OriginalNetwork parameter not found');
}
return originalNetwork;
};
export const getPaymentTokenOrThrow = (hre: HardhatRuntimeEnvironment) => {
const paymentToken = hre.paymentToken;
if (!paymentToken) {
throw new Error('PaymentToken parameter not found');
}
return paymentToken;
};
export const getActionOrThrow = (hre: HardhatRuntimeEnvironment) => {
const action = hre.action;
if (!action) {
throw new Error('Action parameter not found');
}
return action;
};
export const getMTokenOrPaymentTokenOrThrow = (
hre: HardhatRuntimeEnvironment,
) => {
const mToken = hre.mtoken;
const paymentToken = hre.paymentToken;
if (mToken && paymentToken) {
throw new Error('Only one of MToken or PaymentToken can be provided');
}
if (mToken) {
return { mToken };
}
if (paymentToken) {
return { paymentToken };
}
throw new Error('MToken or PaymentToken parameter not found');
};
export const getImplAddressFromProxy = async (
hre: HardhatRuntimeEnvironment,
proxyAddress: string,
): Promise<string> =>
await getImplementationAddress(hre.ethers.provider, proxyAddress);
export const logDeploy = (
contractName: string,
contractType: string | undefined,
address: string,
) =>
console.info(
`\x1b[32m${contractName}\x1b[0m${contractType ? ' ' : ''}${
contractType ?? ''
}:\t`,
'\x1b[36m',
address,
'\x1b[0m',
);
export const etherscanVerify = async (
hre: HardhatRuntimeEnvironment,
contractAddress: string,
...constructorArguments: unknown[]
) => {
const network = hre.network.name;
if (network === 'localhost' || network === 'hardhat') return;
await verify(hre, contractAddress, ...constructorArguments);
};
export const etherscanVerifyImplementation = async (
hre: HardhatRuntimeEnvironment,
proxyAddress: string,
...constructorArguments: unknown[]
) => {
const contractAddress = await getImplAddressFromProxy(hre, proxyAddress);
return etherscanVerify(hre, contractAddress, ...constructorArguments);
};
export const logDeployProxy = async (
hre: HardhatRuntimeEnvironment,
contractName: string,
address: string,
) => {
logDeploy(contractName, 'Proxy', address);
try {
logDeploy(
contractName,
'Impl',
await getImplAddressFromProxy(hre, address),
);
} catch (err) {
console.error('Log impl error. ', err);
}
};
export const tryEtherscanVerifyImplementation = async (
hre: HardhatRuntimeEnvironment,
proxyAddress: string,
...constructorArguments: unknown[]
) => {
return await etherscanVerifyImplementation(
hre,
proxyAddress,
...constructorArguments,
)
.catch((err) => {
console.error('Unable to verify. Error: ', err);
return false;
})
.then(() => {
return true;
});
};
export const verify = async (
hre: HardhatRuntimeEnvironment,
contractAddress: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...constructorArguments: any[]
) => {
await hre.run('verify:verify', {
address: contractAddress,
constructorArguments,
});
};
export const encodeFnSelector = (selector: string) =>
ethers.utils.id(selector).substring(0, 10);
export const importWithoutCache = async (pathResolved: string) => {
delete require.cache[pathResolved];
return await import(pathResolved);
};