TokenVesting.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import EVMRevert from '../../helpers/EVMRevert';
  2. import latestTime from '../../helpers/latestTime';
  3. import { increaseTimeTo, duration } from '../../helpers/increaseTime';
  4. import { ethGetBlock } from '../../helpers/web3';
  5. const BigNumber = web3.BigNumber;
  6. require('chai')
  7. .use(require('chai-as-promised'))
  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 this.vesting.release(this.token.address).should.be.rejectedWith(EVMRevert);
  24. });
  25. it('can be released after cliff', async function () {
  26. await increaseTimeTo(this.start + this.cliff + duration.weeks(1));
  27. await this.vesting.release(this.token.address).should.be.fulfilled;
  28. });
  29. it('should release proper amount after cliff', async function () {
  30. await increaseTimeTo(this.start + this.cliff);
  31. const { receipt } = await this.vesting.release(this.token.address);
  32. const block = await ethGetBlock(receipt.blockNumber);
  33. const releaseTime = block.timestamp;
  34. const balance = await this.token.balanceOf(beneficiary);
  35. balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.duration).floor());
  36. });
  37. it('should linearly release tokens during vesting period', async function () {
  38. const vestingPeriod = this.duration - this.cliff;
  39. const checkpoints = 4;
  40. for (let i = 1; i <= checkpoints; i++) {
  41. const now = this.start + this.cliff + i * (vestingPeriod / checkpoints);
  42. await increaseTimeTo(now);
  43. await this.vesting.release(this.token.address);
  44. const balance = await this.token.balanceOf(beneficiary);
  45. const expectedVesting = amount.mul(now - this.start).div(this.duration).floor();
  46. balance.should.bignumber.equal(expectedVesting);
  47. }
  48. });
  49. it('should have released all after end', async function () {
  50. await increaseTimeTo(this.start + this.duration);
  51. await this.vesting.release(this.token.address);
  52. const balance = await this.token.balanceOf(beneficiary);
  53. balance.should.bignumber.equal(amount);
  54. });
  55. it('should be revoked by owner if revocable is set', async function () {
  56. await this.vesting.revoke(this.token.address, { from: owner }).should.be.fulfilled;
  57. });
  58. it('should fail to be revoked by owner if revocable not set', async function () {
  59. const vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, false, { from: owner });
  60. await vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMRevert);
  61. });
  62. it('should return the non-vested tokens when revoked by owner', async function () {
  63. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  64. const vested = await this.vesting.vestedAmount(this.token.address);
  65. await this.vesting.revoke(this.token.address, { from: owner });
  66. const ownerBalance = await this.token.balanceOf(owner);
  67. ownerBalance.should.bignumber.equal(amount.sub(vested));
  68. });
  69. it('should keep the vested tokens when revoked by owner', async function () {
  70. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  71. const vestedPre = await this.vesting.vestedAmount(this.token.address);
  72. await this.vesting.revoke(this.token.address, { from: owner });
  73. const vestedPost = await this.vesting.vestedAmount(this.token.address);
  74. vestedPre.should.bignumber.equal(vestedPost);
  75. });
  76. it('should fail to be revoked a second time', async function () {
  77. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  78. await this.vesting.vestedAmount(this.token.address);
  79. await this.vesting.revoke(this.token.address, { from: owner });
  80. await this.vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMRevert);
  81. });
  82. });