ERC20PresetMinterPauser.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const { accounts, contract, web3 } = require('@openzeppelin/test-environment');
  2. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { ZERO_ADDRESS } = constants;
  4. const { expect } = require('chai');
  5. const ERC20PresetMinterPauser = contract.fromArtifact('ERC20PresetMinterPauser');
  6. describe('ERC20PresetMinterPauser', function () {
  7. const [ deployer, other ] = accounts;
  8. const name = 'MinterPauserToken';
  9. const symbol = 'DRT';
  10. const amount = new BN('5000');
  11. const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
  12. const MINTER_ROLE = web3.utils.soliditySha3('MINTER_ROLE');
  13. const PAUSER_ROLE = web3.utils.soliditySha3('PAUSER_ROLE');
  14. beforeEach(async function () {
  15. this.token = await ERC20PresetMinterPauser.new(name, symbol, { from: deployer });
  16. });
  17. it('deployer has the default admin role', async function () {
  18. expect(await this.token.getRoleMemberCount(DEFAULT_ADMIN_ROLE)).to.be.bignumber.equal('1');
  19. expect(await this.token.getRoleMember(DEFAULT_ADMIN_ROLE, 0)).to.equal(deployer);
  20. });
  21. it('deployer has the minter role', async function () {
  22. expect(await this.token.getRoleMemberCount(MINTER_ROLE)).to.be.bignumber.equal('1');
  23. expect(await this.token.getRoleMember(MINTER_ROLE, 0)).to.equal(deployer);
  24. });
  25. it('deployer has the pauser role', async function () {
  26. expect(await this.token.getRoleMemberCount(PAUSER_ROLE)).to.be.bignumber.equal('1');
  27. expect(await this.token.getRoleMember(PAUSER_ROLE, 0)).to.equal(deployer);
  28. });
  29. it('minter and pauser role admin is the default admin', async function () {
  30. expect(await this.token.getRoleAdmin(MINTER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  31. expect(await this.token.getRoleAdmin(PAUSER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  32. });
  33. describe('minting', function () {
  34. it('deployer can mint tokens', async function () {
  35. const receipt = await this.token.mint(other, amount, { from: deployer });
  36. expectEvent(receipt, 'Transfer', { from: ZERO_ADDRESS, to: other, value: amount });
  37. expect(await this.token.balanceOf(other)).to.be.bignumber.equal(amount);
  38. });
  39. it('other accounts cannot mint tokens', async function () {
  40. await expectRevert(
  41. this.token.mint(other, amount, { from: other }),
  42. 'ERC20PresetMinterPauser: must have minter role to mint'
  43. );
  44. });
  45. });
  46. describe('pausing', function () {
  47. it('deployer can pause', async function () {
  48. const receipt = await this.token.pause({ from: deployer });
  49. expectEvent(receipt, 'Paused', { account: deployer });
  50. expect(await this.token.paused()).to.equal(true);
  51. });
  52. it('deployer can unpause', async function () {
  53. await this.token.pause({ from: deployer });
  54. const receipt = await this.token.unpause({ from: deployer });
  55. expectEvent(receipt, 'Unpaused', { account: deployer });
  56. expect(await this.token.paused()).to.equal(false);
  57. });
  58. it('cannot mint while paused', async function () {
  59. await this.token.pause({ from: deployer });
  60. await expectRevert(
  61. this.token.mint(other, amount, { from: deployer }),
  62. 'ERC20Pausable: token transfer while paused'
  63. );
  64. });
  65. it('other accounts cannot pause', async function () {
  66. await expectRevert(this.token.pause({ from: other }), 'ERC20PresetMinterPauser: must have pauser role to pause');
  67. });
  68. });
  69. describe('burning', function () {
  70. it('holders can burn their tokens', async function () {
  71. await this.token.mint(other, amount, { from: deployer });
  72. const receipt = await this.token.burn(amount.subn(1), { from: other });
  73. expectEvent(receipt, 'Transfer', { from: other, to: ZERO_ADDRESS, value: amount.subn(1) });
  74. expect(await this.token.balanceOf(other)).to.be.bignumber.equal('1');
  75. });
  76. });
  77. });