Skip to content

getAbiItem

Retrieves an item from the ABI.

Import

import { getAbiItem } from 'viem'

Usage

import { getAbiItem } from 'viem'
 
const encodedData = getAbiItem({
  abi: [
    { 
      name: 'x', 
      type: 'function', 
      inputs: [{ type: 'uint256' }], 
      outputs: [],
      stateMutability: 'payable'
    },
    { 
      name: 'y', 
      type: 'event', 
      inputs: [{ type: 'address' }], 
      outputs: [{ type: 'uint256' }],
      stateMutability: 'view'
    },
    { 
      name: 'z', 
      type: 'function', 
      inputs: [{ type: 'string' }],
      outputs: [{ type: 'uint256' }],
      stateMutability: 'view'
    }
  ],
  name: 'y',
})
/**
 * { 
 *  name: 'y', 
 *  type: 'event', 
 *  inputs: [{ type: 'address' }], 
 *  outputs: [{ type: 'uint256' }],
 *  stateMutability: 'view'
 * }
 */

Returns

AbiItem

The ABI item.

Parameters

abi

The contract's ABI.

const encodedData = getAbiItem({
  abi: [...], 
  name: 'x',
})

name

  • Type: string

Name of the ABI item to extract.

const encodedData = getAbiItem({
  abi: [...],
  name: 'x', 
})

You can also provide the ABI item's 4byte selector:

const encodedData = getAbiItem({
  abi: [...],
  name: '0x70a08231', 
})

args (optional)

  • Type: Inferred.

Optional arguments to identify function overrides.

const encodedData = getAbiItem({
  abi: [...],
  name: 'y',
  args: ['0x0000000000000000000000000000000000000000'], 
})