ERC20Pausable.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const { BN, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ERC20PausableMock = artifacts.require('ERC20PausableMock');
  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 ERC20PausableMock.new(name, symbol, holder, initialSupply);
  11. });
  12. describe('pausable token', function () {
  13. describe('transfer', function () {
  14. it('allows to transfer when unpaused', async function () {
  15. await this.token.transfer(recipient, initialSupply, { from: holder });
  16. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  17. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(initialSupply);
  18. });
  19. it('allows to transfer when paused and then unpaused', async function () {
  20. await this.token.pause();
  21. await this.token.unpause();
  22. await this.token.transfer(recipient, initialSupply, { from: holder });
  23. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  24. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(initialSupply);
  25. });
  26. it('reverts when trying to transfer when paused', async function () {
  27. await this.token.pause();
  28. await expectRevert(this.token.transfer(recipient, initialSupply, { from: holder }),
  29. 'ERC20Pausable: token transfer while paused',
  30. );
  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 expectRevert(this.token.transferFrom(
  53. holder, recipient, allowance, { from: anotherAccount }), 'ERC20Pausable: token transfer while paused',
  54. );
  55. });
  56. });
  57. describe('mint', function () {
  58. const amount = new BN('42');
  59. it('allows to mint when unpaused', async function () {
  60. await this.token.mint(recipient, amount);
  61. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  62. });
  63. it('allows to mint when paused and then unpaused', async function () {
  64. await this.token.pause();
  65. await this.token.unpause();
  66. await this.token.mint(recipient, amount);
  67. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  68. });
  69. it('reverts when trying to mint when paused', async function () {
  70. await this.token.pause();
  71. await expectRevert(this.token.mint(recipient, amount),
  72. 'ERC20Pausable: token transfer while paused',
  73. );
  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 expectRevert(this.token.burn(holder, amount),
  91. 'ERC20Pausable: token transfer while paused',
  92. );
  93. });
  94. });
  95. });
  96. });