123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- const { ethers } = require('hardhat');
- const { expect } = require('chai');
- const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
- const { RevertType } = require('../../../helpers/enums');
- const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
- const firstTokenId = 1n;
- const secondTokenId = 2n;
- const firstTokenValue = 1000n;
- const secondTokenValue = 1000n;
- const RECEIVER_SINGLE_MAGIC_VALUE = '0xf23a6e61';
- const RECEIVER_BATCH_MAGIC_VALUE = '0xbc197c81';
- const deployReceiver = (
- revertType,
- returnValueSingle = RECEIVER_SINGLE_MAGIC_VALUE,
- returnValueBatched = RECEIVER_BATCH_MAGIC_VALUE,
- ) => ethers.deployContract('$ERC1155ReceiverMock', [returnValueSingle, returnValueBatched, revertType]);
- const fixture = async () => {
- const [eoa, operator, owner] = await ethers.getSigners();
- const utils = await ethers.deployContract('$ERC1155Utils');
- const receivers = {
- correct: await deployReceiver(RevertType.None),
- invalid: await deployReceiver(RevertType.None, '0xdeadbeef', '0xdeadbeef'),
- message: await deployReceiver(RevertType.RevertWithMessage),
- empty: await deployReceiver(RevertType.RevertWithoutMessage),
- customError: await deployReceiver(RevertType.RevertWithCustomError),
- panic: await deployReceiver(RevertType.Panic),
- nonReceiver: await ethers.deployContract('CallReceiverMock'),
- eoa,
- };
- return { operator, owner, utils, receivers };
- };
- describe('ERC1155Utils', function () {
- beforeEach(async function () {
- Object.assign(this, await loadFixture(fixture));
- });
- describe('onERC1155Received', function () {
- it('succeeds when called by an EOA', async function () {
- await expect(
- this.utils.$checkOnERC1155Received(
- this.operator,
- this.owner,
- this.receivers.eoa,
- firstTokenId,
- firstTokenValue,
- '0x',
- ),
- ).to.not.be.reverted;
- });
- it('succeeds when data is passed', async function () {
- const data = '0x12345678';
- await expect(
- this.utils.$checkOnERC1155Received(
- this.operator,
- this.owner,
- this.receivers.correct,
- firstTokenId,
- firstTokenValue,
- data,
- ),
- ).to.not.be.reverted;
- });
- it('succeeds when data is empty', async function () {
- await expect(
- this.utils.$checkOnERC1155Received(
- this.operator,
- this.owner,
- this.receivers.correct,
- firstTokenId,
- firstTokenValue,
- '0x',
- ),
- ).to.not.be.reverted;
- });
- it('reverts when receiver returns invalid value', async function () {
- await expect(
- this.utils.$checkOnERC1155Received(
- this.operator,
- this.owner,
- this.receivers.invalid,
- firstTokenId,
- firstTokenValue,
- '0x',
- ),
- )
- .to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
- .withArgs(this.receivers.invalid);
- });
- it('reverts when receiver reverts with message', async function () {
- await expect(
- this.utils.$checkOnERC1155Received(
- this.operator,
- this.owner,
- this.receivers.message,
- firstTokenId,
- firstTokenValue,
- '0x',
- ),
- ).to.be.revertedWith('ERC1155ReceiverMock: reverting on receive');
- });
- it('reverts when receiver reverts without message', async function () {
- await expect(
- this.utils.$checkOnERC1155Received(
- this.operator,
- this.owner,
- this.receivers.empty,
- firstTokenId,
- firstTokenValue,
- '0x',
- ),
- )
- .to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
- .withArgs(this.receivers.empty);
- });
- it('reverts when receiver reverts with custom error', async function () {
- await expect(
- this.utils.$checkOnERC1155Received(
- this.operator,
- this.owner,
- this.receivers.customError,
- firstTokenId,
- firstTokenValue,
- '0x',
- ),
- )
- .to.be.revertedWithCustomError(this.receivers.customError, 'CustomError')
- .withArgs(RECEIVER_SINGLE_MAGIC_VALUE);
- });
- it('reverts when receiver panics', async function () {
- await expect(
- this.utils.$checkOnERC1155Received(
- this.operator,
- this.owner,
- this.receivers.panic,
- firstTokenId,
- firstTokenValue,
- '0x',
- ),
- ).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
- });
- it('reverts when receiver does not implement onERC1155Received', async function () {
- await expect(
- this.utils.$checkOnERC1155Received(
- this.operator,
- this.owner,
- this.receivers.nonReceiver,
- firstTokenId,
- firstTokenValue,
- '0x',
- ),
- )
- .to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
- .withArgs(this.receivers.nonReceiver);
- });
- });
- describe('onERC1155BatchReceived', function () {
- it('succeeds when called by an EOA', async function () {
- await expect(
- this.utils.$checkOnERC1155BatchReceived(
- this.operator,
- this.owner,
- this.receivers.eoa,
- [firstTokenId, secondTokenId],
- [firstTokenValue, secondTokenValue],
- '0x',
- ),
- ).to.not.be.reverted;
- });
- it('succeeds when data is passed', async function () {
- const data = '0x12345678';
- await expect(
- this.utils.$checkOnERC1155BatchReceived(
- this.operator,
- this.owner,
- this.receivers.correct,
- [firstTokenId, secondTokenId],
- [firstTokenValue, secondTokenValue],
- data,
- ),
- ).to.not.be.reverted;
- });
- it('succeeds when data is empty', async function () {
- await expect(
- this.utils.$checkOnERC1155BatchReceived(
- this.operator,
- this.owner,
- this.receivers.correct,
- [firstTokenId, secondTokenId],
- [firstTokenValue, secondTokenValue],
- '0x',
- ),
- ).to.not.be.reverted;
- });
- it('reverts when receiver returns invalid value', async function () {
- await expect(
- this.utils.$checkOnERC1155BatchReceived(
- this.operator,
- this.owner,
- this.receivers.invalid,
- [firstTokenId, secondTokenId],
- [firstTokenValue, secondTokenValue],
- '0x',
- ),
- )
- .to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
- .withArgs(this.receivers.invalid);
- });
- it('reverts when receiver reverts with message', async function () {
- await expect(
- this.utils.$checkOnERC1155BatchReceived(
- this.operator,
- this.owner,
- this.receivers.message,
- [firstTokenId, secondTokenId],
- [firstTokenValue, secondTokenValue],
- '0x',
- ),
- ).to.be.revertedWith('ERC1155ReceiverMock: reverting on batch receive');
- });
- it('reverts when receiver reverts without message', async function () {
- await expect(
- this.utils.$checkOnERC1155BatchReceived(
- this.operator,
- this.owner,
- this.receivers.empty,
- [firstTokenId, secondTokenId],
- [firstTokenValue, secondTokenValue],
- '0x',
- ),
- )
- .to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
- .withArgs(this.receivers.empty);
- });
- it('reverts when receiver reverts with custom error', async function () {
- await expect(
- this.utils.$checkOnERC1155BatchReceived(
- this.operator,
- this.owner,
- this.receivers.customError,
- [firstTokenId, secondTokenId],
- [firstTokenValue, secondTokenValue],
- '0x',
- ),
- )
- .to.be.revertedWithCustomError(this.receivers.customError, 'CustomError')
- .withArgs(RECEIVER_SINGLE_MAGIC_VALUE);
- });
- it('reverts when receiver panics', async function () {
- await expect(
- this.utils.$checkOnERC1155BatchReceived(
- this.operator,
- this.owner,
- this.receivers.panic,
- [firstTokenId, secondTokenId],
- [firstTokenValue, secondTokenValue],
- '0x',
- ),
- ).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
- });
- it('reverts when receiver does not implement onERC1155BatchReceived', async function () {
- await expect(
- this.utils.$checkOnERC1155BatchReceived(
- this.operator,
- this.owner,
- this.receivers.nonReceiver,
- [firstTokenId, secondTokenId],
- [firstTokenValue, secondTokenValue],
- '0x',
- ),
- )
- .to.be.revertedWithCustomError(this.utils, 'ERC1155InvalidReceiver')
- .withArgs(this.receivers.nonReceiver);
- });
- });
- });
|