Skip to content

Commit

Permalink
Closes #161: Add EIP-165 support in DefaultCallbackHandler (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmeissner committed Mar 24, 2021
1 parent a34d45e commit b4ac134
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
16 changes: 15 additions & 1 deletion contracts/handler/DefaultCallbackHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ pragma solidity >=0.7.0 <0.9.0;
import "../interfaces/ERC1155TokenReceiver.sol";
import "../interfaces/ERC721TokenReceiver.sol";
import "../interfaces/ERC777TokensRecipient.sol";
import "../interfaces/IERC165.sol";

/// @title Default Callback Handler - returns true for known token callbacks
/// @author Richard Meissner - <richard@gnosis.pm>
contract DefaultCallbackHandler is ERC1155TokenReceiver, ERC777TokensRecipient, ERC721TokenReceiver {
contract DefaultCallbackHandler is ERC1155TokenReceiver, ERC777TokensRecipient, ERC721TokenReceiver, IERC165 {

string public constant NAME = "Default Callback Handler";
string public constant VERSION = "1.0.0";
Expand Down Expand Up @@ -48,4 +49,17 @@ contract DefaultCallbackHandler is ERC1155TokenReceiver, ERC777TokensRecipient,
// We implement this for completeness, doesn't really have any value
}


function supportsInterface(bytes4 interfaceId)
virtual
override
external
view
returns (bool)
{
return interfaceId == type(ERC1155TokenReceiver).interfaceId
|| interfaceId == type(ERC721TokenReceiver).interfaceId
|| interfaceId == type(IERC165).interfaceId;
}

}
15 changes: 15 additions & 0 deletions contracts/interfaces/IERC165.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.7.0 <0.9.0;

/// @notice More details at https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/introspection/IERC165.sol
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
23 changes: 23 additions & 0 deletions test/handlers/DefaultCallbackHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ describe("DefaultCallbackHandler", async () => {
});

describe("ERC1155", async () => {
it('should supports ERC1155 interface', async () => {
const handler = await getDefaultCallbackHandler()
await expect(
await handler.callStatic.supportsInterface("0x4e2312e0")
).to.be.eq(true)
})

it('to handle onERC1155Received', async () => {
const handler = await getDefaultCallbackHandler()
await expect(
Expand All @@ -26,6 +33,13 @@ describe("DefaultCallbackHandler", async () => {
})

describe("ERC721", async () => {
it('should supports ERC721 interface', async () => {
const handler = await getDefaultCallbackHandler()
await expect(
await handler.callStatic.supportsInterface("0x150b7a02")
).to.be.eq(true)
})

it('to handle onERC721Received', async () => {
const handler = await getDefaultCallbackHandler()
await expect(
Expand All @@ -40,4 +54,13 @@ describe("DefaultCallbackHandler", async () => {
await handler.callStatic.tokensReceived(AddressZero, AddressZero, AddressZero, 0, "0x", "0x")
})
})

describe("ERC165", async () => {
it('should not support random interface', async () => {
const handler = await getDefaultCallbackHandler()
await expect(
await handler.callStatic.supportsInterface("0xbaddad42")
).to.be.eq(false)
})
})
})

0 comments on commit b4ac134

Please sign in to comment.