-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardhat.config.ts
522 lines (479 loc) · 16.8 KB
/
hardhat.config.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/* eslint-disable no-console */
import "@nomiclabs/hardhat-ethers"
import "hardhat-jest-plugin"
import "hardhat-deploy"
import fs from "fs"
import { readdir, readFile, writeFile } from "fs/promises"
import { HardhatUserConfig } from "hardhat/types"
import { subtask, task, types } from "hardhat/config"
import { Contract } from "ethers"
import { LSPConfiguration } from "./types"
const contractConfigs: Array<LSPConfiguration> = require("./contractConfigs.json")
const deployedContractConfigs: Array<LSPConfiguration> = require("./deployedContractConfigs.json")
const addresses = require("./addresses.json")
const abis = require("./abis")
function mnemonic() {
try {
return fs.readFileSync("./mnemonic.txt").toString().trim()
} catch (e) {
console.log("WARNING: No mnemonic file")
}
return ""
}
function url() {
try {
return fs.readFileSync("./url.txt").toString().trim()
} catch (e) {
console.log("WARNING: No url file")
}
return ""
}
const config: HardhatUserConfig = {
solidity: "0.7.3",
namedAccounts: {
deployer: 0,
tokenRecipient: 1,
},
networks: {
hardhat: {
forking: {
url: url(),
},
chainId: 42,
},
kovan: {
url: url(),
accounts: {
mnemonic: mnemonic(),
},
},
},
}
export default config
task(
"convert",
"Convert all json abis to human readable format",
async (_, { ethers }) => {
const path = "./abis/"
const files = await readdir(path)
for (const file of files) {
if (file === "index.ts") continue
const jsonBuffer = await readFile(path.concat(file))
const jsonAbi = JSON.parse(jsonBuffer.toString())
const iface = new ethers.utils.Interface(jsonAbi)
const readableAbi = iface.format(ethers.utils.FormatTypes.full)
console.log("Readable Abi: ", readableAbi)
await writeFile(path.concat(file), JSON.stringify(readableAbi, null, 2))
}
}
)
task("collateral", "Mint Collateral Tokens for use in tests")
.addOptionalParam("collateralName", "Name of Collateral to mint", "WETH")
.addOptionalParam(
"amount",
"Amount of Ether for which to generate collateral",
"10"
)
.addOptionalParam(
"gasprice",
"Gas Price to use in transactions",
50 * 100000000,
types.int
)
.setAction(
async (
{ collateralName, amount, gasprice },
{ ethers, getNamedAccounts, run }
) => {
if (collateralName === "LUSD") {
await run("LUSD", { amount })
} else {
const collateralContract = await ethers.getContractAt(
abis[collateralName],
addresses[collateralName]
)
const namedAccounts = await getNamedAccounts()
const transactionOptions = {
gasPrice: gasprice, // gasprice arg * 1 GWEI
from: namedAccounts.deployer,
value: ethers.utils.parseUnits(amount),
}
const depositTx = await collateralContract.deposit(transactionOptions)
await depositTx.wait()
console.log(`Deposited ${amount} in ${collateralName}`)
}
}
)
subtask("LUSD", "mint lusd")
.addParam("amount", "Amount of Ether for which to generate collateral", "10")
.setAction(async ({ amount }, { ethers, getNamedAccounts }) => {
const namedAccounts = await getNamedAccounts()
const transactionOptions = {
from: namedAccounts.deployer,
value: ethers.utils.parseUnits(amount),
}
const borrowerOperations = await ethers.getContractAt(
abis.BorrowerOperations,
addresses.BorrowerOperations
)
// Todo : implement getting real hints https://github.com/liquity/dev#example-borrower-operations-with-hints
const openTroveTx = await borrowerOperations.openTrove(
ethers.utils.parseUnits(".75"),
ethers.utils.parseUnits("2000"),
namedAccounts.deployer,
namedAccounts.deployer,
transactionOptions
)
await openTroveTx.wait()
})
task("synthetic", "Mint Synthetic Tokens for use in tests")
.addParam("syntheticName", "Name of Synthetic to mint")
.addOptionalParam("amount", "Amount of synthetic tokens to mint", "1")
.addOptionalParam(
"gasprice",
"Gas Price to use in transactions",
50 * 100000000,
types.int
)
.setAction(
async (
{ syntheticName, amount, gasprice },
{ ethers, getNamedAccounts }
) => {
const contractConfig = deployedContractConfigs.find(
(config) => config.syntheticName === syntheticName
)
if (contractConfig !== undefined) {
const namedAccounts = await getNamedAccounts()
const transactionOptions = {
gasPrice: gasprice, // gasprice arg * 1 GWEI
from: namedAccounts.deployer,
}
const LSPContract = await ethers.getContractAt(
abis.LSP,
contractConfig.address || ""
)
// Approve Collateral
const necessaryCollateral = ethers.utils.parseUnits(
(
parseFloat(amount) * parseFloat(contractConfig.collateralPerPair)
).toString()
)
const collateralAddress = addresses[contractConfig.collateralToken]
const collateralAbi = abis[contractConfig.collateralToken]
console.log("Collateral Address: ", collateralAddress)
const collateralContract = await ethers.getContractAt(
collateralAbi,
collateralAddress || ""
)
const approveTx = await collateralContract.approve(
contractConfig.address,
necessaryCollateral,
transactionOptions
)
await approveTx.wait()
console.log(
`Approve tx for ${necessaryCollateral.toString()} of collateral`,
approveTx
)
// Sends transaction
const createTx = await LSPContract.create(
ethers.utils.parseUnits(amount),
transactionOptions
)
await createTx.wait()
console.log(`Create tx for ${amount} of tokens`, createTx)
}
}
)
task("launch", "Launch all configured LSP contracts")
.addOptionalParam(
"gasprice",
"Gas Price to use in transactions",
50 * 100000000,
types.int
)
.setAction(async ({ gasprice }, { ethers, getNamedAccounts, run }) => {
const LSPCreator = await ethers.getContractAt(
abis.LSPCreator,
addresses.LSPCreator
)
const namedAccounts = await getNamedAccounts()
const transactionOptions = {
gasPrice: gasprice, // gasprice arg * 1 GWEI
from: namedAccounts.deployer,
}
const contracts: Record<string, Contract> = { LSPCreator }
for (const contractConfiguration of contractConfigs) {
try {
console.log(contractConfiguration)
// Parse Contract configuration values
const expirationTimestamp = Math.floor(
new Date(contractConfiguration.expirationTime).getTime() / 1000
).toString()
const priceIdentifier = ethers.utils.formatBytes32String(
contractConfiguration.priceIdentifier
)
const collateralPerPair = ethers.utils.parseUnits(
contractConfiguration.collateralPerPair
)
const syntheticName = contractConfiguration.syntheticName
const syntheticSymbol = contractConfiguration.syntheticSymbol
const collateralTokenAddress =
addresses[contractConfiguration.collateralToken]
const financialProductLibraryAddress =
addresses[contractConfiguration.financialProductLibrary]
const customAncillaryData = ethers.utils.formatBytes32String(
contractConfiguration.customAncillaryData
)
const prepaidProposerReward = ethers.utils.parseUnits(
contractConfiguration.prepaidProposerReward
)
// Get Collateral Contract instance if not present already
if (!(contractConfiguration.collateralToken in contracts)) {
contracts[
contractConfiguration.collateralToken
] = await ethers.getContractAt(
abis[contractConfiguration.collateralToken],
collateralTokenAddress
)
}
// Create and Approve collateral for the proposer reward
const collateralContract =
contracts[contractConfiguration.collateralToken]
if (contractConfiguration.collateralToken === "LUSD") {
await run("LUSD", { amount: "10" })
} else {
const depositTx = await collateralContract.deposit({
value: prepaidProposerReward.mul(
contractConfiguration.collateralPriceInEth
),
...transactionOptions,
})
await depositTx.wait()
console.log(
`Deposited ${prepaidProposerReward} in ${contractConfiguration.collateralToken}`
)
}
const approveTx = await collateralContract.approve(
addresses.LSPCreator,
prepaidProposerReward,
transactionOptions
)
await approveTx.wait()
console.log(
`Approved ${prepaidProposerReward} in ${contractConfiguration.collateralToken}`
)
// Launch LSP
console.log(`Simulating deploying ${syntheticName} to retrieve address`)
const lspAddress = await LSPCreator.callStatic.createLongShortPair(
expirationTimestamp,
collateralPerPair,
priceIdentifier,
syntheticName,
syntheticSymbol,
collateralTokenAddress,
financialProductLibraryAddress,
customAncillaryData,
prepaidProposerReward,
transactionOptions
)
console.log(`Deploying ${syntheticName} to address ${lspAddress}`)
const launchTx = await LSPCreator.createLongShortPair(
expirationTimestamp,
collateralPerPair,
priceIdentifier,
syntheticName,
syntheticSymbol,
collateralTokenAddress,
financialProductLibraryAddress,
customAncillaryData,
prepaidProposerReward,
transactionOptions
)
contractConfiguration.address = lspAddress
console.log(
`Deployed contract ${syntheticName} to address ${contractConfiguration.address} in transaction: ${launchTx.hash}`
)
// Configure Financial ProductLibrary
// Get Financial Product Library instance if not present already
if (!(contractConfiguration.financialProductLibrary in contracts)) {
contracts[
contractConfiguration.financialProductLibrary
] = await ethers.getContractAt(
abis[contractConfiguration.financialProductLibrary],
financialProductLibraryAddress
)
}
// Set Parameters
const financialProductLibraryContract =
contracts[contractConfiguration.financialProductLibrary]
await financialProductLibraryContract.setLongShortPairParameters(
lspAddress,
ethers.utils.parseUnits(
contractConfiguration.financialProductLibraryParameters[0]
),
ethers.utils.parseUnits(
contractConfiguration.financialProductLibraryParameters[1]
),
transactionOptions
)
console.log(
`Set the parameters to ${contractConfiguration.financialProductLibraryParameters} for ${contractConfiguration.financialProductLibrary} of contract ${syntheticName}`
)
contractConfiguration.success = true
} catch (e) {
console.log(
`FAILED to deploy contract ${contractConfiguration.syntheticName}`,
e
)
contractConfiguration.success = false
contractConfiguration.error = e.toString()
}
}
const outputFile = "./deployedContractConfigs.json"
await writeFile(outputFile, JSON.stringify(contractConfigs, null, 2))
})
task("time:expiry", "Set time to expiry date of given contract")
.addParam(
"syntheticName",
"Name of the contract whose expiration time you want to travel to / past"
)
.setAction(async ({ syntheticName }, { ethers }) => {
const contractConfig = deployedContractConfigs.find(
(config) => config.syntheticName === syntheticName
)
if (contractConfig !== undefined) {
const expirationTime = contractConfig.expirationTime
console.log("Traveling to expiration time: ", expirationTime)
const timeStamp = new Date(expirationTime).getTime() / 1000
await ethers.provider.send("evm_setNextBlockTimestamp", [timeStamp + 1])
await ethers.provider.send("evm_mine", [])
}
})
task(
"time:dispute",
"Advances time by the default dispute liveness value"
).setAction(async (_, { ethers }) => {
const optimisticOracle = await ethers.getContractAt(
abis.OptimisticOracle,
addresses.OptimisticOracle
)
const defaultLiveness = await optimisticOracle.defaultLiveness()
console.log("Default Liveness: ", defaultLiveness.toNumber())
await ethers.provider.send("evm_increaseTime", [defaultLiveness.toNumber()])
})
task("settle:oracle", "Settle on oracle")
.addParam(
"syntheticName",
"Name of the contract for which you want to propose a settlement price"
)
.setAction(async ({ syntheticName }, { ethers }) => {
const contractConfig = deployedContractConfigs.find(
(config) => config.syntheticName === syntheticName
)
if (contractConfig !== undefined) {
const optimisticOracle = await ethers.getContractAt(
abis.OptimisticOracle,
addresses.OptimisticOracle
)
// Send Settlement Transaction
const requester = contractConfig.address
const identifier = ethers.utils.formatBytes32String(
contractConfig.priceIdentifier
)
const timestamp = Math.floor(
new Date(contractConfig.expirationTime).getTime() / 1000
).toString()
const ancillaryData = ethers.utils.formatBytes32String(
contractConfig.customAncillaryData
)
const settleTx = await optimisticOracle.settle(
requester,
identifier,
timestamp,
ancillaryData
)
await settleTx.wait()
console.log("Settled on Oracle")
}
})
task("settle:lsp", "Settle on lsp contract")
.addParam(
"syntheticName",
"Name of the contract for which you want to settle tokens"
)
.addOptionalParam("longTokens", "Amount of long tokens to redeem", "0")
.addOptionalParam("shortTokens", "Amount of short tokens to redeem", "0")
.setAction(async ({ longTokens, shortTokens, syntheticName }, { ethers }) => {
const contractConfig = deployedContractConfigs.find(
(config) => config.syntheticName === syntheticName
)
if (contractConfig !== undefined) {
const lspContract = await ethers.getContractAt(
abis.LSP,
contractConfig.address || "NOADDRESS"
)
const longTokensParsed = ethers.utils.parseUnits(longTokens)
const shortTokensParsed = ethers.utils.parseUnits(shortTokens)
const settleTx = await lspContract.settle(
longTokensParsed,
shortTokensParsed
)
await settleTx.wait()
}
})
task("propose", "Propose price for given contract")
.addParam(
"syntheticName",
"Name of the contract for which you want to propose a settlement price"
)
.addParam("proposedPrice", "Price value to propose")
.setAction(async ({ syntheticName, proposedPrice }, { ethers }) => {
const contractConfig = deployedContractConfigs.find(
(config) => config.syntheticName === syntheticName
)
if (contractConfig !== undefined) {
const optimisticOracle = await ethers.getContractAt(
abis.OptimisticOracle,
addresses.OptimisticOracle
)
await optimisticOracle.deployed()
console.log("Optimistic Oracle is deployed")
// Approve collateral for the bond
const collateralAddress = addresses[contractConfig.collateralToken]
const collateralAbi = abis[contractConfig.collateralToken]
console.log("Collateral Address: ", collateralAddress)
const collateralContract = await ethers.getContractAt(
collateralAbi,
collateralAddress || ""
)
console.log("Connected to collateral contract")
const approveTx = await collateralContract.approve(
addresses.OptimisticOracle,
ethers.constants.MaxUint256
)
await approveTx.wait()
console.log("Approved collateral")
// Send Proposal
const requester = contractConfig.address
const identifier = ethers.utils.formatBytes32String(
contractConfig.priceIdentifier
)
const timestamp = Math.floor(
new Date(contractConfig.expirationTime).getTime() / 1000
).toString()
const ancillaryData = ethers.utils.formatBytes32String(
contractConfig.customAncillaryData
)
const proposeTx = await optimisticOracle.proposePrice(
requester,
identifier,
timestamp,
ancillaryData,
ethers.utils.parseUnits(proposedPrice)
)
await proposeTx.wait()
console.log("Proposed Price")
}
})