TokenVesting.test.js 6.0 KB

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