TokenVesting.test.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. const { BN, constants, expectEvent, shouldFail, time } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const ERC20Mintable = artifacts.require('ERC20Mintable');
  4. const TokenVesting = artifacts.require('TokenVesting');
  5. contract('TokenVesting', function ([_, owner, beneficiary]) {
  6. const amount = new BN('1000');
  7. beforeEach(async function () {
  8. // +1 minute so it starts after contract instantiation
  9. this.start = (await time.latest()).add(time.duration.minutes(1));
  10. this.cliffDuration = time.duration.years(1);
  11. this.duration = time.duration.years(2);
  12. });
  13. it('reverts with a duration shorter than the cliff', async function () {
  14. const cliffDuration = this.duration;
  15. const duration = this.cliffDuration;
  16. cliffDuration.should.be.bignumber.that.is.at.least(duration);
  17. await shouldFail.reverting.withMessage(
  18. TokenVesting.new(beneficiary, this.start, cliffDuration, duration, true, { from: owner }),
  19. 'TokenVesting: cliff is longer than duration'
  20. );
  21. });
  22. it('reverts with a null beneficiary', async function () {
  23. await shouldFail.reverting.withMessage(
  24. TokenVesting.new(ZERO_ADDRESS, this.start, this.cliffDuration, this.duration, true, { from: owner }),
  25. 'TokenVesting: beneficiary is the zero address'
  26. );
  27. });
  28. it('reverts with a null duration', async function () {
  29. // cliffDuration should also be 0, since the duration must be larger than the cliff
  30. await shouldFail.reverting.withMessage(
  31. TokenVesting.new(beneficiary, this.start, 0, 0, true, { from: owner }), 'TokenVesting: duration is 0'
  32. );
  33. });
  34. it('reverts if the end time is in the past', async function () {
  35. const now = await time.latest();
  36. this.start = now.sub(this.duration).sub(time.duration.minutes(1));
  37. await shouldFail.reverting.withMessage(
  38. TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner }),
  39. 'TokenVesting: final time is before current time'
  40. );
  41. });
  42. context('once deployed', function () {
  43. beforeEach(async function () {
  44. this.vesting = await TokenVesting.new(
  45. beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner });
  46. this.token = await ERC20Mintable.new({ from: owner });
  47. await this.token.mint(this.vesting.address, amount, { from: owner });
  48. });
  49. it('can get state', async function () {
  50. (await this.vesting.beneficiary()).should.be.equal(beneficiary);
  51. (await this.vesting.cliff()).should.be.bignumber.equal(this.start.add(this.cliffDuration));
  52. (await this.vesting.start()).should.be.bignumber.equal(this.start);
  53. (await this.vesting.duration()).should.be.bignumber.equal(this.duration);
  54. (await this.vesting.revocable()).should.be.equal(true);
  55. });
  56. it('cannot be released before cliff', async function () {
  57. await shouldFail.reverting.withMessage(this.vesting.release(this.token.address),
  58. 'TokenVesting: no tokens are due'
  59. );
  60. });
  61. it('can be released after cliff', async function () {
  62. await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(1)));
  63. const { logs } = await this.vesting.release(this.token.address);
  64. expectEvent.inLogs(logs, 'TokensReleased', {
  65. token: this.token.address,
  66. amount: await this.token.balanceOf(beneficiary),
  67. });
  68. });
  69. it('should release proper amount after cliff', async function () {
  70. await time.increaseTo(this.start.add(this.cliffDuration));
  71. await this.vesting.release(this.token.address);
  72. const releaseTime = await time.latest();
  73. const releasedAmount = amount.mul(releaseTime.sub(this.start)).div(this.duration);
  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.sub(this.cliffDuration);
  79. const checkpoints = 4;
  80. for (let i = 1; i <= checkpoints; i++) {
  81. const now = this.start.add(this.cliffDuration).add((vestingPeriod.muln(i).divn(checkpoints)));
  82. await time.increaseTo(now);
  83. await this.vesting.release(this.token.address);
  84. const expectedVesting = amount.mul(now.sub(this.start)).div(this.duration);
  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.add(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.withMessage(vesting.revoke(this.token.address, { from: owner }),
  105. 'TokenVesting: cannot revoke'
  106. );
  107. });
  108. it('should return the non-vested tokens when revoked by owner', async function () {
  109. await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(12)));
  110. const vested = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
  111. await this.vesting.revoke(this.token.address, { from: owner });
  112. (await this.token.balanceOf(owner)).should.bignumber.equal(amount.sub(vested));
  113. });
  114. it('should keep the vested tokens when revoked by owner', async function () {
  115. await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(12)));
  116. const vestedPre = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
  117. await this.vesting.revoke(this.token.address, { from: owner });
  118. const vestedPost = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
  119. vestedPre.should.bignumber.equal(vestedPost);
  120. });
  121. it('should fail to be revoked a second time', async function () {
  122. await this.vesting.revoke(this.token.address, { from: owner });
  123. await shouldFail.reverting.withMessage(this.vesting.revoke(this.token.address, { from: owner }),
  124. 'TokenVesting: token already revoked'
  125. );
  126. });
  127. function vestedAmount (total, now, start, cliffDuration, duration) {
  128. return (now.lt(start.add(cliffDuration))) ? new BN(0) : total.mul((now.sub(start))).div(duration);
  129. }
  130. });
  131. });