Skip to content

Utilities

Utility types for working with ABIs and EIP-712 Typed Data.

AbiParameterToPrimitiveType

Converts AbiParameter to corresponding TypeScript primitive type.

NameDescriptionType
TAbiParameterParameter to convert to TypeScript representation.AbiParameter
TAbiParameterKindKind to narrow by parameter type.AbiParameterKind (optional)
returnsTypeScript primitive type.TType (inferred)

Example

import { AbiParameterToPrimitiveType } from 'abitype'
 
type Result = AbiParameterToPrimitiveType<{
  // ^?
 
  
  name: 'owner'
  type: 'address'
}>
## Errors were thrown in the sample, but not included in an errors tag These errors were not marked as being expected: 2307. Expected: // @errors: 2307 Compiler Errors: index.ts [2307] 44 - Cannot find module 'abitype' or its corresponding type declarations.

AbiParametersToPrimitiveTypes

Converts array of AbiParameter to corresponding TypeScript primitive types.

NameDescriptionType
TAbiParametersParameters to convert to TypeScript representations.readonly AbiParameter[]
TAbiParameterKindKind to narrow by parameter type.AbiParameterKind (optional)
returnsTypeScript primitive types.TType[] (inferred)

Example

import { AbiParametersToPrimitiveTypes } from 'abitype'
 
type Result = AbiParametersToPrimitiveTypes<
  // ^?
 
  [
    { name: 'to'; type: 'address'; },
    { name: 'tokenId'; type: 'uint256'; },
  ]
>
## Errors were thrown in the sample, but not included in an errors tag These errors were not marked as being expected: 2307. Expected: // @errors: 2307 Compiler Errors: index.ts [2307] 46 - Cannot find module 'abitype' or its corresponding type declarations.

AbiTypeToPrimitiveType

Converts AbiType to corresponding TypeScript primitive type.

NameDescriptionType
TAbiTypeABI type to convert to TypeScript representation.AbiType
TAbiParameterKindKind to narrow by parameter type.AbiParameterKind (optional)
returnsTypeScript primitive type.TType (inferred)

Example

import { AbiTypeToPrimitiveType } from 'abitype'
 
type Result = AbiTypeToPrimitiveType<'address'>
//   ^?
 

ExtractAbiError

Extracts AbiError with name from Abi.

NameDescriptionType
TAbiABI.Abi
TErrorNameName of error.string (inferred)
returnsABI Error.AbiError

Example

import { ExtractAbiError } from 'abitype'
 
const abi = [
  { name: 'BarError', type: 'error', inputs: [] },
  { name: 'FooError', type: 'error', inputs: [] },
] as const
 
type Result = ExtractAbiError<typeof abi, 'FooError'>
  //   ^?
 
 
 
 
 

ExtractAbiErrorNames

Extracts all AbiError names from Abi.

NameDescriptionType
TAbiABI.Abi
returnsABI Error names.string (inferred)

Example

import { ExtractAbiErrorNames } from 'abitype'
 
const abi = [
  { name: 'FooError', type: 'error', inputs: [] },
  { name: 'BarError', type: 'error', inputs: [] },
] as const
 
type Result = ExtractAbiErrorNames<typeof abi>
//   ^?
 

ExtractAbiErrors

Extracts all AbiError types from Abi.

NameDescriptionType
TAbiABI.Abi
returnsABI Errors.AbiError (union)

Example

import { ExtractAbiErrors } from 'abitype'
 
const abi = [
  { name: 'FooError', type: 'error', inputs: [] },
  { name: 'BarError', type: 'error', inputs: [] },
] as const
 
type Result = ExtractAbiErrors<typeof abi>
//   ^?
 
 
 
 
 
 
 
 
 

ExtractAbiEvent

Extracts AbiEvent with name from Abi.

NameDescriptionType
TAbiABI.Abi
TEventNameName of event.string (inferred)
returnsABI Event.AbiEvent

Example

