TokenVesting.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. beforeEach(async function () {
  15. this.token = await MintableToken.new({ from: owner });
  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. this.vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, true, { from: owner });
  20. await this.token.mint(this.vesting.address, amount, { from: owner });
  21. });
  22. it('cannot be released before cliff', async function () {
  23. await expectThrow(
  24. this.vesting.release(this.token.address),
  25. EVMRevert,
  26. );
  27. });
  28. it('can be released after cliff', async function () {
  29. await increaseTimeTo(this.start + this.cliff + duration.weeks(1));
  30. await this.vesting.release(this.token.address);
  31. });
  32. it('should release proper amount after cliff', async function () {
  33. await increaseTimeTo(this.start + this.cliff);
  34. const { receipt } = await this.vesting.release(this.token.address);
  35. const block = await ethGetBlock(receipt.blockNumber);
  36. const releaseTime = block.timestamp;
  37. const balance = await this.token.balanceOf(beneficiary);
  38. balance.should.bignumber.eq(amount.mul(releaseTime - this.start).div(this.duration).floor());
  39. });
  40. it('should linearly release tokens during vesting period', async function () {
  41. const vestingPeriod = this.duration - this.cliff;
  42. const checkpoints = 4;
  43. for (let i = 1; i <= checkpoints; i++) {
  44. const now = this.start + this.cliff + i * (vestingPeriod / checkpoints);
  45. await increaseTimeTo(now);
  46. await this.vesting.release(this.token.address);
  47. const balance = await this.token.balanceOf(beneficiary);
  48. const expectedVesting = amount.mul(now - this.start).div(this.duration).floor();
  49. balance.should.bignumber.eq(expectedVesting);
  50. }
  51. });
  52. it('should have released all after end', async function () {
  53. await increaseTimeTo(this.start + this.duration);
  54. await this.vesting.release(this.token.address);
  55. const balance = await this.token.balanceOf(beneficiary);
  56. balance.should.bignumber.eq(amount);
  57. });
  58. it('should be revoked by owner if revocable is set', async function () {
  59. await this.vesting.revoke(this.token.address, { from: owner });
  60. });
  61. it('should fail to be revoked by owner if revocable not set', async function () {
  62. const vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, false, { from: owner });
  63. await expectThrow(
  64. vesting.revoke(this.token.address, { from: owner }),
  65. EVMRevert,
  66. );
  67. });
  68. it('should return the non-vested tokens when revoked by owner', async function () {
  69. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  70. const vested = await this.vesting.vestedAmount(this.token.address);
  71. await this.vesting.revoke(this.token.address, { from: owner });
  72. const ownerBalance = await this.token.balanceOf(owner);
  73. ownerBalance.should.bignumber.eq(amount.sub(vested));
  74. });
  75. it('should keep the vested tokens when revoked by owner', async function () {
  76. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  77. const vestedPre = await this.vesting.vestedAmount(this.token.address);
  78. await this.vesting.revoke(this.token.address, { from: owner });
  79. const vestedPost = await this.vesting.vestedAmount(this.token.address);
  80. vestedPre.should.bignumber.eq(vestedPost);
  81. });
  82. it('should fail to be revoked a second time', async function () {
  83. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  84. await this.vesting.vestedAmount(this.token.address);
  85. await this.vesting.revoke(this.token.address, { from: owner });
  86. await expectThrow(
  87. this.vesting.revoke(this.token.address, { from: owner }),
  88. EVMRevert,
  89. );
  90. });
  91. });