ERC1155PresetMinterPauser.test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, from: ZERO_ADDRESS, to: other, value: firstTokenIdAmount, id: firstTokenId },
  41. );
  42. expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount);
  43. });
  44. it('other accounts cannot mint tokens', async function () {
  45. await expectRevert(
  46. this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: other }),
  47. 'ERC1155PresetMinterPauser: must have minter role to mint',
  48. );
  49. });
  50. });
  51. describe('batched minting', function () {
  52. it('deployer can batch mint tokens', async function () {
  53. const receipt = await this.token.mintBatch(
  54. other, [firstTokenId, secondTokenId], [firstTokenIdAmount, secondTokenIdAmount], '0x', { from: deployer },
  55. );
  56. expectEvent(receipt, 'TransferBatch',
  57. { operator: deployer, from: ZERO_ADDRESS, to: other },
  58. );
  59. expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount);
  60. });
  61. it('other accounts cannot batch mint tokens', async function () {
  62. await expectRevert(
  63. this.token.mintBatch(
  64. other, [firstTokenId, secondTokenId], [firstTokenIdAmount, secondTokenIdAmount], '0x', { from: other },
  65. ),
  66. 'ERC1155PresetMinterPauser: must have minter role to mint',
  67. );
  68. });
  69. });
  70. describe('pausing', function () {
  71. it('deployer can pause', async function () {
  72. const receipt = await this.token.pause({ from: deployer });
  73. expectEvent(receipt, 'Paused', { account: deployer });
  74. expect(await this.token.paused()).to.equal(true);
  75. });
  76. it('deployer can unpause', async function () {
  77. await this.token.pause({ from: deployer });
  78. const receipt = await this.token.unpause({ from: deployer });
  79. expectEvent(receipt, 'Unpaused', { account: deployer });
  80. expect(await this.token.paused()).to.equal(false);
  81. });
  82. it('cannot mint while paused', async function () {
  83. await this.token.pause({ from: deployer });
  84. await expectRevert(
  85. this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer }),
  86. 'ERC1155Pausable: token transfer while paused',
  87. );
  88. });
  89. it('other accounts cannot pause', async function () {
  90. await expectRevert(
  91. this.token.pause({ from: other }),
  92. 'ERC1155PresetMinterPauser: must have pauser role to pause',
  93. );
  94. });
  95. });
  96. describe('burning', function () {
  97. it('holders can burn their tokens', async function () {
  98. await this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer });
  99. const receipt = await this.token.burn(other, firstTokenId, firstTokenIdAmount.subn(1), { from: other });
  100. expectEvent(receipt, 'TransferSingle',
  101. { operator: other, from: other, to: ZERO_ADDRESS, value: firstTokenIdAmount.subn(1), id: firstTokenId },
  102. );
  103. expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal('1');
  104. });
  105. });
  106. });