From d6644b316cbef843dc5939d925e2724913be21b6 Mon Sep 17 00:00:00 2001 From: Kyryl R Date: Sat, 6 Jan 2024 18:41:43 +0200 Subject: [PATCH] Added support for passing contract names to the getInterfaceID function as well. --- CHANGELOG.md | 1 + src/index.ts | 6 +++++- .../contracts/ERC165Contract.sol | 8 ++++++++ test/index.test.ts | 2 ++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a2c84f..f02511f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,3 +4,4 @@ * Finished the base version of the `getInterfaceID` function. * Used `Hardhat` and `solidity-ast` packages to parse the contract and get the interface ID. +* Added support for passing contract names to the `getInterfaceID` function. diff --git a/src/index.ts b/src/index.ts index bb334a9..4a5fe19 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,11 @@ export async function getInterfaceID(contractName: string) { let interfaceID = 0n; for (const functionDef of allFunctions) { - interfaceID = interfaceID ^ BigInt(`0x${functionDef.functionSelector!}`); + if (!functionDef.functionSelector) { + continue; + } + + interfaceID = interfaceID ^ BigInt(`0x${functionDef.functionSelector}`); } return "0x" + interfaceID.toString(16).padStart(8, "0"); diff --git a/test/fixture-projects/hardhat-project-typechain-ethers/contracts/ERC165Contract.sol b/test/fixture-projects/hardhat-project-typechain-ethers/contracts/ERC165Contract.sol index d2e4d5e..6989ef4 100644 --- a/test/fixture-projects/hardhat-project-typechain-ethers/contracts/ERC165Contract.sol +++ b/test/fixture-projects/hardhat-project-typechain-ethers/contracts/ERC165Contract.sol @@ -20,4 +20,12 @@ contract ERC165Contract is ERC165 { interfaceId == type(IERC721Enumerable).interfaceId || super.supportsInterface(interfaceId); } + + function _version() internal pure returns (string memory) { + return "1.0.0"; + } + + function _author() private pure returns (string memory) { + return "0x0"; + } } diff --git a/test/index.test.ts b/test/index.test.ts index 2500fe9..88d557a 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -31,6 +31,8 @@ describe("Interface Id Calculation", function () { await expect(erc165Contract.supportsInterface(await getInterfaceID("IERC721Enumerable"))).to.be.eventually.true; await expect(erc165Contract.supportsInterface(await getInterfaceID("IERC4626"))).to.be.eventually.true; + expect(await getInterfaceID("IERC165")).to.be.equal(await getInterfaceID("ERC165Contract")); + await expect(erc165Contract.supportsInterface("0xffffffff")).to.be.eventually.false; }); });