Skip to content

Commit

Permalink
Migrate finance tests to ethers.js (OpenZeppelin#4723)
Browse files Browse the repository at this point in the history
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: ernestognw <ernestognw@gmail.com>
  • Loading branch information
3 people authored Nov 8, 2023
1 parent 248be2f commit f1f427d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 67 deletions.
48 changes: 15 additions & 33 deletions test/finance/VestingWallet.behavior.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,36 @@
const { time } = require('@nomicfoundation/hardhat-network-helpers');
const { expectEvent } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { bigint: time } = require('../helpers/time');

function releasedEvent(token, amount) {
return token ? ['ERC20Released', { token: token.address, amount }] : ['EtherReleased', { amount }];
}

function shouldBehaveLikeVesting(beneficiary) {
function shouldBehaveLikeVesting() {
it('check vesting schedule', async function () {
const [vestedAmount, releasable, ...args] = this.token
? ['vestedAmount(address,uint64)', 'releasable(address)', this.token.address]
: ['vestedAmount(uint64)', 'releasable()'];

for (const timestamp of this.schedule) {
await time.increaseTo(timestamp);
await time.forward.timestamp(timestamp);
const vesting = this.vestingFn(timestamp);

expect(await this.mock.methods[vestedAmount](...args, timestamp)).to.be.bignumber.equal(vesting);

expect(await this.mock.methods[releasable](...args)).to.be.bignumber.equal(vesting);
expect(await this.mock.vestedAmount(...this.args, timestamp)).to.be.equal(vesting);
expect(await this.mock.releasable(...this.args)).to.be.equal(vesting);
}
});

it('execute vesting schedule', async function () {
const [release, ...args] = this.token ? ['release(address)', this.token.address] : ['release()'];

let released = web3.utils.toBN(0);
const before = await this.getBalance(beneficiary);

let released = 0n;
{
const receipt = await this.mock.methods[release](...args);

await expectEvent.inTransaction(receipt.tx, this.mock, ...releasedEvent(this.token, '0'));

await this.checkRelease(receipt, beneficiary, '0');
const tx = await this.mock.release(...this.args);
await expect(tx)
.to.emit(this.mock, this.releasedEvent)
.withArgs(...this.argsVerify, 0);

expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before);
await this.checkRelease(tx, 0n);
}

for (const timestamp of this.schedule) {
await time.setNextBlockTimestamp(timestamp);
await time.forward.timestamp(timestamp, false);
const vested = this.vestingFn(timestamp);

const receipt = await this.mock.methods[release](...args);
await expectEvent.inTransaction(receipt.tx, this.mock, ...releasedEvent(this.token, vested.sub(released)));

await this.checkRelease(receipt, beneficiary, vested.sub(released));

expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before.add(vested));
const tx = await this.mock.release(...this.args);
await expect(tx).to.emit(this.mock, this.releasedEvent);

await this.checkRelease(tx, vested - released);
released = vested;
}
});
Expand Down
78 changes: 44 additions & 34 deletions test/finance/VestingWallet.test.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,79 @@
const { constants, expectEvent, time } = require('@openzeppelin/test-helpers');
const { web3 } = require('@openzeppelin/test-helpers/src/setup');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { BNmin } = require('../helpers/math');
const { expectRevertCustomError } = require('../helpers/customError');

const VestingWallet = artifacts.require('VestingWallet');
const ERC20 = artifacts.require('$ERC20');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { bigint: time } = require('../helpers/time');
const { min } = require('../helpers/math');

const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior');

contract('VestingWallet', function (accounts) {
const [sender, beneficiary] = accounts;
async function fixture() {
const amount = ethers.parseEther('100');
const duration = time.duration.years(4);
const start = (await time.clock.timestamp()) + time.duration.hours(1);

const amount = web3.utils.toBN(web3.utils.toWei('100'));
const duration = web3.utils.toBN(4 * 365 * 86400); // 4 years
const [sender, beneficiary] = await ethers.getSigners();
const mock = await ethers.deployContract('VestingWallet', [beneficiary, start, duration]);
return { mock, amount, duration, start, sender, beneficiary };
}

describe('VestingWallet', function () {
beforeEach(async function () {
this.start = (await time.latest()).addn(3600); // in 1 hour
this.mock = await VestingWallet.new(beneficiary, this.start, duration);
Object.assign(this, await loadFixture(fixture));
});

it('rejects zero address for beneficiary', async function () {
await expectRevertCustomError(
VestingWallet.new(constants.ZERO_ADDRESS, this.start, duration),
'OwnableInvalidOwner',
[constants.ZERO_ADDRESS],
);
await expect(ethers.deployContract('VestingWallet', [ethers.ZeroAddress, this.start, this.duration]))
.revertedWithCustomError(this.mock, 'OwnableInvalidOwner')
.withArgs(ethers.ZeroAddress);
});

it('check vesting contract', async function () {
expect(await this.mock.owner()).to.be.equal(beneficiary);
expect(await this.mock.start()).to.be.bignumber.equal(this.start);
expect(await this.mock.duration()).to.be.bignumber.equal(duration);
expect(await this.mock.end()).to.be.bignumber.equal(this.start.add(duration));
expect(await this.mock.owner()).to.be.equal(this.beneficiary.address);
expect(await this.mock.start()).to.be.equal(this.start);
expect(await this.mock.duration()).to.be.equal(this.duration);
expect(await this.mock.end()).to.be.equal(this.start + this.duration);
});

describe('vesting schedule', function () {
beforeEach(async function () {
beforeEach(function () {
this.schedule = Array(64)
.fill()
.map((_, i) => web3.utils.toBN(i).mul(duration).divn(60).add(this.start));
this.vestingFn = timestamp => BNmin(amount, amount.mul(timestamp.sub(this.start)).div(duration));
.map((_, i) => (BigInt(i) * this.duration) / 60n + this.start);
this.vestingFn = timestamp => min(this.amount, (this.amount * (timestamp - this.start)) / this.duration);
});

describe('Eth vesting', function () {
beforeEach(async function () {
await web3.eth.sendTransaction({ from: sender, to: this.mock.address, value: amount });
this.getBalance = account => web3.eth.getBalance(account).then(web3.utils.toBN);
this.checkRelease = () => {};
await this.sender.sendTransaction({ to: this.mock, value: this.amount });

this.getBalance = signer => ethers.provider.getBalance(signer);
this.checkRelease = (tx, amount) => expect(tx).to.changeEtherBalances([this.beneficiary], [amount]);

this.releasedEvent = 'EtherReleased';
this.args = [];
this.argsVerify = [];
});

shouldBehaveLikeVesting(beneficiary);
shouldBehaveLikeVesting();
});

describe('ERC20 vesting', function () {
beforeEach(async function () {
this.token = await ERC20.new('Name', 'Symbol');
this.token = await ethers.deployContract('$ERC20', ['Name', 'Symbol']);
await this.token.$_mint(this.mock, this.amount);

this.getBalance = account => this.token.balanceOf(account);
this.checkRelease = (receipt, to, value) =>
expectEvent.inTransaction(receipt.tx, this.token, 'Transfer', { from: this.mock.address, to, value });
this.checkRelease = async (tx, amount) => {
await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.mock.target, this.beneficiary.address, amount);
await expect(tx).to.changeTokenBalances(this.token, [this.mock, this.beneficiary], [-amount, amount]);
};

await this.token.$_mint(this.mock.address, amount);
this.releasedEvent = 'ERC20Released';
this.args = [ethers.Typed.address(this.token.target)];
this.argsVerify = [this.token.target];
});

shouldBehaveLikeVesting(beneficiary);
shouldBehaveLikeVesting();
});
});
});

0 comments on commit f1f427d

Please sign in to comment.