TokenVesting.test.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const { BN, constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const ERC20Mintable = artifacts.require('ERC20Mintable');
  5. const TokenVesting = artifacts.require('TokenVesting');
  6. contract('TokenVesting', function ([_, owner, beneficiary]) {
  7. const amount = new BN('1000');
  8. beforeEach(async function () {
  9. // +1 minute so it starts after contract instantiation
  10. this.start = (await time.latest()).add(time.duration.minutes(1));
  11. this.cliffDuration = time.duration.years(1);
  12. this.duration = time.duration.years(2);
  13. });
  14. it('reverts with a duration shorter than the cliff', async function () {
  15. const cliffDuration = this.duration;
  16. const duration = this.cliffDuration;
  17. expect(cliffDuration).to.be.bignumber.that.is.at.least(duration);
  18. await expectRevert(
  19. TokenVesting.new(beneficiary, this.start, cliffDuration, duration, true, { from: owner }),
  20. 'TokenVesting: cliff is longer than duration'
  21. );
  22. });
  23. it('reverts with a null beneficiary', async function () {
  24. await expectRevert(
  25. TokenVesting.new(ZERO_ADDRESS, this.start, this.cliffDuration, this.duration, true, { from: owner }),
  26. 'TokenVesting: beneficiary is the zero address'
  27. );
  28. });
  29. it('reverts with a null duration', async function () {
  30. // cliffDuration should also be 0, since the duration must be larger than the cliff
  31. await expectRevert(
  32. TokenVesting.new(beneficiary, this.start, 0, 0, true, { from: owner }), 'TokenVesting: duration is 0'
  33. );
  34. });
  35. it('reverts if the end time is in the past', async function () {
  36. const now = await time.latest();
  37. this.start = now.sub(this.duration).sub(time.duration.minutes(1));
  38. await expectRevert(
  39. TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner }),
  40. 'TokenVesting: final time is before current time'
  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. expect(await this.vesting.beneficiary()).to.equal(beneficiary);
  52. expect(await this.vesting.cliff()).to.be.bignumber.equal(this.start.add(this.cliffDuration));
  53. expect(await this.vesting.start()).to.be.bignumber.equal(this.start);
  54. expect(await this.vesting.duration()).to.be.bignumber.equal(this.duration);
  55. expect(await this.vesting.revocable()).to.be.equal(true);
  56. });
  57. it('cannot be released before cliff', async function () {
  58. await expectRevert(this.vesting.release(this.token.address),
  59. 'TokenVesting: no tokens are due'
  60. );
  61. });
  62. it('can be released after cliff', async function () {
  63. await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(1)));
  64. const { logs } = await this.vesting.release(this.token.address);
  65. expectEvent.inLogs(logs, 'TokensReleased', {
  66. token: this.token.address,
  67. amount: await this.token.balanceOf(beneficiary),
  68. });
  69. });
  70. it('should release proper amount after cliff', async function () {
  71. await time.increaseTo(this.start.add(this.cliffDuration));
  72. await this.vesting.release(this.token.address);
  73. const releaseTime = await time.latest();
  74. const releasedAmount = amount.mul(releaseTime.sub(this.start)).div(this.duration);
  75. expect(await this.token.balanceOf(beneficiary)).to.be.bignumber.equal(releasedAmount);
  76. expect(await this.vesting.released(this.token.address)).to.be.bignumber.equal(releasedAmount);
  77. });
  78. it('should linearly release tokens during vesting period', async function () {
  79. const vestingPeriod = this.duration.sub(this.cliffDuration);
  80. const checkpoints = 4;
  81. for (let i = 1; i <= checkpoints; i++) {
  82. const now = this.start.add(this.cliffDuration).add((vestingPeriod.muln(i).divn(checkpoints)));
  83. await time.increaseTo(now);
  84. await this.vesting.release(this.token.address);
  85. const expectedVesting = amount.mul(now.sub(this.start)).div(this.duration);
  86. expect(await this.token.balanceOf(beneficiary)).to.be.bignumber.equal(expectedVesting);
  87. expect(await this.vesting.released(this.token.address)).to.be.bignumber.equal(expectedVesting);
  88. }
  89. });
  90. it('should have released all after end', async function () {
  91. await time.increaseTo(this.start.add(this.duration));
  92. await this.vesting.release(this.token.address);
  93. expect(await this.token.balanceOf(beneficiary)).to.be.bignumber.equal(amount);
  94. expect(await this.vesting.released(this.token.address)).to.be.bignumber.equal(amount);
  95. });
  96. it('should be revoked by owner if revocable is set', async function () {
  97. const { logs } = await this.vesting.revoke(this.token.address, { from: owner });
  98. expectEvent.inLogs(logs, 'TokenVestingRevoked', { token: this.token.address });
  99. expect(await this.vesting.revoked(this.token.address)).to.equal(true);
  100. });
  101. it('should fail to be revoked by owner if revocable not set', async function () {
  102. const vesting = await TokenVesting.new(
  103. beneficiary, this.start, this.cliffDuration, this.duration, false, { from: owner }
  104. );
  105. await expectRevert(vesting.revoke(this.token.address, { from: owner }),
  106. 'TokenVesting: cannot revoke'
  107. );
  108. });
  109. it('should return the non-vested tokens when revoked by owner', async function () {
  110. await time.increaseTo(this.start.add(this.cliffDuration).add(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. expect(await this.token.balanceOf(owner)).to.be.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.add(this.cliffDuration).add(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. expect(vestedPre).to.be.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 expectRevert(this.vesting.revoke(this.token.address, { from: owner }),
  125. 'TokenVesting: token already revoked'
  126. );
  127. });
  128. function vestedAmount (total, now, start, cliffDuration, duration) {
  129. return (now.lt(start.add(cliffDuration))) ? new BN(0) : total.mul((now.sub(start))).div(duration);
  130. }
  131. });
  132. });