Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #161: Add EIP-165 support in DefaultCallbackHandler #285

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 - <[email protected]>
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know interfaceId was introduced in Solidity some months ago, nice

|| interfaceId == type(IERC165).interfaceId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess this is the same that interfaceId == this.supportsInterface.selector

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so yes ... I just went with this as it was more in line we the statements above.

}

}
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)
})
})
})