TokenVesting improvements (#1431)

* Improved TokenVesting events.

* Added extra checks to TokenVesting.

* Renamed the events.

* Fixed linter error.

* Fixed a test that failed to cover a require.

* Renamed TokensRevoked to TokenVestingRevoked.
pull/1440/head
Nicolás Venturo 6 years ago committed by GitHub
parent a07499796a
commit 67dac7ae99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      contracts/drafts/TokenVesting.sol
  2. 30
      test/drafts/TokenVesting.test.js

@ -16,8 +16,8 @@ contract TokenVesting is Ownable {
using SafeMath for uint256; using SafeMath for uint256;
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
event Released(uint256 amount); event TokensReleased(address token, uint256 amount);
event Revoked(); event TokenVestingRevoked(address token);
// beneficiary of tokens after they are released // beneficiary of tokens after they are released
address private _beneficiary; address private _beneficiary;
@ -52,6 +52,8 @@ contract TokenVesting is Ownable {
{ {
require(beneficiary != address(0)); require(beneficiary != address(0));
require(cliffDuration <= duration); require(cliffDuration <= duration);
require(duration > 0);
require(start.add(duration) > block.timestamp);
_beneficiary = beneficiary; _beneficiary = beneficiary;
_revocable = revocable; _revocable = revocable;
@ -122,7 +124,7 @@ contract TokenVesting is Ownable {
token.safeTransfer(_beneficiary, unreleased); token.safeTransfer(_beneficiary, unreleased);
emit Released(unreleased); emit TokensReleased(token, unreleased);
} }
/** /**
@ -143,7 +145,7 @@ contract TokenVesting is Ownable {
token.safeTransfer(owner(), refund); token.safeTransfer(owner(), refund);
emit Revoked(); emit TokenVestingRevoked(token);
} }
/** /**

@ -1,4 +1,5 @@
const shouldFail = require('../helpers/shouldFail'); const shouldFail = require('../helpers/shouldFail');
const expectEvent = require('../helpers/expectEvent');
const time = require('../helpers/time'); const time = require('../helpers/time');
const { ethGetBlock } = require('../helpers/web3'); const { ethGetBlock } = require('../helpers/web3');
const { ZERO_ADDRESS } = require('../helpers/constants'); const { ZERO_ADDRESS } = require('../helpers/constants');
@ -22,7 +23,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
this.duration = time.duration.years(2); this.duration = time.duration.years(2);
}); });
it('rejects a duration shorter than the cliff', async function () { it('reverts with a duration shorter than the cliff', async function () {
const cliffDuration = this.duration; const cliffDuration = this.duration;
const duration = this.cliffDuration; const duration = this.cliffDuration;
@ -33,12 +34,28 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
); );
}); });
it('requires a valid beneficiary', async function () { it('reverts with a null beneficiary', async function () {
await shouldFail.reverting( await shouldFail.reverting(
TokenVesting.new(ZERO_ADDRESS, this.start, this.cliffDuration, this.duration, true, { from: owner }) TokenVesting.new(ZERO_ADDRESS, this.start, this.cliffDuration, this.duration, true, { from: owner })
); );
}); });
it('reverts with a null duration', async function () {
// cliffDuration should also be 0, since the duration must be larger than the cliff
await shouldFail.reverting(
TokenVesting.new(beneficiary, this.start, 0, 0, true, { from: owner })
);
});
it('reverts if the end time is in the past', async function () {
const now = await time.latest();
this.start = now - this.duration - time.duration.minutes(1);
await shouldFail.reverting(
TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner })
);
});
context('once deployed', function () { context('once deployed', function () {
beforeEach(async function () { beforeEach(async function () {
this.vesting = await TokenVesting.new( this.vesting = await TokenVesting.new(
@ -62,7 +79,11 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
it('can be released after cliff', async function () { it('can be released after cliff', async function () {
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(1)); await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(1));
await this.vesting.release(this.token.address); const { logs } = await this.vesting.release(this.token.address);
expectEvent.inLogs(logs, 'TokensReleased', {
token: this.token.address,
amount: await this.token.balanceOf(beneficiary),
});
}); });
it('should release proper amount after cliff', async function () { it('should release proper amount after cliff', async function () {
@ -100,7 +121,8 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
}); });
it('should be revoked by owner if revocable is set', async function () { it('should be revoked by owner if revocable is set', async function () {
await this.vesting.revoke(this.token.address, { from: owner }); const { logs } = await this.vesting.revoke(this.token.address, { from: owner });
expectEvent.inLogs(logs, 'TokenVestingRevoked', { token: this.token.address });
(await this.vesting.revoked(this.token.address)).should.equal(true); (await this.vesting.revoked(this.token.address)).should.equal(true);
}); });

Loading…
Cancel
Save