TokenVesting.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const BigNumber = web3.BigNumber
  2. require('chai')
  3. .use(require('chai-as-promised'))
  4. .use(require('chai-bignumber')(BigNumber))
  5. .should();
  6. import EVMThrow from './helpers/EVMThrow'
  7. import latestTime from './helpers/latestTime';
  8. import {increaseTimeTo, duration} from './helpers/increaseTime';
  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.cliff = latestTime() + duration.years(1);
  16. this.end = latestTime() + duration.years(2);
  17. this.vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, true, { from: owner });
  18. this.start = latestTime(); // gets the timestamp at construction
  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(EVMThrow);
  23. });
  24. it('can be released after cliff', async function () {
  25. await increaseTimeTo(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.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.end - this.start).floor());
  34. });
  35. it('should linearly release tokens during vesting period');
  36. it('should have released all after end', async function () {
  37. await increaseTimeTo(this.end);
  38. await this.vesting.release(this.token.address);
  39. const balance = await this.token.balanceOf(beneficiary);
  40. balance.should.bignumber.equal(amount);
  41. });
  42. it('should not fail to be revoked by owner if revocable is set', async function () {
  43. const vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, true, { from: owner } );
  44. await vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMThrow);
  45. });
  46. it('should fail to be revoked by owner if revocable not set', async function () {
  47. const vesting = await TokenVesting.new(beneficiary, this.cliff, this.end, false, { from: owner } );
  48. await vesting.revoke(this.token.address, { from: owner }).should.be.rejectedWith(EVMThrow);
  49. });
  50. it('should return the non-vested tokens when revoked by owner', async function () {
  51. await increaseTimeTo(this.cliff + duration.weeks(1));
  52. await this.vesting.release(this.token.address);
  53. const vested = await this.vesting.vestedAmount(this.token.address);
  54. const balance = await this.token.balanceOf(this.vesting.address);
  55. await this.vesting.revoke(this.token.address, { from: owner });
  56. const ownerBalance = await this.token.balanceOf(owner);
  57. ownerBalance.should.bignumber.equal(balance.sub(vested));
  58. });
  59. it('should keep the vested tokens when revoked by owner', async function () {
  60. await increaseTimeTo(this.cliff + duration.weeks(1));
  61. await this.vesting.release(this.token.address);
  62. const vested = await this.vesting.vestedAmount(this.token.address);
  63. await this.vesting.revoke(this.token.address, { from: owner });
  64. const balance = await this.token.balanceOf(this.vesting.address);
  65. balance.should.bignumber.equal(vested);
  66. });
  67. });