TokenTimelock.test.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const { BN, expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ERC20Mock = artifacts.require('ERC20Mock');
  4. const TokenTimelock = artifacts.require('TokenTimelock');
  5. contract('TokenTimelock', function (accounts) {
  6. const [ beneficiary ] = accounts;
  7. const name = 'My Token';
  8. const symbol = 'MTKN';
  9. const amount = new BN(100);
  10. context('with token', function () {
  11. beforeEach(async function () {
  12. this.token = await ERC20Mock.new(name, symbol, beneficiary, 0); // We're not using the preminted tokens
  13. });
  14. it('rejects a release time in the past', async function () {
  15. const pastReleaseTime = (await time.latest()).sub(time.duration.years(1));
  16. await expectRevert(
  17. TokenTimelock.new(this.token.address, beneficiary, pastReleaseTime),
  18. 'TokenTimelock: release time is before current time',
  19. );
  20. });
  21. context('once deployed', function () {
  22. beforeEach(async function () {
  23. this.releaseTime = (await time.latest()).add(time.duration.years(1));
  24. this.timelock = await TokenTimelock.new(this.token.address, beneficiary, this.releaseTime);
  25. await this.token.mint(this.timelock.address, amount);
  26. });
  27. it('can get state', async function () {
  28. expect(await this.timelock.token()).to.equal(this.token.address);
  29. expect(await this.timelock.beneficiary()).to.equal(beneficiary);
  30. expect(await this.timelock.releaseTime()).to.be.bignumber.equal(this.releaseTime);
  31. });
  32. it('cannot be released before time limit', async function () {
  33. await expectRevert(this.timelock.release(), 'TokenTimelock: current time is before release time');
  34. });
  35. it('cannot be released just before time limit', async function () {
  36. await time.increaseTo(this.releaseTime.sub(time.duration.seconds(3)));
  37. await expectRevert(this.timelock.release(), 'TokenTimelock: current time is before release time');
  38. });
  39. it('can be released just after limit', async function () {
  40. await time.increaseTo(this.releaseTime.add(time.duration.seconds(1)));
  41. await this.timelock.release();
  42. expect(await this.token.balanceOf(beneficiary)).to.be.bignumber.equal(amount);
  43. });
  44. it('can be released after time limit', async function () {
  45. await time.increaseTo(this.releaseTime.add(time.duration.years(1)));
  46. await this.timelock.release();
  47. expect(await this.token.balanceOf(beneficiary)).to.be.bignumber.equal(amount);
  48. });
  49. it('cannot be released twice', async function () {
  50. await time.increaseTo(this.releaseTime.add(time.duration.years(1)));
  51. await this.timelock.release();
  52. await expectRevert(this.timelock.release(), 'TokenTimelock: no tokens to release');
  53. expect(await this.token.balanceOf(beneficiary)).to.be.bignumber.equal(amount);
  54. });
  55. });
  56. });
  57. });