clawchain/viem/site/pages/docs/utilities/toFunctionSelector.md

1017 B

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

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)')
// @log: Output: 0x6352211e

const selector_2 = toFunctionSelector('ownerOf(uint256)')
// @log: 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',
})
// @log: Output: 0x6352211e

Returns

Hex

The selector as a hex value.

Parameters

function

The function to generate a selector for.