TokenVesting.test.js 6.0 KB

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