TokenVesting.test.js 6.8 KB

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