TokenVesting.test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const shouldFail = require('../helpers/shouldFail');
  2. const expectEvent = require('../helpers/expectEvent');
  3. const time = require('../helpers/time');
  4. const { ethGetBlock } = require('../helpers/web3');
  5. const { ZERO_ADDRESS } = require('../helpers/constants');
  6. const BigNumber = web3.BigNumber;
  7. require('chai')
  8. .use(require('chai-bignumber')(BigNumber))
  9. .should();
  10. const ERC20Mintable = artifacts.require('ERC20Mintable');
  11. const TokenVesting = artifacts.require('TokenVesting');
  12. contract('TokenVesting', function ([_, owner, beneficiary]) {
  13. const amount = new BigNumber(1000);
  14. beforeEach(async function () {
  15. // +1 minute so it starts after contract instantiation
  16. this.start = (await time.latest()) + time.duration.minutes(1);
  17. this.cliffDuration = time.duration.years(1);
  18. this.duration = time.duration.years(2);
  19. });
  20. it('reverts with a duration shorter than the cliff', async function () {
  21. const cliffDuration = this.duration;
  22. const duration = this.cliffDuration;
  23. cliffDuration.should.be.gt(duration);
  24. await shouldFail.reverting(
  25. TokenVesting.new(beneficiary, this.start, cliffDuration, duration, true, { from: owner })
  26. );
  27. });
  28. it('reverts with a null beneficiary', async function () {
  29. await shouldFail.reverting(
  30. TokenVesting.new(ZERO_ADDRESS, this.start, this.cliffDuration, this.duration, true, { from: owner })
  31. );
  32. });
  33. it('reverts with a null duration', async function () {
  34. // cliffDuration should also be 0, since the duration must be larger than the cliff
  35. await shouldFail.reverting(
  36. TokenVesting.new(beneficiary, this.start, 0, 0, true, { from: owner })
  37. );
  38. });
  39. it('reverts if the end time is in the past', async function () {
  40. const now = await time.latest();
  41. this.start = now - this.duration - time.duration.minutes(1);
  42. await shouldFail.reverting(
  43. TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner })
  44. );
  45. });
  46. context('once deployed', function () {
  47. beforeEach(async function () {
  48. this.vesting = await TokenVesting.new(
  49. beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner });
  50. this.token = await ERC20Mintable.new({ from: owner });
  51. await this.token.mint(this.vesting.address, amount, { from: owner });
  52. });
  53. it('can get state', async function () {
  54. (await this.vesting.beneficiary()).should.be.equal(beneficiary);
  55. (await this.vesting.cliff()).should.be.bignumber.equal(this.start + this.cliffDuration);
  56. (await this.vesting.start()).should.be.bignumber.equal(this.start);
  57. (await this.vesting.duration()).should.be.bignumber.equal(this.duration);
  58. (await this.vesting.revocable()).should.be.equal(true);
  59. });
  60. it('cannot be released before cliff', async function () {
  61. await shouldFail.reverting(this.vesting.release(this.token.address));
  62. });
  63. it('can be released after cliff', async function () {
  64. await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(1));
  65. const { logs } = await this.vesting.release(this.token.address);
  66. expectEvent.inLogs(logs, 'TokensReleased', {
  67. token: this.token.address,
  68. amount: await this.token.balanceOf(beneficiary),
  69. });
  70. });
  71. it('should release proper amount after cliff', async function () {
  72. await time.increaseTo(this.start + this.cliffDuration);
  73. const { receipt } = await this.vesting.release(this.token.address);
  74. const block = await ethGetBlock(receipt.blockNumber);
  75. const releaseTime = block.timestamp;
  76. const releasedAmount = amount.mul(releaseTime - this.start).div(this.duration).floor();
  77. (await this.token.balanceOf(beneficiary)).should.bignumber.equal(releasedAmount);
  78. (await this.vesting.released(this.token.address)).should.bignumber.equal(releasedAmount);
  79. });
  80. it('should linearly release tokens during vesting period', async function () {
  81. const vestingPeriod = this.duration - this.cliffDuration;
  82. const checkpoints = 4;
  83. for (let i = 1; i <= checkpoints; i++) {
  84. const now = this.start + this.cliffDuration + i * (vestingPeriod / checkpoints);
  85. await time.increaseTo(now);
  86. await this.vesting.release(this.token.address);
  87. const expectedVesting = amount.mul(now - this.start).div(this.duration).floor();
  88. (await this.token.balanceOf(beneficiary)).should.bignumber.equal(expectedVesting);
  89. (await this.vesting.released(this.token.address)).should.bignumber.equal(expectedVesting);
  90. }
  91. });
  92. it('should have released all after end', async function () {
  93. await time.increaseTo(this.start + this.duration);
  94. await this.vesting.release(this.token.address);
  95. (await this.token.balanceOf(beneficiary)).should.bignumber.equal(amount);
  96. (await this.vesting.released(this.token.address)).should.bignumber.equal(amount);
  97. });
  98. it('should be revoked by owner if revocable is set', async function () {
  99. const { logs } = await this.vesting.revoke(this.token.address, { from: owner });
  100. expectEvent.inLogs(logs, 'TokenVestingRevoked', { token: this.token.address });
  101. (await this.vesting.revoked(this.token.address)).should.equal(true);
  102. });
  103. it('should fail to be revoked by owner if revocable not set', async function () {
  104. const vesting = await TokenVesting.new(
  105. beneficiary, this.start, this.cliffDuration, this.duration, false, { from: owner }
  106. );
  107. await shouldFail.reverting(vesting.revoke(this.token.address, { from: owner }));
  108. });
  109. it('should return the non-vested tokens when revoked by owner', async function () {
  110. await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
  111. const vested = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
  112. await this.vesting.revoke(this.token.address, { from: owner });
  113. (await this.token.balanceOf(owner)).should.bignumber.equal(amount.sub(vested));
  114. });
  115. it('should keep the vested tokens when revoked by owner', async function () {
  116. await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
  117. const vestedPre = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
  118. await this.vesting.revoke(this.token.address, { from: owner });
  119. const vestedPost = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
  120. vestedPre.should.bignumber.equal(vestedPost);
  121. });
  122. it('should fail to be revoked a second time', async function () {
  123. await this.vesting.revoke(this.token.address, { from: owner });
  124. await shouldFail.reverting(this.vesting.revoke(this.token.address, { from: owner }));
  125. });
  126. function vestedAmount (total, now, start, cliffDuration, duration) {
  127. return (now < start + cliffDuration) ? 0 : Math.round(total * (now - start) / duration);
  128. }
  129. });
  130. });