-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsetup-fork.ts
220 lines (184 loc) · 6.11 KB
/
setup-fork.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
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
213
214
215
216
217
218
219
220
import Contracts from '../components/Contracts';
import { DeployedContracts, getNamedSigners, isTenderlyFork, runPendingDeployments } from '../utils/Deploy';
import Logger from '../utils/Logger';
import { NATIVE_TOKEN_ADDRESS } from '../utils/TokenData';
import { toWei } from '../utils/Types';
import { ZERO_ADDRESS } from '../utils/Constants';
import '@nomiclabs/hardhat-ethers';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import '@tenderly/hardhat-tenderly';
import '@typechain/hardhat';
import AdmZip from 'adm-zip';
import { BigNumber } from 'ethers';
import { getNamedAccounts } from 'hardhat';
import 'hardhat-deploy';
import { isEmpty } from 'lodash';
import path from 'path';
interface EnvOptions {
DEV_ADDRESSES: string;
FORK_NAME: string;
FORK_RESEARCH: boolean;
TENDERLY_PROJECT: string;
TENDERLY_USERNAME: string;
TENDERLY_FORK_ID: string;
}
const {
DEV_ADDRESSES,
FORK_NAME,
FORK_RESEARCH: isResearch,
TENDERLY_PROJECT,
TENDERLY_USERNAME,
TENDERLY_FORK_ID: forkId
}: EnvOptions = process.env as any as EnvOptions;
interface FundingRequest {
token: string;
amount: BigNumber;
whale: SignerWithAddress;
}
const fundAccount = async (account: string, fundingRequests: FundingRequest[]) => {
Logger.log(`Funding ${account}...`);
for (const fundingRequest of fundingRequests) {
const { whale, token, amount } = fundingRequest;
// for tokens which are missing skip funding request
if (token === ZERO_ADDRESS) {
continue;
}
if (!whale) {
continue;
}
if (token === NATIVE_TOKEN_ADDRESS) {
await whale.sendTransaction({
value: amount,
to: account
});
continue;
}
const tokenContract = await Contracts.ERC20.attach(token);
// check if whale has enough balance
const whaleBalance = await tokenContract.balanceOf(whale.address);
if (whaleBalance.lt(amount)) {
Logger.error(`Whale ${whale.address} has insufficient balance for ${token}`);
continue;
}
await tokenContract.connect(whale).transfer(account, amount);
}
};
const fundAccounts = async () => {
Logger.log('Funding test accounts...');
Logger.log();
const { dai, link, usdc, wbtc } = await getNamedAccounts();
const { ethWhale, bntWhale, daiWhale, linkWhale, usdcWhale, wbtcWhale } = await getNamedSigners();
const bnt = await DeployedContracts.BNT.deployed();
const fundingRequests = [
{
token: NATIVE_TOKEN_ADDRESS,
amount: toWei(10_000),
whale: ethWhale
},
{
token: bnt.address,
amount: toWei(10_000),
whale: bntWhale
},
{
token: dai,
amount: toWei(100_000),
whale: daiWhale
},
{
token: link,
amount: toWei(10_000),
whale: linkWhale
},
{
token: usdc,
amount: toWei(100_000, 6),
whale: usdcWhale
},
{
token: wbtc,
amount: toWei(100, 8),
whale: wbtcWhale
}
];
if (isEmpty(DEV_ADDRESSES)) {
Logger.log('No dev addresses to fund');
return;
}
const devAddresses = DEV_ADDRESSES.split(',');
for (const fundingRequest of fundingRequests) {
if (fundingRequest.token === ZERO_ADDRESS) {
Logger.log(`Skipping funding for ${fundingRequest.token}`);
}
const { whale } = fundingRequest;
if (!whale) {
continue;
}
const whaleBalance = await whale.getBalance();
// transfer ETH to the funding account if it doesn't have ETH
if (whaleBalance.lt(toWei(1))) {
await ethWhale.sendTransaction({
value: toWei(1),
to: whale.address
});
}
}
for (const account of devAddresses) {
await fundAccount(account, fundingRequests);
}
Logger.log();
};
const setLockDuration = async (lockDuration: number) => {
Logger.log(`Setting withdrawal lock duration to ${lockDuration} seconds...`);
Logger.log();
const { daoMultisig } = await getNamedSigners();
const pendingWithdrawals = await DeployedContracts.PendingWithdrawals.deployed();
await pendingWithdrawals.connect(daoMultisig).setLockDuration(lockDuration);
};
const runDeployments = async () => {
Logger.log('Running pending deployments...');
Logger.log();
await runPendingDeployments();
Logger.log();
};
const archiveArtifacts = async () => {
const zip = new AdmZip();
const srcDir = path.resolve(path.join(__dirname, './tenderly'));
const dest = path.resolve(path.join(__dirname, `../fork-${forkId}.zip`));
zip.addLocalFolder(srcDir);
zip.writeZip(dest);
Logger.log(`Archived ${srcDir} to ${dest}...`);
Logger.log();
};
const main = async () => {
if (!isTenderlyFork()) {
throw new Error('Invalid network');
}
Logger.log();
await runDeployments();
await fundAccounts();
const lockDuration = 2;
if (isResearch) {
await setLockDuration(lockDuration);
}
await archiveArtifacts();
const description = FORK_NAME ? `Bancor V3 ${FORK_NAME} Fork` : 'Bancor V3 Mainnet Fork';
Logger.log('********************************************************************************');
Logger.log();
Logger.log(description);
Logger.log('‾'.repeat(description.length));
Logger.log(` RPC: https://rpc.tenderly.co/fork/${forkId}`);
Logger.log(` Dashboard: https://dashboard.tenderly.co/${TENDERLY_USERNAME}/${TENDERLY_PROJECT}/fork/${forkId}`);
if (isResearch) {
Logger.log();
Logger.log(` * Withdrawal locking duration was set to ${lockDuration} seconds`);
}
Logger.log();
Logger.log('********************************************************************************');
};
main()
.then(() => process.exit(0))
.catch((error) => {
Logger.error(error);
process.exit(1);
});