ERC20Pausable.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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(this.token.transfer(recipient, initialSupply, { from: holder }), 'Paused', []);
  31. });
  32. });
  33. describe('transfer from', function () {
  34. const allowance = new BN(40);
  35. beforeEach(async function () {
  36. await this.token.approve(anotherAccount, allowance, { from: holder });
  37. });
  38. it('allows to transfer from when unpaused', async function () {
  39. await this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount });
  40. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(allowance);
  41. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(allowance));
  42. });
  43. it('allows to transfer when paused and then unpaused', async function () {
  44. await this.token.$_pause();
  45. await this.token.$_unpause();
  46. await this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount });
  47. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(allowance);
  48. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(allowance));
  49. });
  50. it('reverts when trying to transfer from when paused', async function () {
  51. await this.token.$_pause();
  52. await expectRevertCustomError(
  53. this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount }),
  54. 'Paused',
  55. [],
  56. );
  57. });
  58. });
  59. describe('mint', function () {
  60. const amount = new BN('42');
  61. it('allows to mint when unpaused', async function () {
  62. await this.token.$_mint(recipient, amount);
  63. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  64. });
  65. it('allows to mint when paused and then unpaused', async function () {
  66. await this.token.$_pause();
  67. await this.token.$_unpause();
  68. await this.token.$_mint(recipient, amount);
  69. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  70. });
  71. it('reverts when trying to mint when paused', async function () {
  72. await this.token.$_pause();
  73. await expectRevertCustomError(this.token.$_mint(recipient, amount), 'Paused', []);
  74. });
  75. });
  76. describe('burn', function () {
  77. const amount = new BN('42');
  78. it('allows to burn when unpaused', async function () {
  79. await this.token.$_burn(holder, amount);
  80. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(amount));
  81. });
  82. it('allows to burn when paused and then unpaused', async function () {
  83. await this.token.$_pause();
  84. await this.token.$_unpause();
  85. await this.token.$_burn(holder, amount);
  86. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(amount));
  87. });
  88. it('reverts when trying to burn when paused', async function () {
  89. await this.token.$_pause();
  90. await expectRevertCustomError(this.token.$_burn(holder, amount), 'Paused', []);
  91. });
  92. });
  93. });
  94. });