dexorder
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const time = require('../helpers/time');
|
||||
|
||||
async function envSetup(mock, beneficiary, token) {
|
||||
return {
|
||||
eth: {
|
||||
checkRelease: async (tx, amount) => {
|
||||
await expect(tx).to.changeEtherBalances([mock, beneficiary], [-amount, amount]);
|
||||
},
|
||||
setupFailure: async () => {
|
||||
const beneficiaryMock = await ethers.deployContract('EtherReceiverMock');
|
||||
await beneficiaryMock.setAcceptEther(false);
|
||||
await mock.connect(beneficiary).transferOwnership(beneficiaryMock);
|
||||
return { args: [], error: [mock, 'FailedCall'] };
|
||||
},
|
||||
releasedEvent: 'EtherReleased',
|
||||
args: [],
|
||||
},
|
||||
token: {
|
||||
checkRelease: async (tx, amount) => {
|
||||
await expect(tx).to.emit(token, 'Transfer').withArgs(mock, beneficiary, amount);
|
||||
await expect(tx).to.changeTokenBalances(token, [mock, beneficiary], [-amount, amount]);
|
||||
},
|
||||
setupFailure: async () => {
|
||||
const pausableToken = await ethers.deployContract('$ERC20Pausable', ['Name', 'Symbol']);
|
||||
await pausableToken.$_pause();
|
||||
return {
|
||||
args: [ethers.Typed.address(pausableToken)],
|
||||
error: [pausableToken, 'EnforcedPause'],
|
||||
};
|
||||
},
|
||||
releasedEvent: 'ERC20Released',
|
||||
args: [ethers.Typed.address(token)],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function shouldBehaveLikeVesting() {
|
||||
it('check vesting schedule', async function () {
|
||||
for (const timestamp of this.schedule) {
|
||||
await time.increaseTo.timestamp(timestamp);
|
||||
const vesting = this.vestingFn(timestamp);
|
||||
|
||||
expect(await this.mock.vestedAmount(...this.args, timestamp)).to.equal(vesting);
|
||||
expect(await this.mock.releasable(...this.args)).to.equal(vesting);
|
||||
}
|
||||
});
|
||||
|
||||
it('execute vesting schedule', async function () {
|
||||
let released = 0n;
|
||||
{
|
||||
const tx = await this.mock.release(...this.args);
|
||||
await expect(tx)
|
||||
.to.emit(this.mock, this.releasedEvent)
|
||||
.withArgs(...this.args, 0);
|
||||
|
||||
await this.checkRelease(tx, 0n);
|
||||
}
|
||||
|
||||
for (const timestamp of this.schedule) {
|
||||
await time.increaseTo.timestamp(timestamp, false);
|
||||
const vested = this.vestingFn(timestamp);
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
it('should revert on transaction failure', async function () {
|
||||
const { args, error } = await this.setupFailure();
|
||||
|
||||
for (const timestamp of this.schedule) {
|
||||
await time.increaseTo.timestamp(timestamp);
|
||||
|
||||
await expect(this.mock.release(...args)).to.be.revertedWithCustomError(...error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
envSetup,
|
||||
shouldBehaveLikeVesting,
|
||||
};
|
||||
@@ -0,0 +1,65 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { min } = require('../helpers/math');
|
||||
const time = require('../helpers/time');
|
||||
|
||||
const { envSetup, shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
|
||||
|
||||
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 [sender, beneficiary] = await ethers.getSigners();
|
||||
const mock = await ethers.deployContract('VestingWallet', [beneficiary, start, duration]);
|
||||
|
||||
const token = await ethers.deployContract('$ERC20', ['Name', 'Symbol']);
|
||||
await token.$_mint(mock, amount);
|
||||
await sender.sendTransaction({ to: mock, value: amount });
|
||||
|
||||
const env = await envSetup(mock, beneficiary, token);
|
||||
|
||||
const schedule = Array.from({ length: 64 }, (_, i) => (BigInt(i) * duration) / 60n + start);
|
||||
const vestingFn = timestamp => min(amount, (amount * (timestamp - start)) / duration);
|
||||
|
||||
return { mock, duration, start, beneficiary, schedule, vestingFn, env };
|
||||
}
|
||||
|
||||
describe('VestingWallet', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('rejects zero address for beneficiary', async function () {
|
||||
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.equal(this.beneficiary);
|
||||
expect(await this.mock.start()).to.equal(this.start);
|
||||
expect(await this.mock.duration()).to.equal(this.duration);
|
||||
expect(await this.mock.end()).to.equal(this.start + this.duration);
|
||||
});
|
||||
|
||||
describe('vesting schedule', function () {
|
||||
describe('Eth vesting', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, this.env.eth);
|
||||
});
|
||||
|
||||
shouldBehaveLikeVesting();
|
||||
});
|
||||
|
||||
describe('ERC20 vesting', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, this.env.token);
|
||||
});
|
||||
|
||||
shouldBehaveLikeVesting();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const { min } = require('../helpers/math');
|
||||
const time = require('../helpers/time');
|
||||
|
||||
const { envSetup, shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
|
||||
|
||||
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 cliffDuration = time.duration.years(1);
|
||||
const cliff = start + cliffDuration;
|
||||
|
||||
const [sender, beneficiary] = await ethers.getSigners();
|
||||
const mock = await ethers.deployContract('$VestingWalletCliff', [beneficiary, start, duration, cliffDuration]);
|
||||
|
||||
const token = await ethers.deployContract('$ERC20', ['Name', 'Symbol']);
|
||||
await token.$_mint(mock, amount);
|
||||
await sender.sendTransaction({ to: mock, value: amount });
|
||||
|
||||
const env = await envSetup(mock, beneficiary, token);
|
||||
|
||||
const schedule = Array.from({ length: 64 }, (_, i) => (BigInt(i) * duration) / 60n + start);
|
||||
const vestingFn = timestamp => min(amount, timestamp < cliff ? 0n : (amount * (timestamp - start)) / duration);
|
||||
|
||||
return { mock, duration, start, beneficiary, cliff, schedule, vestingFn, env };
|
||||
}
|
||||
|
||||
describe('VestingWalletCliff', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('rejects a larger cliff than vesting duration', async function () {
|
||||
await expect(
|
||||
ethers.deployContract('$VestingWalletCliff', [this.beneficiary, this.start, this.duration, this.duration + 1n]),
|
||||
)
|
||||
.revertedWithCustomError(this.mock, 'InvalidCliffDuration')
|
||||
.withArgs(this.duration + 1n, this.duration);
|
||||
});
|
||||
|
||||
it('check vesting contract', async function () {
|
||||
expect(await this.mock.owner()).to.equal(this.beneficiary);
|
||||
expect(await this.mock.start()).to.equal(this.start);
|
||||
expect(await this.mock.duration()).to.equal(this.duration);
|
||||
expect(await this.mock.end()).to.equal(this.start + this.duration);
|
||||
expect(await this.mock.cliff()).to.equal(this.cliff);
|
||||
});
|
||||
|
||||
describe('vesting schedule', function () {
|
||||
describe('Eth vesting', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, this.env.eth);
|
||||
});
|
||||
|
||||
shouldBehaveLikeVesting();
|
||||
});
|
||||
|
||||
describe('ERC20 vesting', function () {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, this.env.token);
|
||||
});
|
||||
|
||||
shouldBehaveLikeVesting();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user