TokenVesting.test.js 5.2 KB

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