ERC1155PresetMinterPauser.test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
  4. const { expect } = require('chai');
  5. const ERC1155PresetMinterPauser = artifacts.require('ERC1155PresetMinterPauser');
  6. contract('ERC1155PresetMinterPauser', function (accounts) {
  7. const [deployer, other] = accounts;
  8. const firstTokenId = new BN('845');
  9. const firstTokenIdAmount = new BN('5000');
  10. const secondTokenId = new BN('48324');
  11. const secondTokenIdAmount = new BN('77875');
  12. const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
  13. const MINTER_ROLE = web3.utils.soliditySha3('MINTER_ROLE');
  14. const PAUSER_ROLE = web3.utils.soliditySha3('PAUSER_ROLE');
  15. const uri = 'https://token.com';
  16. beforeEach(async function () {
  17. this.token = await ERC1155PresetMinterPauser.new(uri, { from: deployer });
  18. });
  19. shouldSupportInterfaces(['ERC1155', 'AccessControl', 'AccessControlEnumerable']);
  20. it('deployer has the default admin role', async function () {
  21. expect(await this.token.getRoleMemberCount(DEFAULT_ADMIN_ROLE)).to.be.bignumber.equal('1');
  22. expect(await this.token.getRoleMember(DEFAULT_ADMIN_ROLE, 0)).to.equal(deployer);
  23. });
  24. it('deployer has the minter role', async function () {
  25. expect(await this.token.getRoleMemberCount(MINTER_ROLE)).to.be.bignumber.equal('1');
  26. expect(await this.token.getRoleMember(MINTER_ROLE, 0)).to.equal(deployer);
  27. });
  28. it('deployer has the pauser role', async function () {
  29. expect(await this.token.getRoleMemberCount(PAUSER_ROLE)).to.be.bignumber.equal('1');
  30. expect(await this.token.getRoleMember(PAUSER_ROLE, 0)).to.equal(deployer);
  31. });
  32. it('minter and pauser role admin is the default admin', async function () {
  33. expect(await this.token.getRoleAdmin(MINTER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  34. expect(await this.token.getRoleAdmin(PAUSER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  35. });
  36. describe('minting', function () {
  37. it('deployer can mint tokens', async function () {
  38. const receipt = await this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer });
  39. expectEvent(receipt, 'TransferSingle', {
  40. operator: deployer,
  41. from: ZERO_ADDRESS,
  42. to: other,
  43. value: firstTokenIdAmount,
  44. id: firstTokenId,
  45. });
  46. expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount);
  47. });
  48. it('other accounts cannot mint tokens', async function () {
  49. await expectRevert(
  50. this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: other }),
  51. 'ERC1155PresetMinterPauser: must have minter role to mint',
  52. );
  53. });
  54. });
  55. describe('batched minting', function () {
  56. it('deployer can batch mint tokens', async function () {
  57. const receipt = await this.token.mintBatch(
  58. other,
  59. [firstTokenId, secondTokenId],
  60. [firstTokenIdAmount, secondTokenIdAmount],
  61. '0x',
  62. { from: deployer },
  63. );
  64. expectEvent(receipt, 'TransferBatch', { operator: deployer, from: ZERO_ADDRESS, to: other });
  65. expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount);
  66. });
  67. it('other accounts cannot batch mint tokens', async function () {
  68. await expectRevert(
  69. this.token.mintBatch(other, [firstTokenId, secondTokenId], [firstTokenIdAmount, secondTokenIdAmount], '0x', {
  70. from: other,
  71. }),
  72. 'ERC1155PresetMinterPauser: must have minter role to mint',
  73. );
  74. });
  75. });
  76. describe('pausing', function () {
  77. it('deployer can pause', async function () {
  78. const receipt = await this.token.pause({ from: deployer });
  79. expectEvent(receipt, 'Paused', { account: deployer });
  80. expect(await this.token.paused()).to.equal(true);
  81. });
  82. it('deployer can unpause', async function () {
  83. await this.token.pause({ from: deployer });
  84. const receipt = await this.token.unpause({ from: deployer });
  85. expectEvent(receipt, 'Unpaused', { account: deployer });
  86. expect(await this.token.paused()).to.equal(false);
  87. });
  88. it('cannot mint while paused', async function () {
  89. await this.token.pause({ from: deployer });
  90. await expectRevert(
  91. this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer }),
  92. 'ERC1155Pausable: token transfer while paused',
  93. );
  94. });
  95. it('other accounts cannot pause', async function () {
  96. await expectRevert(
  97. this.token.pause({ from: other }),
  98. 'ERC1155PresetMinterPauser: must have pauser role to pause',
  99. );
  100. });
  101. it('other accounts cannot unpause', async function () {
  102. await this.token.pause({ from: deployer });
  103. await expectRevert(
  104. this.token.unpause({ from: other }),
  105. 'ERC1155PresetMinterPauser: must have pauser role to unpause',
  106. );
  107. });
  108. });
  109. describe('burning', function () {
  110. it('holders can burn their tokens', async function () {
  111. await this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer });
  112. const receipt = await this.token.burn(other, firstTokenId, firstTokenIdAmount.subn(1), { from: other });
  113. expectEvent(receipt, 'TransferSingle', {
  114. operator: other,
  115. from: other,
  116. to: ZERO_ADDRESS,
  117. value: firstTokenIdAmount.subn(1),
  118. id: firstTokenId,
  119. });
  120. expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal('1');
  121. });
  122. });
  123. });