Skip to content
On this page

getFunctionSelector โ€‹

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

Install โ€‹

ts
import { getFunctionSelector } from 'viem'

Usage โ€‹

ts
import { getFunctionSelector } from 'viem'

const selector = getFunctionSelector('function ownerOf(uint256 tokenId)')
// 0x6352211e

const selector = getFunctionSelector('ownerOf(uint256)')
// 0x6352211e

// or from an `AbiFunction` on your contract ABI
const selector = getFunctionSelector({
  name: 'ownerOf',
  type: 'function',
  inputs: [{ name: 'tokenId', type: 'uint256' }],
  outputs: [],
  stateMutability: 'view',
})
// 0x6352211e

Returns โ€‹

Hex

The selector as a hex value.

Parameters โ€‹

function โ€‹

The function to generate a selector for.

Released under the MIT License.