ERC721Utils.test.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { RevertType } = require('../../../helpers/enums');
  5. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  6. const tokenId = 1n;
  7. const RECEIVER_MAGIC_VALUE = '0x150b7a02';
  8. const deployReceiver = (revertType, returnValue = RECEIVER_MAGIC_VALUE) =>
  9. ethers.deployContract('$ERC721ReceiverMock', [returnValue, revertType]);
  10. const fixture = async () => {
  11. const [eoa, operator, owner] = await ethers.getSigners();
  12. const utils = await ethers.deployContract('$ERC721Utils');
  13. const receivers = {
  14. correct: await deployReceiver(RevertType.None),
  15. invalid: await deployReceiver(RevertType.None, '0xdeadbeef'),
  16. message: await deployReceiver(RevertType.RevertWithMessage),
  17. empty: await deployReceiver(RevertType.RevertWithoutMessage),
  18. customError: await deployReceiver(RevertType.RevertWithCustomError),
  19. panic: await deployReceiver(RevertType.Panic),
  20. nonReceiver: await ethers.deployContract('CallReceiverMock'),
  21. eoa,
  22. };
  23. return { operator, owner, utils, receivers };
  24. };
  25. describe('ERC721Utils', function () {
  26. beforeEach(async function () {
  27. Object.assign(this, await loadFixture(fixture));
  28. });
  29. describe('onERC721Received', function () {
  30. it('succeeds when called by an EOA', async function () {
  31. await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.eoa, tokenId, '0x')).to
  32. .not.be.reverted;
  33. });
  34. it('succeeds when data is passed', async function () {
  35. const data = '0x12345678';
  36. await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.correct, tokenId, data))
  37. .to.not.be.reverted;
  38. });
  39. it('succeeds when data is empty', async function () {
  40. await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.correct, tokenId, '0x'))
  41. .to.not.be.reverted;
  42. });
  43. it('reverts when receiver returns invalid value', async function () {
  44. await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.invalid, tokenId, '0x'))
  45. .to.be.revertedWithCustomError(this.utils, 'ERC721InvalidReceiver')
  46. .withArgs(this.receivers.invalid);
  47. });
  48. it('reverts when receiver reverts with message', async function () {
  49. await expect(
  50. this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.message, tokenId, '0x'),
  51. ).to.be.revertedWith('ERC721ReceiverMock: reverting');
  52. });
  53. it('reverts when receiver reverts without message', async function () {
  54. await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.empty, tokenId, '0x'))
  55. .to.be.revertedWithCustomError(this.utils, 'ERC721InvalidReceiver')
  56. .withArgs(this.receivers.empty);
  57. });
  58. it('reverts when receiver reverts with custom error', async function () {
  59. await expect(
  60. this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.customError, tokenId, '0x'),
  61. )
  62. .to.be.revertedWithCustomError(this.receivers.customError, 'CustomError')
  63. .withArgs(RECEIVER_MAGIC_VALUE);
  64. });
  65. it('reverts when receiver panics', async function () {
  66. await expect(
  67. this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.panic, tokenId, '0x'),
  68. ).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
  69. });
  70. it('reverts when receiver does not implement onERC721Received', async function () {
  71. await expect(
  72. this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.nonReceiver, tokenId, '0x'),
  73. )
  74. .to.be.revertedWithCustomError(this.utils, 'ERC721InvalidReceiver')
  75. .withArgs(this.receivers.nonReceiver);
  76. });
  77. });
  78. });