ERC20Pausable.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const { BN } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { expectRevertCustomError } = require('../../../helpers/customError');
  4. const ERC20Pausable = artifacts.require('$ERC20Pausable');
  5. contract('ERC20Pausable', function (accounts) {
  6. const [holder, recipient, anotherAccount] = accounts;
  7. const initialSupply = new BN(100);
  8. const name = 'My Token';
  9. const symbol = 'MTKN';
  10. beforeEach(async function () {
  11. this.token = await ERC20Pausable.new(name, symbol);
  12. await this.token.$_mint(holder, initialSupply);
  13. });
  14. describe('pausable token', function () {
  15. describe('transfer', function () {
  16. it('allows to transfer when unpaused', async function () {
  17. await this.token.transfer(recipient, initialSupply, { from: holder });
  18. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  19. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(initialSupply);
  20. });
  21. it('allows to transfer when paused and then unpaused', async function () {
  22. await this.token.$_pause();
  23. await this.token.$_unpause();
  24. await this.token.transfer(recipient, initialSupply, { from: holder });
  25. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  26. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(initialSupply);
  27. });
  28. it('reverts when trying to transfer when paused', async function () {
  29. await this.token.$_pause();
  30. await expectRevertCustomError(
  31. this.token.transfer(recipient, initialSupply, { from: holder }),
  32. 'EnforcedPause',
  33. [],
  34. );
  35. });
  36. });
  37. describe('transfer from', function () {
  38. const allowance = new BN(40);
  39. beforeEach(async function () {
  40. await this.token.approve(anotherAccount, allowance, { from: holder });
  41. });
  42. it('allows to transfer from when unpaused', async function () {
  43. await this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount });
  44. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(allowance);
  45. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(allowance));
  46. });
  47. it('allows to transfer when paused and then unpaused', async function () {
  48. await this.token.$_pause();
  49. await this.token.$_unpause();
  50. await this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount });
  51. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(allowance);
  52. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(allowance));
  53. });
  54. it('reverts when trying to transfer from when paused', async function () {
  55. await this.token.$_pause();
  56. await expectRevertCustomError(
  57. this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount }),
  58. 'EnforcedPause',
  59. [],
  60. );
  61. });
  62. });
  63. describe('mint', function () {
  64. const amount = new BN('42');
  65. it('allows to mint when unpaused', async function () {
  66. await this.token.$_mint(recipient, amount);
  67. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  68. });
  69. it('allows to mint when paused and then unpaused', async function () {
  70. await this.token.$_pause();
  71. await this.token.$_unpause();
  72. await this.token.$_mint(recipient, amount);
  73. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  74. });
  75. it('reverts when trying to mint when paused', async function () {
  76. await this.token.$_pause();
  77. await expectRevertCustomError(this.token.$_mint(recipient, amount), 'EnforcedPause', []);
  78. });
  79. });
  80. describe('burn', function () {
  81. const amount = new BN('42');
  82. it('allows to burn when unpaused', async function () {
  83. await this.token.$_burn(holder, amount);
  84. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(amount));
  85. });
  86. it('allows to burn when paused and then unpaused', async function () {
  87. await this.token.$_pause();
  88. await this.token.$_unpause();
  89. await this.token.$_burn(holder, amount);
  90. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(amount));
  91. });
  92. it('reverts when trying to burn when paused', async function () {
  93. await this.token.$_pause();
  94. await expectRevertCustomError(this.token.$_burn(holder, amount), 'EnforcedPause', []);
  95. });
  96. });
  97. });
  98. });