ERC20Pausable.test.js 4.6 KB

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