Add a VestingWallet (#2748)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
72
test/finance/VestingWallet.behavior.js
Normal file
72
test/finance/VestingWallet.behavior.js
Normal file
@ -0,0 +1,72 @@
|
||||
const { expectEvent } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
function releasedEvent (token, amount) {
|
||||
return token
|
||||
? [ 'ERC20Released', { token: token.address, amount } ]
|
||||
: [ 'EtherReleased', { amount } ];
|
||||
}
|
||||
|
||||
function shouldBehaveLikeVesting (beneficiary) {
|
||||
it('check vesting schedule', async function () {
|
||||
const [ method, ...args ] = this.token
|
||||
? [ 'vestedAmount(address,uint64)', this.token.address ]
|
||||
: [ 'vestedAmount(uint64)' ];
|
||||
|
||||
for (const timestamp of this.schedule) {
|
||||
expect(await this.mock.methods[method](...args, timestamp))
|
||||
.to.be.bignumber.equal(this.vestingFn(timestamp));
|
||||
}
|
||||
});
|
||||
|
||||
it('execute vesting schedule', async function () {
|
||||
const [ method, ...args ] = this.token
|
||||
? [ 'release(address)', this.token.address ]
|
||||
: [ 'release()' ];
|
||||
|
||||
let released = web3.utils.toBN(0);
|
||||
const before = await this.getBalance(beneficiary);
|
||||
|
||||
{
|
||||
const receipt = await this.mock.methods[method](...args);
|
||||
|
||||
await expectEvent.inTransaction(
|
||||
receipt.tx,
|
||||
this.mock,
|
||||
...releasedEvent(this.token, '0'),
|
||||
);
|
||||
|
||||
await this.checkRelease(receipt, beneficiary, '0');
|
||||
|
||||
expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before);
|
||||
}
|
||||
|
||||
for (const timestamp of this.schedule) {
|
||||
const vested = this.vestingFn(timestamp);
|
||||
|
||||
await new Promise(resolve => web3.currentProvider.send({
|
||||
method: 'evm_setNextBlockTimestamp',
|
||||
params: [ timestamp.toNumber() ],
|
||||
}, resolve));
|
||||
|
||||
const receipt = await this.mock.methods[method](...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));
|
||||
|
||||
released = vested;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeVesting,
|
||||
};
|
||||
67
test/finance/VestingWallet.test.js
Normal file
67
test/finance/VestingWallet.test.js
Normal file
@ -0,0 +1,67 @@
|
||||
const { constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
|
||||
const { web3 } = require('@openzeppelin/test-helpers/src/setup');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const ERC20Mock = artifacts.require('ERC20Mock');
|
||||
const VestingWallet = artifacts.require('VestingWallet');
|
||||
|
||||
const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
|
||||
|
||||
const min = (...args) => args.slice(1).reduce((x, y) => x.lt(y) ? x : y, args[0]);
|
||||
|
||||
contract('VestingWallet', function (accounts) {
|
||||
const [ sender, beneficiary ] = accounts;
|
||||
|
||||
const amount = web3.utils.toBN(web3.utils.toWei('100'));
|
||||
const duration = web3.utils.toBN(4 * 365 * 86400); // 4 years
|
||||
|
||||
beforeEach(async function () {
|
||||
this.start = (await time.latest()).addn(3600); // in 1 hour
|
||||
this.mock = await VestingWallet.new(beneficiary, this.start, duration);
|
||||
});
|
||||
|
||||
it('rejects zero address for beneficiary', async function () {
|
||||
await expectRevert(
|
||||
VestingWallet.new(constants.ZERO_ADDRESS, this.start, duration),
|
||||
'VestingWallet: beneficiary is zero address',
|
||||
);
|
||||
});
|
||||
|
||||
it('check vesting contract', async function () {
|
||||
expect(await this.mock.beneficiary()).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);
|
||||
});
|
||||
|
||||
describe('vesting schedule', function () {
|
||||
beforeEach(async function () {
|
||||
this.schedule = Array(64).fill().map((_, i) => web3.utils.toBN(i).mul(duration).divn(60).add(this.start));
|
||||
this.vestingFn = timestamp => min(amount, amount.mul(timestamp.sub(this.start)).div(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 = () => {};
|
||||
});
|
||||
|
||||
shouldBehaveLikeVesting(beneficiary);
|
||||
});
|
||||
|
||||
describe('ERC20 vesting', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC20Mock.new('Name', 'Symbol', this.mock.address, 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 },
|
||||
);
|
||||
});
|
||||
|
||||
shouldBehaveLikeVesting(beneficiary);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user