ERC1155Holder.test.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
  5. const ids = [1n, 2n, 3n];
  6. const values = [1000n, 2000n, 3000n];
  7. const data = '0x12345678';
  8. async function fixture() {
  9. const [owner] = await ethers.getSigners();
  10. const token = await ethers.deployContract('$ERC1155', ['https://token-cdn-domain/{id}.json']);
  11. const mock = await ethers.deployContract('$ERC1155Holder');
  12. await token.$_mintBatch(owner, ids, values, '0x');
  13. return { owner, token, mock };
  14. }
  15. describe('ERC1155Holder', function () {
  16. beforeEach(async function () {
  17. Object.assign(this, await loadFixture(fixture));
  18. });
  19. shouldSupportInterfaces(['ERC1155Receiver']);
  20. it('receives ERC1155 tokens from a single ID', async function () {
  21. await this.token.connect(this.owner).safeTransferFrom(this.owner, this.mock, ids[0], values[0], data);
  22. expect(await this.token.balanceOf(this.mock, ids[0])).to.equal(values[0]);
  23. for (let i = 1; i < ids.length; i++) {
  24. expect(await this.token.balanceOf(this.mock, ids[i])).to.equal(0n);
  25. }
  26. });
  27. it('receives ERC1155 tokens from a multiple IDs', async function () {
  28. expect(
  29. await this.token.balanceOfBatch(
  30. ids.map(() => this.mock),
  31. ids,
  32. ),
  33. ).to.deep.equal(ids.map(() => 0n));
  34. await this.token.connect(this.owner).safeBatchTransferFrom(this.owner, this.mock, ids, values, data);
  35. expect(
  36. await this.token.balanceOfBatch(
  37. ids.map(() => this.mock),
  38. ids,
  39. ),
  40. ).to.deep.equal(values);
  41. });
  42. });