TokenVesting.test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 ERC20Mintable = artifacts.require('ERC20Mintable');
  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 ERC20Mintable.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. (await this.token.balanceOf(beneficiary)).should.bignumber.equal(
  55. amount.mul(releaseTime - this.start).div(this.duration).floor()
  56. );
  57. });
  58. it('should linearly release tokens during vesting period', async function () {
  59. const vestingPeriod = this.duration - this.cliff;
  60. const checkpoints = 4;
  61. for (let i = 1; i <= checkpoints; i++) {
  62. const now = this.start + this.cliff + i * (vestingPeriod / checkpoints);
  63. await increaseTimeTo(now);
  64. await this.vesting.release(this.token.address);
  65. const expectedVesting = amount.mul(now - this.start).div(this.duration).floor();
  66. (await this.token.balanceOf(beneficiary)).should.bignumber.equal(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. (await this.token.balanceOf(beneficiary)).should.bignumber.equal(amount);
  73. });
  74. it('should be revoked by owner if revocable is set', async function () {
  75. await this.vesting.revoke(this.token.address, { from: owner });
  76. });
  77. it('should fail to be revoked by owner if revocable not set', async function () {
  78. const vesting = await TokenVesting.new(
  79. beneficiary, this.start, this.cliff, this.duration, false, { from: owner }
  80. );
  81. await expectThrow(
  82. vesting.revoke(this.token.address, { from: owner }),
  83. EVMRevert,
  84. );
  85. });
  86. it('should return the non-vested tokens when revoked by owner', async function () {
  87. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  88. const vested = await this.vesting.vestedAmount(this.token.address);
  89. await this.vesting.revoke(this.token.address, { from: owner });
  90. (await this.token.balanceOf(owner)).should.bignumber.equal(amount.sub(vested));
  91. });
  92. it('should keep the vested tokens when revoked by owner', async function () {
  93. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  94. const vestedPre = await this.vesting.vestedAmount(this.token.address);
  95. await this.vesting.revoke(this.token.address, { from: owner });
  96. const vestedPost = await this.vesting.vestedAmount(this.token.address);
  97. vestedPre.should.bignumber.equal(vestedPost);
  98. });
  99. it('should fail to be revoked a second time', async function () {
  100. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  101. await this.vesting.vestedAmount(this.token.address);
  102. await this.vesting.revoke(this.token.address, { from: owner });
  103. await expectThrow(
  104. this.vesting.revoke(this.token.address, { from: owner }),
  105. EVMRevert,
  106. );
  107. });
  108. });
  109. });