TokenTimelock.test.js 2.9 KB

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