import { ExtractAbiEvent } from 'abitype'
 
const abi = [
  {
    name: 'Approval',
    type: 'event',
    anonymous: false,
    inputs: [
      { name: 'owner', type: 'address', indexed: true },
      { name: 'approved', type: 'address', indexed: true },
      { name: 'tokenId', type: 'uint256', indexed: true },
    ],
  },
  {
    name: 'Transfer',
    type: 'event',
    anonymous: false,
    inputs: [
      { name: 'from', type: 'address', indexed: true },
      { name: 'to', type: 'address', indexed: true },
      { name: 'tokenId', type: 'uint256', indexed: true },
    ],
  },
] as const
 
type Result = ExtractAbiEvent<typeof abi, 'Transfer'>
//   ^?
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ExtractAbiEventNames

Extracts all AbiEvent names from Abi.

NameDescriptionType
TAbiABI.Abi
returnsABI Error names.string (inferred)

Example

import { ExtractAbiEventNames } from 'abitype'
 
const abi = [
  {
    name: 'Approval',
    type: 'event',
    anonymous: false,
    inputs: [
      { name: 'owner', type: 'address', indexed: true },
      { name: 'approved', type: 'address', indexed: true },
      { name: 'tokenId', type: 'uint256', indexed: true },
    ],
  },
  {
    name: 'Transfer',
    type: 'event',
    anonymous: false,
    inputs: [
      { name: 'from', type: 'address', indexed: true },
      { name: 'to', type: 'address', indexed: true },
      { name: 'tokenId', type: 'uint256', indexed: true },
    ],
  },
] as const
 
type Result = ExtractAbiEventNames<typeof abi>
//   ^?
 

ExtractAbiEvents

Extracts all AbiEvent types from Abi.

NameDescriptionType
TAbiABI.Abi
returnsABI Events.AbiEvent (union)

Example

import { ExtractAbiEvents } from 'abitype'
 
const abi = [
  {
    name: 'Approval',
    type: 'event',
    anonymous: false,
    inputs: [
      { name: 'owner', type: 'address', indexed: true },
      { name: 'approved', type: 'address', indexed: true },
      { name: 'tokenId', type: 'uint256', indexed: true },
    ],
  },
  {
    name: 'Transfer',
    type: 'event',
    anonymous: false,
    inputs: [
      { name: 'from', type: 'address', indexed: true },
      { name: 'to', type: 'address', indexed: true },
      { name: 'tokenId', type: 'uint256', indexed: true },
    ],
  },
] as const
 
type Result = ExtractAbiEvents<typeof abi>
//   ^?
 
 
 
 
 
 
 
 
 
 
 
 
 

ExtractAbiFunction

Extracts AbiFunction with name from Abi.

NameDescriptionType
TAbiABI.Abi
TFunctionNameName of function.string (inferred)
TAbiStateMutabilityABI state mutability.AbiStateMutability (optional)
returnsABI Function.AbiFunction

Example

import { ExtractAbiFunction } from 'abitype'
 
const abi = [
  {
    name: 'balanceOf',
    type: 'function',
    stateMutability: 'view',
    inputs: [{ name: 'owner', type: 'address' }],
    outputs: [{ name: 'balance', type: 'uint256' }],
  },
  {
    name: 'safeTransferFrom',
    type: 'function',
    stateMutability: 'nonpayable',
    inputs: [
      { name: 'from', type: 'address' },
      { name: 'to', type: 'address' },
      { name: 'tokenId', type: 'uint256' },
    ],
    outputs: [],
  },
] as const
 
type Result = ExtractAbiFunction<typeof abi, 'balanceOf'>
//   ^?
 
 
 
 
 
 
 
 
 
 
 
 

ExtractAbiFunctionNames

Extracts all AbiFunction names from Abi.

NameDescriptionType
TAbiABI.Abi
TAbiStateMutabilityABI state mutability.AbiStateMutability (optional)
returnsABI Event names.string (inferred)

Example

