TokenTimelock.test.js 2.9 KB

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