ERC1155PresetMinterPauser.test.js 5.0 KB

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