Skip to content

Commit 8b4b7b8

Browse files
authored
Simplify finance tests (#4912)
1 parent 141c947 commit 8b4b7b8

File tree

3 files changed

+42
-78
lines changed

3 files changed

+42
-78
lines changed

test/finance/VestingWallet.behavior.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
1+
const { ethers } = require('hardhat');
12
const { expect } = require('chai');
23
const time = require('../helpers/time');
34

5+
async function envSetup(mock, beneficiary, token) {
6+
return {
7+
eth: {
8+
checkRelease: async (tx, amount) => {
9+
await expect(tx).to.changeEtherBalances([mock, beneficiary], [-amount, amount]);
10+
},
11+
setupFailure: async () => {
12+
const beneficiaryMock = await ethers.deployContract('EtherReceiverMock');
13+
await beneficiaryMock.setAcceptEther(false);
14+
await mock.connect(beneficiary).transferOwnership(beneficiaryMock);
15+
return { args: [], error: [mock, 'FailedInnerCall'] };
16+
},
17+
releasedEvent: 'EtherReleased',
18+
args: [],
19+
},
20+
token: {
21+
checkRelease: async (tx, amount) => {
22+
await expect(tx).to.emit(token, 'Transfer').withArgs(mock, beneficiary, amount);
23+
await expect(tx).to.changeTokenBalances(token, [mock, beneficiary], [-amount, amount]);
24+
},
25+
setupFailure: async () => {
26+
const pausableToken = await ethers.deployContract('$ERC20Pausable', ['Name', 'Symbol']);
27+
await pausableToken.$_pause();
28+
return {
29+
args: [ethers.Typed.address(pausableToken)],
30+
error: [pausableToken, 'EnforcedPause'],
31+
};
32+
},
33+
releasedEvent: 'ERC20Released',
34+
args: [ethers.Typed.address(token)],
35+
},
36+
};
37+
}
38+
439
function shouldBehaveLikeVesting() {
540
it('check vesting schedule', async function () {
641
for (const timestamp of this.schedule) {
@@ -18,7 +53,7 @@ function shouldBehaveLikeVesting() {
1853
const tx = await this.mock.release(...this.args);
1954
await expect(tx)
2055
.to.emit(this.mock, this.releasedEvent)
21-
.withArgs(...this.argsVerify, 0);
56+
.withArgs(...this.args, 0);
2257

2358
await this.checkRelease(tx, 0n);
2459
}
@@ -47,5 +82,6 @@ function shouldBehaveLikeVesting() {
4782
}
4883

4984
module.exports = {
85+
envSetup,
5086
shouldBehaveLikeVesting,
5187
};

test/finance/VestingWallet.test.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
55
const { min } = require('../helpers/math');
66
const time = require('../helpers/time');
77

8-
const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
8+
const { envSetup, shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
99

1010
async function fixture() {
1111
const amount = ethers.parseEther('100');
@@ -19,44 +19,9 @@ async function fixture() {
1919
await token.$_mint(mock, amount);
2020
await sender.sendTransaction({ to: mock, value: amount });
2121

22-
const pausableToken = await ethers.deployContract('$ERC20Pausable', ['Name', 'Symbol']);
23-
const beneficiaryMock = await ethers.deployContract('EtherReceiverMock');
24-
25-
const env = {
26-
eth: {
27-
checkRelease: async (tx, amount) => {
28-
await expect(tx).to.emit(mock, 'EtherReleased').withArgs(amount);
29-
await expect(tx).to.changeEtherBalances([mock, beneficiary], [-amount, amount]);
30-
},
31-
setupFailure: async () => {
32-
await beneficiaryMock.setAcceptEther(false);
33-
await mock.connect(beneficiary).transferOwnership(beneficiaryMock);
34-
return { args: [], error: [mock, 'FailedInnerCall'] };
35-
},
36-
releasedEvent: 'EtherReleased',
37-
argsVerify: [],
38-
args: [],
39-
},
40-
token: {
41-
checkRelease: async (tx, amount) => {
42-
await expect(tx).to.emit(token, 'Transfer').withArgs(mock, beneficiary, amount);
43-
await expect(tx).to.changeTokenBalances(token, [mock, beneficiary], [-amount, amount]);
44-
},
45-
setupFailure: async () => {
46-
await pausableToken.$_pause();
47-
return {
48-
args: [ethers.Typed.address(pausableToken)],
49-
error: [pausableToken, 'EnforcedPause'],
50-
};
51-
},
52-
releasedEvent: 'ERC20Released',
53-
argsVerify: [token],
54-
args: [ethers.Typed.address(token)],
55-
},
56-
};
22+
const env = await envSetup(mock, beneficiary, token);
5723

5824
const schedule = Array.from({ length: 64 }, (_, i) => (BigInt(i) * duration) / 60n + start);
59-
6025
const vestingFn = timestamp => min(amount, (amount * (timestamp - start)) / duration);
6126

6227
return { mock, duration, start, beneficiary, schedule, vestingFn, env };

test/finance/VestingWalletCliff.test.js

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
55
const { min } = require('../helpers/math');
66
const time = require('../helpers/time');
77

8-
const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
8+
const { envSetup, shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
99

1010
async function fixture() {
1111
const amount = ethers.parseEther('100');
@@ -21,46 +21,9 @@ async function fixture() {
2121
await token.$_mint(mock, amount);
2222
await sender.sendTransaction({ to: mock, value: amount });
2323

24-
const pausableToken = await ethers.deployContract('$ERC20Pausable', ['Name', 'Symbol']);
25-
const beneficiaryMock = await ethers.deployContract('EtherReceiverMock');
26-
27-
const env = {
28-
eth: {
29-
checkRelease: async (tx, amount) => {
30-
await expect(tx).to.emit(mock, 'EtherReleased').withArgs(amount);
31-
await expect(tx).to.changeEtherBalances([mock, beneficiary], [-amount, amount]);
32-
},
33-
setupFailure: async () => {
34-
await beneficiaryMock.setAcceptEther(false);
35-
await mock.connect(beneficiary).transferOwnership(beneficiaryMock);
36-
return { args: [], error: [mock, 'FailedInnerCall'] };
37-
},
38-
releasedEvent: 'EtherReleased',
39-
argsVerify: [],
40-
args: [],
41-
},
42-
token: {
43-
checkRelease: async (tx, amount) => {
44-
await expect(tx).to.emit(token, 'Transfer').withArgs(mock, beneficiary, amount);
45-
await expect(tx).to.changeTokenBalances(token, [mock, beneficiary], [-amount, amount]);
46-
},
47-
setupFailure: async () => {
48-
await pausableToken.$_pause();
49-
return {
50-
args: [ethers.Typed.address(pausableToken)],
51-
error: [pausableToken, 'EnforcedPause'],
52-
};
53-
},
54-
releasedEvent: 'ERC20Released',
55-
argsVerify: [token],
56-
args: [ethers.Typed.address(token)],
57-
},
58-
};
59-
60-
const schedule = Array(64)
61-
.fill()
62-
.map((_, i) => (BigInt(i) * duration) / 60n + start);
24+
const env = await envSetup(mock, beneficiary, token);
6325

26+
const schedule = Array.from({ length: 64 }, (_, i) => (BigInt(i) * duration) / 60n + start);
6427
const vestingFn = timestamp => min(amount, timestamp < cliff ? 0n : (amount * (timestamp - start)) / duration);
6528

6629
return { mock, duration, start, beneficiary, cliff, schedule, vestingFn, env };

0 commit comments

Comments
 (0)