ERC1155PresetMinterPauser.test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 ERC1155PresetMinterPauser = contract.fromArtifact('ERC1155PresetMinterPauser');
  6. describe('ERC1155PresetMinterPauser', function () {
  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. it('deployer has the default admin role', async function () {
  20. expect(await this.token.getRoleMemberCount(DEFAULT_ADMIN_ROLE)).to.be.bignumber.equal('1');
  21. expect(await this.token.getRoleMember(DEFAULT_ADMIN_ROLE, 0)).to.equal(deployer);
  22. });
  23. it('deployer has the minter role', async function () {
  24. expect(await this.token.getRoleMemberCount(MINTER_ROLE)).to.be.bignumber.equal('1');
  25. expect(await this.token.getRoleMember(MINTER_ROLE, 0)).to.equal(deployer);
  26. });
  27. it('deployer has the pauser role', async function () {
  28. expect(await this.token.getRoleMemberCount(PAUSER_ROLE)).to.be.bignumber.equal('1');
  29. expect(await this.token.getRoleMember(PAUSER_ROLE, 0)).to.equal(deployer);
  30. });
  31. it('minter and pauser role admin is the default admin', async function () {
  32. expect(await this.token.getRoleAdmin(MINTER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  33. expect(await this.token.getRoleAdmin(PAUSER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  34. });
  35. describe('minting', function () {
  36. it('deployer can mint tokens', async function () {
  37. const receipt = await this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer });
  38. expectEvent(receipt, 'TransferSingle',
  39. { operator: deployer, from: ZERO_ADDRESS, to: other, value: firstTokenIdAmount, id: firstTokenId },
  40. );
  41. expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount);
  42. });
  43. it('other accounts cannot mint tokens', async function () {
  44. await expectRevert(
  45. this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: other }),
  46. 'ERC1155PresetMinterPauser: must have minter role to mint',
  47. );
  48. });
  49. });
  50. describe('batched minting', function () {
  51. it('deployer can batch mint tokens', async function () {
  52. const receipt = await this.token.mintBatch(
  53. other, [firstTokenId, secondTokenId], [firstTokenIdAmount, secondTokenIdAmount], '0x', { from: deployer },
  54. );
  55. expectEvent(receipt, 'TransferBatch',
  56. { operator: deployer, from: ZERO_ADDRESS, to: other },
  57. );
  58. expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount);
  59. });
  60. it('other accounts cannot batch mint tokens', async function () {
  61. await expectRevert(
  62. this.token.mintBatch(
  63. other, [firstTokenId, secondTokenId], [firstTokenIdAmount, secondTokenIdAmount], '0x', { from: other },
  64. ),
  65. 'ERC1155PresetMinterPauser: must have minter role to mint',
  66. );
  67. });
  68. });
  69. describe('pausing', function () {
  70. it('deployer can pause', async function () {
  71. const receipt = await this.token.pause({ from: deployer });
  72. expectEvent(receipt, 'Paused', { account: deployer });
  73. expect(await this.token.paused()).to.equal(true);
  74. });
  75. it('deployer can unpause', async function () {
  76. await this.token.pause({ from: deployer });
  77. const receipt = await this.token.unpause({ from: deployer });
  78. expectEvent(receipt, 'Unpaused', { account: deployer });
  79. expect(await this.token.paused()).to.equal(false);
  80. });
  81. it('cannot mint while paused', async function () {
  82. await this.token.pause({ from: deployer });
  83. await expectRevert(
  84. this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer }),
  85. 'ERC1155Pausable: token transfer while paused',
  86. );
  87. });
  88. it('other accounts cannot pause', async function () {
  89. await expectRevert(
  90. this.token.pause({ from: other }),
  91. 'ERC1155PresetMinterPauser: must have pauser role to pause',
  92. );
  93. });
  94. });
  95. describe('burning', function () {
  96. it('holders can burn their tokens', async function () {
  97. await this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer });
  98. const receipt = await this.token.burn(other, firstTokenId, firstTokenIdAmount.subn(1), { from: other });
  99. expectEvent(receipt, 'TransferSingle',
  100. { operator: other, from: other, to: ZERO_ADDRESS, value: firstTokenIdAmount.subn(1), id: firstTokenId },
  101. );
  102. expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal('1');
  103. });
  104. });
  105. });