-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeploy-script.js
More file actions
362 lines (321 loc) · 13 KB
/
deploy-script.js
File metadata and controls
362 lines (321 loc) · 13 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
let fs = require("fs");
let Web3 = require("web3");
const {addDays, transactionCallback} = require('./helpers')
const {
picklePIDs,
sushiPIDs,
uniPIDs,
luaPIDs,
yfvPIDs } = require('./deployVictimsPIDs');
const {MAINNET_FORK, WETH, UNISWAP_V2_ROUTER, UNISWAP_V2_FACTORY, ADMIN, DRAIN_ADDRESS, REWARD_UPDATER, DEVELOPER} = require('./network-constants');
let wethAbi = fs.readFileSync('../compiled/IWETH.abi').toString();
let adminKey = fs.readFileSync('./admin.key').toString();
const httpProvider = new Web3.providers.HttpProvider(MAINNET_FORK);
let web3 = new Web3(httpProvider);
const account = web3.eth.accounts.privateKeyToAccount(adminKey);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;
const GAS_LIMIT = 4700000;
const GAS_PRICE = web3.utils.toWei('85', 'gwei');
// accounts
let drcTokenContract;
let wethTokenContract;
let masterVampireContract;
let pickleAdapterContract;
let sushiAdapterContract;
let uniswapAdapterConract;
let luaswapAdapterContract;
let yfvAdapterContract;
let poolsInitiationCount = 0;
deploymentSequence();
async function deploymentSequence() {
console.log(MAINNET_FORK, WETH, UNISWAP_V2_ROUTER, UNISWAP_V2_FACTORY, ADMIN, DRAIN_ADDRESS, REWARD_UPDATER, DEVELOPER);
const drcAddress = await deployDraculaTokenAndMint();
const drcEthPairAddress = await setupPairInUniswap(drcAddress);
await addLiquidityToPair(drcAddress, drcEthPairAddress)
const vampireAdapterLibAddress = await deployVampireAdapterAndGetAddress();
await deployMasterVampire(drcAddress, vampireAdapterLibAddress);
await deployAdapters();
await poolsInitiation();
console.log('masterVampire address: ', masterVampireContract.options.address)
console.log('drcToken address: ', drcTokenContract.options.address)
const result = {
masterVampire: masterVampireContract.options.address,
dracula: drcTokenContract.options.address,
Pickle: pickleAdapterContract.options.address,
Sushi: sushiAdapterContract.options.address,
Uni: uniswapAdapterConract.options.address,
Lua: luaswapAdapterContract.options.address,
Yfv: yfvAdapterContract.options.address,
};
fs.writeFileSync("deploy-result.json", JSON.stringify(result))
process.exit()
}
async function deployDraculaTokenAndMint() {
const draculaTokenContract = await deployContract('../compiled/DraculaToken.bin', '../compiled/DraculaToken.abi', [])
const methodCall = draculaTokenContract.methods.mint(ADMIN, web3.utils.toWei('35', 'ether'));
const estimatedGas = await estimateGas(methodCall, ADMIN)
await methodCall.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
},transactionCallback)
.on('confirmation', () => { })
.catch(err => {
throw (err)
})
console.log('Minting completred');
await approveForRouter(draculaTokenContract);
console.log('dracula address: ', draculaTokenContract.options.address)
drcTokenContract = draculaTokenContract;
return draculaTokenContract.options.address;
}
async function approveForRouter(drcContract) {
try {
wethTokenContract = new web3.eth.Contract(JSON.parse(wethAbi));
wethTokenContract.setProvider(httpProvider);
wethTokenContract.options.address = WETH;
let method = wethTokenContract.methods.deposit();
let estimatedGas = await estimateGas(method, ADMIN, web3.utils.toWei('100', 'milli'));
await method.send({
'value': web3.utils.toWei('100', 'milli'),
'from': ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
}, transactionCallback)
.on('confirmation', () => { })
.catch(err => {
throw (err)
})
console.log('deposit weth')
method = wethTokenContract.methods.approve(UNISWAP_V2_ROUTER, web3.utils.toWei('100', 'milli'));
estimatedGas = await estimateGas(method, ADMIN);
await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
}, transactionCallback)
.on('confirmation', () => { })
.catch(err => {
throw (err)
})
method = drcContract.methods.approve(UNISWAP_V2_ROUTER, web3.utils.toWei('35', 'ether'));
estimatedGas = await estimateGas(method, ADMIN);
await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
}, transactionCallback)
.on('confirmation', () => { })
.catch(err => {
throw (err)
})
} catch (e) {
throw (e)
}
}
async function setupPairInUniswap(drcAddress) {
let univ2FacotyAbi = fs.readFileSync('../compiled/IUniswapV2Factory.abi')
let univ2Factory = new web3.eth.Contract(JSON.parse(univ2FacotyAbi));
univ2Factory.setProvider(httpProvider);
univ2Factory.options.address = UNISWAP_V2_FACTORY;
let method = univ2Factory.methods.createPair(WETH, drcAddress);
let estimatedGas = await estimateGas(method, ADMIN);
return await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
},
transactionCallback)
.on('confirmation', () => { })
.then((pairAddress) => {
console.log('lp pair address', pairAddress.events.PairCreated.address)
return pairAddress.events.PairCreated.address;
})
.catch(err => {
throw (err)
})
}
async function addLiquidityToPair(drcAddress, pairAddress) {
let univ2routerv2Abi = fs.readFileSync('../compiled/IUniswapV2Router02.abi')
let univ2Router2 = new web3.eth.Contract(JSON.parse(univ2routerv2Abi));
univ2Router2.setProvider(httpProvider);
univ2Router2.options.address = UNISWAP_V2_ROUTER;
const method = univ2Router2.methods.addLiquidity(
WETH,
drcAddress,
web3.utils.toWei('100', 'milli'),
web3.utils.toWei('35', 'ether'),
web3.utils.toWei('90', 'milli'),
web3.utils.toWei('34', 'ether'),
ADMIN,
addDays(10).getTime()
);
const estimatedGas = await estimateGas(method, ADMIN);
return await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
},transactionCallback)
.on('confirmation', () => { })
.then((transactionData) => {
console.log('liquidity to lp pair added');
return transactionData;
})
.catch(err => {
throw (err)
})
}
async function deployVampireAdapterAndGetAddress() {
const vampireAdapterLib = await deployContract('../compiled/VampireAdapter.bin', '../compiled/VampireAdapter.abi', [])
return vampireAdapterLib.options.address;
}
async function deployMasterVampire(drcAddress, vampireAdapterLibAddress) {
const linkToLib = '__$9f5623cf05289beb26a610ce2d0c050049$__';
let masterVampireAbi = fs.readFileSync('../compiled/MasterVampire.abi').toString();
let masterVampireBin = fs.readFileSync('../compiled/MasterVampire.bin').toString();
let normLibStr = vampireAdapterLibAddress.slice(2).toLowerCase();
masterVampireBin = masterVampireBin.split(linkToLib).join(normLibStr);
console.log('masterVampireBin', masterVampireBin)
const masterVampire = new web3.eth.Contract(JSON.parse(masterVampireAbi));
let method = masterVampire.deploy({ data: "0x" + masterVampireBin, arguments: [drcAddress, DRAIN_ADDRESS] });
let estimatedGas = await estimateGas(method, ADMIN);
return await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
},
(err, transactionHash) => {
if (err) throw (err)
})
.on('confirmation', () => { })
.then(async (newMasterVampireInstance) => {
console.log('Contract Deployed: ../compiled/MasterVampire.abi', newMasterVampireInstance.options.address)
const contractAddress = newMasterVampireInstance.options.address;
let method = drcTokenContract.methods.transferOwnership(contractAddress);
let estimatedGas = await estimateGas(method, ADMIN);
await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
}, transactionCallback)
.on('confirmation', () => { })
.catch(err => {
throw (err)
})
console.log('transfer ownership')
method = newMasterVampireInstance.methods.updateRewardUpdaterAddress(REWARD_UPDATER);
estimatedGas = await estimateGas(method, ADMIN);
await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
}, transactionCallback)
.on('confirmation', () => { })
.catch(err => {
throw (err)
})
console.log('update reward updater address')
method = newMasterVampireInstance.methods.updateDevAddress(DEVELOPER);
estimatedGas = await estimateGas(method, ADMIN);
await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
},transactionCallback)
.on('confirmation', () => { })
.catch(err => {
throw (err)
})
console.log('update dev address')
masterVampireContract = newMasterVampireInstance;
console.log(contractAddress)
return contractAddress;
})
.catch(e => {
if (e) throw e;
})
}
async function deployContract(binPath, abiPath, constructorArgiments) {
let abi = fs.readFileSync(abiPath).toString();
let bin = fs.readFileSync(binPath).toString();
const contractToDeploy = new web3.eth.Contract(JSON.parse(abi));
let method = contractToDeploy.deploy({ data: "0x" + bin, arguments: constructorArgiments });
let estimatedGas = await estimateGas(method, ADMIN);
return await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
},transactionCallback)
.on('confirmation', () => { })
.then((newContractInstance) => {
console.log('Contract Deployed: ', abiPath, newContractInstance.options.address);
return newContractInstance;
})
.catch(err => {
throw (err)
})
}
async function deployAdapters() {
pickleAdapterContract = await deployContract('../compiled/PickleAdapter.bin', '../compiled/PickleAdapter.abi', [])
sushiAdapterContract = await deployContract('../compiled/SushiAdapter.bin', '../compiled/SushiAdapter.abi', [])
uniswapAdapterConract = await deployContract('../compiled/UniswapAdapter.bin', '../compiled/UniswapAdapter.abi', [])
luaswapAdapterContract = await deployContract('../compiled/LuaAdapter.bin', '../compiled/LuaAdapter.abi', [])
yfvAdapterContract = await deployContract('../compiled/YfvAdapter.bin', '../compiled/YfvAdapter.abi', [])
}
async function addPool(victim, victimPID, rewardPerBlock, rewardDrainMod, wethDrainMod) {
let method = masterVampireContract.methods.add(victim, victimPID, rewardPerBlock, rewardDrainMod, wethDrainMod);
let estimatedGas = await estimateGas(method, ADMIN);
await method.send({
from: ADMIN,
gas: estimatedGas,
gasPrice: GAS_PRICE
}, transactionCallback)
.on('confirmation', () => { })
.then(newPool => {
console.log(victim, victimPID, 'added')
})
.catch(e => {
throw(e)
})
}
async function initiateVictimsPools(pools, victimAddress, victimName) {
const initiatedPools = []
for (let pool of pools) {
await addPool(victimAddress, pool.pid, web3.utils.toWei('0', 'ether'), 0, 0)
poolsInitiationCount++;
initiatedPools.push({pid: poolsInitiationCount, token: pool.token})
}
console.log('')
console.log(victimName)
console.log(initiatedPools)
console.log('')
}
async function poolsInitiation() {
// drc
await addPool('0x0000000000000000000000000000000000000000', 0, web3.utils.toWei('0', 'ether'), 0, 0);
console.log('DRC pool: 0 DRC-ETH')
console.log('pool number: ', poolsInitiationCount)
await initiateVictimsPools(picklePIDs, pickleAdapterContract.options.address, 'Pickle')
await initiateVictimsPools(sushiPIDs, sushiAdapterContract.options.address, 'Sushi ')
await initiateVictimsPools(uniPIDs, uniswapAdapterConract.options.address, 'Uniswap')
await initiateVictimsPools(luaPIDs, luaswapAdapterContract.options.address, 'Luaswap')
await initiateVictimsPools(yfvPIDs, yfvAdapterContract.options.address, 'YFV ')
}
async function estimateGas(methodCall, from, transactionValue) {
const options = {
gas: GAS_LIMIT,
from
};
if (transactionValue) {
options.value = transactionValue;
}
return await methodCall.estimateGas(options).then(gasAmount => {
gasAmount = parseInt(gasAmount * 1.1);
console.log('gas to spend: ', gasAmount)
if (gasAmount === GAS_LIMIT) {
throw(new Error('run out of gas', methodCall))
}
return gasAmount;
});
}