TokenVesting.test.js 7.1 KB

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