Skip to content

Commit

Permalink
Added support for passing contract names to the getInterfaceID functi…
Browse files Browse the repository at this point in the history
…on as well.
  • Loading branch information
KyrylR committed Jan 6, 2024
1 parent 2243eda commit d6644b3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
2 changes: 2 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

0 comments on commit d6644b3

Please sign in to comment.