ERC20Pausable.test.js 4.5 KB

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