Skip to main content
ERC-165 standard interface detection for smart contracts.
Implements ERC-165 interface detection mechanism.

Overview

ERC-165 enables contracts to declare which interfaces they support. Interface IDs are computed by XORing function selectors, creating a unique 4-byte identifier for each interface.

getInterfaceId

getInterfaceId(selectors: SelectorLike[]): InterfaceIdType

Calculates ERC-165 interface ID by XORing function selectors. Parameters:
  • selectors: SelectorLike[] - Array of function selectors (4 bytes each)
Returns: InterfaceIdType - 4-byte interface ID Example:
import { getInterfaceId } from '@tevm/voltaire/Abi/interface';

// Calculate ERC-20 interface ID
const erc20InterfaceId = getInterfaceId([
  '0x70a08231', // balanceOf(address)
  '0x095ea7b3', // approve(address,uint256)
  '0xa9059cbb', // transfer(address,uint256)
  '0xdd62ed3e', // allowance(address,address)
  '0x23b872dd', // transferFrom(address,address,uint256)
  '0x18160ddd', // totalSupply()
]);

console.log('0x' + Buffer.from(erc20InterfaceId).toString('hex'));
// '0x36372b07'

Standard Interface IDs

ERC165_INTERFACE_ID

const ERC165_INTERFACE_ID = '0x01ffc9a7'
Interface ID for supportsInterface(bytes4) function itself.

ERC20_INTERFACE_ID

const ERC20_INTERFACE_ID = '0x36372b07'
Interface ID for ERC-20 token standard. Functions:
  • balanceOf(address)
  • transfer(address,uint256)
  • approve(address,uint256)
  • transferFrom(address,address,uint256)
  • allowance(address,address)
  • totalSupply()

ERC721_INTERFACE_ID

const ERC721_INTERFACE_ID = '0x80ac58cd'
Interface ID for ERC-721 NFT standard.

ERC1155_INTERFACE_ID

const ERC1155_INTERFACE_ID = '0xd9b67a26'
Interface ID for ERC-1155 multi-token standard.

Usage Patterns

Check Interface Support

import { ERC20_INTERFACE_ID, ERC721_INTERFACE_ID } from '@tevm/voltaire/Abi/interface';

async function detectTokenStandard(contractAddress: string) {
  // Call supportsInterface(bytes4)
  const isErc20 = await contract.supportsInterface(ERC20_INTERFACE_ID);
  const isErc721 = await contract.supportsInterface(ERC721_INTERFACE_ID);

  if (isErc721) return 'ERC-721';
  if (isErc20) return 'ERC-20';
  return 'Unknown';
}

Custom Interface

import { getInterfaceId } from '@tevm/voltaire/Abi/interface';
import { Selector } from '@tevm/voltaire/Selector';

// Define custom interface
const selectors = [
  Selector.fromSignature('myFunction(uint256)'),
  Selector.fromSignature('anotherFunction(address)')
];

const interfaceId = getInterfaceId(selectors);

// Implement in Solidity
// function supportsInterface(bytes4 interfaceId) public pure returns (bool) {
//   return interfaceId == 0x... || super.supportsInterface(interfaceId);
// }

Specification

Defined in: src/primitives/Abi/interface/ See also: