TokenVesting.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import EVMRevert from './helpers/EVMRevert';
  2. import latestTime from './helpers/latestTime';
  3. import { increaseTimeTo, duration } from './helpers/increaseTime';
  4. const BigNumber = web3.BigNumber;
  5. require('chai')
  6. .use(require('chai-as-promised'))
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should();
  9. const MintableToken = artifacts.require('MintableToken');
  10. const TokenVesting = artifacts.require('TokenVesting');
  11. contract('TokenVesting', function ([_, owner, beneficiary]) {
  12. const amount = new BigNumber(1000);
  13. beforeEach(async function () {
  14. this.token = await MintableToken.new({ from: owner });
  15. this.start = latestTime() + duration.minutes(1); // +1 minute so it starts after contract instantiation
  16. this.cliff = duration.years(1);
  17. this.duration = duration.years(2);
  18. this.vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, true, { from: owner });
  19. await this.token.mint(this.vesting.address, amount, { from: owner });
  20. });
  21. it('cannot be released before cliff', async function () {
  22. await this.vesting.release(this.token.address).should.be.rejectedWith(EVMRevert);
  23. });
  24. it('can be released after cliff', async function () {
  25. await increaseTimeTo(this.start + this.cliff + duration.weeks(1));
  26. await this.vesting.release(this.token.address).should.be.fulfilled;
  27. });
  28. it('should release proper amount after cliff', async function () {
  29. await increaseTimeTo(this.start + this.cliff);
  30. const { receipt } = await this.vesting.release(this.token.address);
  31. const releaseTime = web3.eth.getBlock(receipt.blockNumber).timestamp;
  32. const balance = await this.token.balanceOf(beneficiary);
  33. balance.should.bignumber.equal(amount.mul(releaseTime - this.start).div(this.duration).floor());
  34. });
  35. it('should linearly release tokens during vesting period', async function () {
  36. const vestingPeriod = this.duration - this.cliff;
  37. const checkpoints = 4;
  38. for (let i = 1; i <= checkpoints; i++) {
  39. const now = this.start + this.cliff + i * (vestingPeriod / checkpoints);
  40. await increaseTimeTo(now);
  41. await this.vesting.release(this.token.address);
  42. const balance = await this.token.balanceOf(beneficiary);
  43. const expectedVesting = amount.mul(now - this.start).div(this.duration).floor();
  44. balance.should.bignumber.equal(expectedVesting);
  45. }
  46. });
  47. it('should have released all after end', async function () {
  48. await increaseTimeTo(this.start + this.duration);
  49. await this.vesting.release(this.token.address);
  50. const balance = await this.token.balanceOf(beneficiary);
  51. balance.should.bignumber.equal(amount);
  52. });
  53. it('should be revoked by owner if revocable is set', async function () {
  54. await this.vesting.revoke(this.token.address, { from: owner }).should.be.fulfilled;
  55. });
  56. it('should fail to be revoked by owner if revocable not set', async function () {
  57. const vesting = await TokenVesting.new(beneficiary, this.start, this.cliff, this.duration, false, { from: owner });
  58. await vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMRevert);
  59. });
  60. it('should return the non-vested tokens when revoked by owner', async function () {
  61. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  62. const vested = await this.vesting.vestedAmount(this.token.address);
  63. await this.vesting.revoke(this.token.address, { from: owner });
  64. const ownerBalance = await this.token.balanceOf(owner);
  65. ownerBalance.should.bignumber.equal(amount.sub(vested));
  66. });
  67. it('should keep the vested tokens when revoked by owner', async function () {
  68. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  69. const vestedPre = await this.vesting.vestedAmount(this.token.address);
  70. await this.vesting.revoke(this.token.address, { from: owner });
  71. const vestedPost = await this.vesting.vestedAmount(this.token.address);
  72. vestedPre.should.bignumber.equal(vestedPost);
  73. });
  74. it('should fail to be revoked a second time', async function () {
  75. await increaseTimeTo(this.start + this.cliff + duration.weeks(12));
  76. await this.vesting.vestedAmount(this.token.address);
  77. await this.vesting.revoke(this.token.address, { from: owner });
  78. await this.vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMRevert);
  79. });
  80. });