Last active
September 27, 2023 05:36
-
-
Save hmmdeif/d6bcafc4bdbab0f44c41da28f8515b57 to your computer and use it in GitHub Desktop.
Hardhat calculate bytes4 interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import contract factory generated from `hardhat compile` with typechain | |
import { IERC165__factory, IERC2981__factory } from '@custom-types/contracts' | |
import { ethers } from 'hardhat' | |
import { utils } from 'ethers' | |
const getInterfaceID = (contractInterface: utils.Interface) => { | |
let interfaceID = ethers.constants.Zero; | |
const functions: string[] = Object.keys(contractInterface.functions); | |
for (let i = 0; i < functions.length; i++) { | |
interfaceID = interfaceID.xor(contractInterface.getSighash(functions[i])); | |
} | |
return interfaceID; | |
} | |
const ERC2981bytes = getInterfaceID(IERC165__factory.createInterface()) | |
.xor(getInterfaceID(IERC2981__factory.createInterface())) | |
.toHexString() | |
// Use in tests (remember solidity overrides go from right to left) | |
describe('Supports interface', () => { | |
it('does not support random interface', async () => { | |
await expect(cats.supportsInterface('0x0')).to.be.reverted | |
}) | |
it('does support ERC165', async () => { | |
const supports = await cats.supportsInterface('0x01ffc9a7') | |
expect(supports).to.be.true | |
}) | |
it('does support ERC2981', async () => { | |
const supports = await cats.supportsInterface(getERC2981InterfaceId()) | |
expect(supports).to.be.true | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment