Skip to content

Commit b4ac134

Browse files
authored
Closes #161: Add EIP-165 support in DefaultCallbackHandler (#285)
1 parent a34d45e commit b4ac134

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

contracts/handler/DefaultCallbackHandler.sol

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ pragma solidity >=0.7.0 <0.9.0;
44
import "../interfaces/ERC1155TokenReceiver.sol";
55
import "../interfaces/ERC721TokenReceiver.sol";
66
import "../interfaces/ERC777TokensRecipient.sol";
7+
import "../interfaces/IERC165.sol";
78

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

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

52+
53+
function supportsInterface(bytes4 interfaceId)
54+
virtual
55+
override
56+
external
57+
view
58+
returns (bool)
59+
{
60+
return interfaceId == type(ERC1155TokenReceiver).interfaceId
61+
|| interfaceId == type(ERC721TokenReceiver).interfaceId
62+
|| interfaceId == type(IERC165).interfaceId;
63+
}
64+
5165
}

contracts/interfaces/IERC165.sol

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: LGPL-3.0-only
2+
pragma solidity >=0.7.0 <0.9.0;
3+
4+
/// @notice More details at https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/introspection/IERC165.sol
5+
interface IERC165 {
6+
/**
7+
* @dev Returns true if this contract implements the interface defined by
8+
* `interfaceId`. See the corresponding
9+
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
10+
* to learn more about how these ids are created.
11+
*
12+
* This function call must use less than 30 000 gas.
13+
*/
14+
function supportsInterface(bytes4 interfaceId) external view returns (bool);
15+
}

test/handlers/DefaultCallbackHandler.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ describe("DefaultCallbackHandler", async () => {
1010
});
1111

1212
describe("ERC1155", async () => {
13+
it('should supports ERC1155 interface', async () => {
14+
const handler = await getDefaultCallbackHandler()
15+
await expect(
16+
await handler.callStatic.supportsInterface("0x4e2312e0")
17+
).to.be.eq(true)
18+
})
19+
1320
it('to handle onERC1155Received', async () => {
1421
const handler = await getDefaultCallbackHandler()
1522
await expect(
@@ -26,6 +33,13 @@ describe("DefaultCallbackHandler", async () => {
2633
})
2734

2835
describe("ERC721", async () => {
36+
it('should supports ERC721 interface', async () => {
37+
const handler = await getDefaultCallbackHandler()
38+
await expect(
39+
await handler.callStatic.supportsInterface("0x150b7a02")
40+
).to.be.eq(true)
41+
})
42+
2943
it('to handle onERC721Received', async () => {
3044
const handler = await getDefaultCallbackHandler()
3145
await expect(
@@ -40,4 +54,13 @@ describe("DefaultCallbackHandler", async () => {
4054
await handler.callStatic.tokensReceived(AddressZero, AddressZero, AddressZero, 0, "0x", "0x")
4155
})
4256
})
57+
58+
describe("ERC165", async () => {
59+
it('should not support random interface', async () => {
60+
const handler = await getDefaultCallbackHandler()
61+
await expect(
62+
await handler.callStatic.supportsInterface("0xbaddad42")
63+
).to.be.eq(false)
64+
})
65+
})
4366
})

0 commit comments

Comments
 (0)