Migrate finance tests to ethers.js (#4723)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
@ -1,54 +1,36 @@
|
|||||||
const { time } = require('@nomicfoundation/hardhat-network-helpers');
|
|
||||||
const { expectEvent } = require('@openzeppelin/test-helpers');
|
|
||||||
const { expect } = require('chai');
|
const { expect } = require('chai');
|
||||||
|
const { bigint: time } = require('../helpers/time');
|
||||||
|
|
||||||
function releasedEvent(token, amount) {
|
function shouldBehaveLikeVesting() {
|
||||||
return token ? ['ERC20Released', { token: token.address, amount }] : ['EtherReleased', { amount }];
|
|
||||||
}
|
|
||||||
|
|
||||||
function shouldBehaveLikeVesting(beneficiary) {
|
|
||||||
it('check vesting schedule', async function () {
|
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) {
|
for (const timestamp of this.schedule) {
|
||||||
await time.increaseTo(timestamp);
|
await time.forward.timestamp(timestamp);
|
||||||
const vesting = this.vestingFn(timestamp);
|
const vesting = this.vestingFn(timestamp);
|
||||||
|
|
||||||
expect(await this.mock.methods[vestedAmount](...args, timestamp)).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);
|
||||||
expect(await this.mock.methods[releasable](...args)).to.be.bignumber.equal(vesting);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('execute vesting schedule', async function () {
|
it('execute vesting schedule', async function () {
|
||||||
const [release, ...args] = this.token ? ['release(address)', this.token.address] : ['release()'];
|
let released = 0n;
|
||||||
|
|
||||||
let released = web3.utils.toBN(0);
|
|
||||||
const before = await this.getBalance(beneficiary);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
const receipt = await this.mock.methods[release](...args);
|
const tx = await this.mock.release(...this.args);
|
||||||
|
await expect(tx)
|
||||||
|
.to.emit(this.mock, this.releasedEvent)
|
||||||
|
.withArgs(...this.argsVerify, 0);
|
||||||
|
|
||||||
await expectEvent.inTransaction(receipt.tx, this.mock, ...releasedEvent(this.token, '0'));
|
await this.checkRelease(tx, 0n);
|
||||||
|
|
||||||
await this.checkRelease(receipt, beneficiary, '0');
|
|
||||||
|
|
||||||
expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const timestamp of this.schedule) {
|
for (const timestamp of this.schedule) {
|
||||||
await time.setNextBlockTimestamp(timestamp);
|
await time.forward.timestamp(timestamp, false);
|
||||||
const vested = this.vestingFn(timestamp);
|
const vested = this.vestingFn(timestamp);
|
||||||
|
|
||||||
const receipt = await this.mock.methods[release](...args);
|
const tx = await this.mock.release(...this.args);
|
||||||
await expectEvent.inTransaction(receipt.tx, this.mock, ...releasedEvent(this.token, vested.sub(released)));
|
await expect(tx).to.emit(this.mock, this.releasedEvent);
|
||||||
|
|
||||||
await this.checkRelease(receipt, beneficiary, vested.sub(released));
|
|
||||||
|
|
||||||
expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before.add(vested));
|
|
||||||
|
|
||||||
|
await this.checkRelease(tx, vested - released);
|
||||||
released = vested;
|
released = vested;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,69 +1,79 @@
|
|||||||
const { constants, expectEvent, time } = require('@openzeppelin/test-helpers');
|
const { ethers } = require('hardhat');
|
||||||
const { web3 } = require('@openzeppelin/test-helpers/src/setup');
|
|
||||||
const { expect } = require('chai');
|
const { expect } = require('chai');
|
||||||
const { BNmin } = require('../helpers/math');
|
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||||
const { expectRevertCustomError } = require('../helpers/customError');
|
const { bigint: time } = require('../helpers/time');
|
||||||
|
const { min } = require('../helpers/math');
|
||||||
const VestingWallet = artifacts.require('VestingWallet');
|
|
||||||
const ERC20 = artifacts.require('$ERC20');
|
|
||||||
|
|
||||||
const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
|
const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
|
||||||
|
|
||||||
contract('VestingWallet', function (accounts) {
|
async function fixture() {
|
||||||
const [sender, beneficiary] = accounts;
|
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 [sender, beneficiary] = await ethers.getSigners();
|
||||||
const duration = web3.utils.toBN(4 * 365 * 86400); // 4 years
|
const mock = await ethers.deployContract('VestingWallet', [beneficiary, start, duration]);
|
||||||
|
return { mock, amount, duration, start, sender, beneficiary };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('VestingWallet', function () {
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
this.start = (await time.latest()).addn(3600); // in 1 hour
|
Object.assign(this, await loadFixture(fixture));
|
||||||
this.mock = await VestingWallet.new(beneficiary, this.start, duration);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('rejects zero address for beneficiary', async function () {
|
it('rejects zero address for beneficiary', async function () {
|
||||||
await expectRevertCustomError(
|
await expect(ethers.deployContract('VestingWallet', [ethers.ZeroAddress, this.start, this.duration]))
|
||||||
VestingWallet.new(constants.ZERO_ADDRESS, this.start, duration),
|
.revertedWithCustomError(this.mock, 'OwnableInvalidOwner')
|
||||||
'OwnableInvalidOwner',
|
.withArgs(ethers.ZeroAddress);
|
||||||
[constants.ZERO_ADDRESS],
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('check vesting contract', async function () {
|
it('check vesting contract', async function () {
|
||||||
expect(await this.mock.owner()).to.be.equal(beneficiary);
|
expect(await this.mock.owner()).to.be.equal(this.beneficiary.address);
|
||||||
expect(await this.mock.start()).to.be.bignumber.equal(this.start);
|
expect(await this.mock.start()).to.be.equal(this.start);
|
||||||
expect(await this.mock.duration()).to.be.bignumber.equal(duration);
|
expect(await this.mock.duration()).to.be.equal(this.duration);
|
||||||
expect(await this.mock.end()).to.be.bignumber.equal(this.start.add(duration));
|
expect(await this.mock.end()).to.be.equal(this.start + this.duration);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('vesting schedule', function () {
|
describe('vesting schedule', function () {
|
||||||
beforeEach(async function () {
|
beforeEach(function () {
|
||||||
this.schedule = Array(64)
|
this.schedule = Array(64)
|
||||||
.fill()
|
.fill()
|
||||||
.map((_, i) => web3.utils.toBN(i).mul(duration).divn(60).add(this.start));
|
.map((_, i) => (BigInt(i) * this.duration) / 60n + this.start);
|
||||||
this.vestingFn = timestamp => BNmin(amount, amount.mul(timestamp.sub(this.start)).div(duration));
|
this.vestingFn = timestamp => min(this.amount, (this.amount * (timestamp - this.start)) / this.duration);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Eth vesting', function () {
|
describe('Eth vesting', function () {
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
await web3.eth.sendTransaction({ from: sender, to: this.mock.address, value: amount });
|
await this.sender.sendTransaction({ to: this.mock, value: this.amount });
|
||||||
this.getBalance = account => web3.eth.getBalance(account).then(web3.utils.toBN);
|
|
||||||
this.checkRelease = () => {};
|
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 () {
|
describe('ERC20 vesting', function () {
|
||||||
beforeEach(async 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.getBalance = account => this.token.balanceOf(account);
|
||||||
this.checkRelease = (receipt, to, value) =>
|
this.checkRelease = async (tx, amount) => {
|
||||||
expectEvent.inTransaction(receipt.tx, this.token, 'Transfer', { from: this.mock.address, to, value });
|
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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user