generated from nicobevilacqua/hardhat-solidity-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-Reentrance.test.ts
41 lines (33 loc) · 1.34 KB
/
10-Reentrance.test.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
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { BigNumber, Contract, utils } from 'ethers';
import { ethers } from 'hardhat';
const { provider } = ethers;
describe('Reentrance', () => {
let owner: SignerWithAddress;
let user1: SignerWithAddress;
let user2: SignerWithAddress;
let attacker: SignerWithAddress;
let contract: Contract;
before(async () => {
[owner, user1, user2, attacker] = await ethers.getSigners();
const Factory = await ethers.getContractFactory('Reentrance');
contract = await Factory.deploy();
await contract.deployed();
let tx = await contract.connect(user1).donate(user2.address, { value: utils.parseEther('1') });
await tx.wait();
tx = await contract.connect(user2).donate(user1.address, { value: utils.parseEther('1.2') });
await tx.wait();
});
it('attack', async () => {
const AttackerFactory = await ethers.getContractFactory('ReentranceAttacker');
const attackerContract = await AttackerFactory.connect(attacker).deploy(contract.address);
await attackerContract.deployed();
const tx = await attackerContract.connect(attacker).attack({
value: utils.parseEther('1'),
});
await tx.wait();
const balance = await provider.getBalance(contract.address);
expect(balance).to.equal(0);
});
});