import { ExtractAbiFunctionNames } from 'abitype'
 
const abi = [
  {
    name: 'balanceOf',
    type: 'function',
    stateMutability: 'view',
    inputs: [{ name: 'owner', type: 'address' }],
    outputs: [{ name: 'balance', type: 'uint256' }],
  },
  {
    name: 'safeTransferFrom',
    type: 'function',
    stateMutability: 'nonpayable',
    inputs: [
      { name: 'from', type: 'address' },
      { name: 'to', type: 'address' },
      { name: 'tokenId', type: 'uint256' },
    ],
    outputs: [],
  },
] as const
 
type Result = ExtractAbiFunctionNames<typeof abi>
//   ^?
 

ExtractAbiFunctions

Extracts all AbiFunction types from Abi.

NameDescriptionType
TAbiABI.Abi
returnsABI Functions.AbiFunction (union)

Example

import { ExtractAbiFunctions } from 'abitype'
 
const abi = [
  {
    name: 'balanceOf',
    type: 'function',
    stateMutability: 'view',
    inputs: [{ name: 'owner', type: 'address' }],
    outputs: [{ name: 'balance', type: 'uint256' }],
  },
  {
    name: 'safeTransferFrom',
    type: 'function',
    stateMutability: 'nonpayable',
    inputs: [
      { name: 'from', type: 'address' },
      { name: 'to', type: 'address' },
      { name: 'tokenId', type: 'uint256' },
    ],
    outputs: [],
  },
] as const
 
type Result = ExtractAbiFunctions<typeof abi>
//   ^?
 
 
 
 
 
 
 
 
 
 
 
 
 
 

By default, extracts all functions, but you can also filter by AbiStateMutability:

type Result = ExtractAbiFunctions<typeof erc721Abi, 'view'>

IsAbi

Checks if type is Abi.

NameDescriptionType
TAbiABI.Abi
returnsBoolean value. true if valid Abi, false if not.boolean

Example

import { IsAbi } from 'abitype'
 
const abi = [
  {
    name: 'balanceOf',
    type: 'function',
    stateMutability: 'view',
    inputs: [{ name: 'owner', type: 'address' }],
    outputs: [{ name: 'balance', type: 'uint256' }],
  },
  {
    name: 'safeTransferFrom',
    type: 'function',
    stateMutability: 'nonpayable',
    inputs: [
      { name: 'from', type: 'address' },
      { name: 'to', type: 'address' },
      { name: 'tokenId', type: 'uint256' },
    ],
    outputs: [],
  },
] as const
 
type Result = IsAbi<typeof abi>
//   ^?
 

IsTypedData

Checks if type is TypedData.

NameDescriptionType
TTypedDataEIP-712 Typed Data schema.TypedData
returnsBoolean value. true if valid TypedData, false if not.boolean

Example

import { IsTypedData } from 'abitype'
 
const types = {
  Person: [
    { name: 'name', type: 'string' },
    { name: 'wallet', type: 'address' },
  ],
  Mail: [
    { name: 'from', type: 'Person' },
    { name: 'to', type: 'Person' },
    { name: 'contents', type: 'string' },
  ],
} as const
 
type Result = IsTypedData<typeof types>
//   ^?
 

TypedDataToPrimitiveTypes

Converts EIP-712 TypedData to corresponding TypeScript primitive type.

NameDescriptionType
TTypedDataEIP-712 Typed Data schema.TypedData
returnsTypeScript representation of schema.{ [name: string]: TType } (inferred)

Example

import { TypedDataToPrimitiveTypes } from 'abitype'
 
const types = {
  Person: [
    { name: 'name', type: 'string' },
    { name: 'wallet', type: 'address' },
  ],
  Mail: [
    { name: 'from', type: 'Person' },
    { name: 'to', type: 'Person' },
    { name: 'contents', type: 'string' },
  ],
} as const
 
type Result = TypedDataToPrimitiveTypes<typeof types>
//   ^?