ERC1155Burnable.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const ids = [42n, 1137n];
  5. const values = [3000n, 9902n];
  6. async function fixture() {
  7. const [holder, operator, other] = await ethers.getSigners();
  8. const token = await ethers.deployContract('$ERC1155Burnable', ['https://token-cdn-domain/{id}.json']);
  9. await token.$_mint(holder, ids[0], values[0], '0x');
  10. await token.$_mint(holder, ids[1], values[1], '0x');
  11. return { token, holder, operator, other };
  12. }
  13. describe('ERC1155Burnable', function () {
  14. beforeEach(async function () {
  15. Object.assign(this, await loadFixture(fixture));
  16. });
  17. describe('burn', function () {
  18. it('holder can burn their tokens', async function () {
  19. await this.token.connect(this.holder).burn(this.holder, ids[0], values[0] - 1n);
  20. expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
  21. });
  22. it("approved operators can burn the holder's tokens", async function () {
  23. await this.token.connect(this.holder).setApprovalForAll(this.operator, true);
  24. await this.token.connect(this.operator).burn(this.holder, ids[0], values[0] - 1n);
  25. expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
  26. });
  27. it("unapproved accounts cannot burn the holder's tokens", async function () {
  28. await expect(this.token.connect(this.other).burn(this.holder, ids[0], values[0] - 1n))
  29. .to.be.revertedWithCustomError(this.token, 'ERC1155MissingApprovalForAll')
  30. .withArgs(this.other, this.holder);
  31. });
  32. });
  33. describe('burnBatch', function () {
  34. it('holder can burn their tokens', async function () {
  35. await this.token.connect(this.holder).burnBatch(this.holder, ids, [values[0] - 1n, values[1] - 2n]);
  36. expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
  37. expect(await this.token.balanceOf(this.holder, ids[1])).to.equal(2n);
  38. });
  39. it("approved operators can burn the holder's tokens", async function () {
  40. await this.token.connect(this.holder).setApprovalForAll(this.operator, true);
  41. await this.token.connect(this.operator).burnBatch(this.holder, ids, [values[0] - 1n, values[1] - 2n]);
  42. expect(await this.token.balanceOf(this.holder, ids[0])).to.equal(1n);
  43. expect(await this.token.balanceOf(this.holder, ids[1])).to.equal(2n);
  44. });
  45. it("unapproved accounts cannot burn the holder's tokens", async function () {
  46. await expect(this.token.connect(this.other).burnBatch(this.holder, ids, [values[0] - 1n, values[1] - 2n]))
  47. .to.be.revertedWithCustomError(this.token, 'ERC1155MissingApprovalForAll')
  48. .withArgs(this.other, this.holder);
  49. });
  50. });
  51. });