Skip to content

toFunctionSelector

Returns the function selector (4 byte encoding) for a given function definition.

Install

import { toFunctionSelector } from 'viem'

Usage

import { toFunctionSelector } from 'viem'
 
const selector_1 = toFunctionSelector('function ownerOf(uint256 tokenId)')
Output: 0x6352211e
const selector_2 = toFunctionSelector('ownerOf(uint256)')
Output: 0x6352211e
// or from an `AbiFunction` on your contract ABI const selector_3 = toFunctionSelector({ name: 'ownerOf', type: 'function', inputs: [{ name: 'tokenId', type: 'uint256' }], outputs: [], stateMutability: 'view', })
Output: 0x6352211e

Returns

Hex

The selector as a hex value.

Parameters

function

The function to generate a selector for.