TokenVesting.test.js 6.6 KB

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