Skip to content
On this page

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

ts
ts
import { AbiParameterToPrimitiveType } from 'abitype'
 
type Result = AbiParameterToPrimitiveType<{
type Result = `0x${string}`
name: 'owner'
type: 'address'
}>
 
Try
ts
import { AbiParameterToPrimitiveType } from 'abitype'
 
type Result = AbiParameterToPrimitiveType<{
type Result = `0x${string}`
name: 'owner'
type: 'address'
}>
 
Try

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

ts
ts
import { AbiParametersToPrimitiveTypes } from 'abitype'
 
type Result = AbiParametersToPrimitiveTypes<
type Result = [`0x${string}`, bigint]
[
{ name: 'to'; type: 'address'; },
{ name: 'tokenId'; type: 'uint256'; },
]
>
Try
ts
import { AbiParametersToPrimitiveTypes } from 'abitype'
 
type Result = AbiParametersToPrimitiveTypes<
type Result = [`0x${string}`, bigint]
[
{ name: 'to'; type: 'address'; },
{ name: 'tokenId'; type: 'uint256'; },
]
>
Try

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)

NOTE

Does not include full array or tuple conversion. Use AbiParameterToPrimitiveType to fully convert array and tuple types.

Example

ts
ts
import { AbiTypeToPrimitiveType } from 'abitype'
 
type Result = AbiTypeToPrimitiveType<'address'>
type Result = `0x${string}`
Try
ts
import { AbiTypeToPrimitiveType } from 'abitype'
 
type Result = AbiTypeToPrimitiveType<'address'>
type Result = `0x${string}`
Try

ExtractAbiError

Extracts AbiError with name from Abi.

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

Example

ts
ts
import { ExtractAbiError } from 'abitype'
 
const abi = [
{ name: 'ApprovalCallerNotOwnerNorApproved', type: 'error', inputs: [] },
{ name: 'ApprovalQueryForNonexistentToken', type: 'error', inputs: [] },
] as const
 
type Result = ExtractAbiError<typeof abi, 'ApprovalQueryForNonexistentToken'>
type Result = { readonly name: "ApprovalQueryForNonexistentToken"; readonly type: "error"; readonly inputs: readonly []; }
Try
ts
import { ExtractAbiError } from 'abitype'
 
const abi = [
{ name: 'ApprovalCallerNotOwnerNorApproved', type: 'error', inputs: [] },
{ name: 'ApprovalQueryForNonexistentToken', type: 'error', inputs: [] },
] as const
 
type Result = ExtractAbiError<typeof abi, 'ApprovalQueryForNonexistentToken'>
type Result = { readonly name: "ApprovalQueryForNonexistentToken"; readonly type: "error"; readonly inputs: readonly []; }
Try

ExtractAbiErrorNames

Extracts all AbiError names from Abi.

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

Example

ts
ts
import { ExtractAbiErrorNames } from 'abitype'
 
const abi = [
{ name: 'ApprovalCallerNotOwnerNorApproved', type: 'error', inputs: [] },
{ name: 'ApprovalQueryForNonexistentToken', type: 'error', inputs: [] },
] as const
 
type Result = ExtractAbiErrorNames<typeof abi>
type Result = "ApprovalCallerNotOwnerNorApproved" | "ApprovalQueryForNonexistentToken"
Try
ts
import { ExtractAbiErrorNames } from 'abitype'
 
const abi = [
{ name: 'ApprovalCallerNotOwnerNorApproved', type: 'error', inputs: [] },
{ name: 'ApprovalQueryForNonexistentToken', type: 'error', inputs: [] },
] as const
 
type Result = ExtractAbiErrorNames<typeof abi>
type Result = "ApprovalCallerNotOwnerNorApproved" | "ApprovalQueryForNonexistentToken"
Try

ExtractAbiErrors

Extracts all AbiError types from Abi.

NameDescriptionType
TAbiABI.Abi
returnsABI Errors.AbiError (union)

Example

ts
ts
import { ExtractAbiErrors } from 'abitype'
 
const abi = [
{ name: 'ApprovalCallerNotOwnerNorApproved', type: 'error', inputs: [] },
{ name: 'ApprovalQueryForNonexistentToken', type: 'error', inputs: [] },
] as const
 
type Result = ExtractAbiErrors<typeof abi>
type Result = { readonly name: "ApprovalCallerNotOwnerNorApproved"; readonly type: "error"; readonly inputs: readonly []; } | { readonly name: "ApprovalQueryForNonexistentToken"; readonly type: "error"; readonly inputs: readonly []; }
Try
ts
import { ExtractAbiErrors } from 'abitype'
 
const abi = [
{ name: 'ApprovalCallerNotOwnerNorApproved', type: 'error', inputs: [] },
{ name: 'ApprovalQueryForNonexistentToken', type: 'error', inputs: [] },
] as const
 
type Result = ExtractAbiErrors<typeof abi>
type Result = { readonly name: "ApprovalCallerNotOwnerNorApproved"; readonly type: "error"; readonly inputs: readonly []; } | { readonly name: "ApprovalQueryForNonexistentToken"; readonly type: "error"; readonly inputs: readonly []; }
Try

ExtractAbiEvent

Extracts AbiEvent with name from Abi.

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

Example

ts
ts
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'>
type Result = { readonly name: "Transfer"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "from"; readonly type: "address"; readonly indexed: true; }, { readonly name: "to"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }
Try
ts
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'>
type Result = { readonly name: "Transfer"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "from"; readonly type: "address"; readonly indexed: true; }, { readonly name: "to"; readonly type: "address"; readonly indexed: true; }, { ...; }]; }
Try

ExtractAbiEventNames

Extracts all AbiEvent names from Abi.

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

Example

ts
ts
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>
type Result = "Approval" | "Transfer"
Try
ts
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>
type Result = "Approval" | "Transfer"
Try

ExtractAbiEvents

Extracts all AbiEvent types from Abi.

NameDescriptionType
TAbiABI.Abi
returnsABI Events.AbiEvent (union)

Example

ts
ts
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>
type Result = { readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; } | { ...; }
Try
ts
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>
type Result = { readonly name: "Approval"; readonly type: "event"; readonly anonymous: false; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; readonly indexed: true; }, { readonly name: "approved"; readonly type: "address"; readonly indexed: true; }, { ...; }]; } | { ...; }
Try

ExtractAbiFunction

Extracts AbiFunction with name from Abi.

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

Example

ts
ts
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'>
type Result = { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }
Try
ts
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'>
type Result = { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; }
Try

ExtractAbiFunctionNames

Extracts all AbiFunction names from Abi.

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

Example

ts
ts
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>
type Result = "balanceOf" | "safeTransferFrom"
Try
ts
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>
type Result = "balanceOf" | "safeTransferFrom"
Try

ExtractAbiFunctions

Extracts all AbiFunction types from Abi.

NameDescriptionType
TAbiABI.Abi
returnsABI Functions.AbiFunction (union)

Example

ts
ts
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>
type Result = { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; } | { ...; }
Try
ts
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>
type Result = { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly name: "owner"; readonly type: "address"; }]; readonly outputs: readonly [{ readonly name: "balance"; readonly type: "uint256"; }]; } | { ...; }
Try

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

ts
type Result = ExtractAbiFunctions<typeof erc721Abi, 'view'>
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

ts
ts
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>
type Result = true
Try
ts
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>
type Result = true
Try

IsTypedData

Checks if type is TypedData.

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

Example

ts
ts
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>
type Result = true
Try
ts
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>
type Result = true
Try

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

ts
ts
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>
type Result = { readonly Person: { name: string; wallet: `0x${string}`; }; readonly Mail: { from: { name: string; wallet: `0x${string}`; }; to: { name: string; wallet: `0x${string}`; }; contents: string; }; }
Try
ts
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>
type Result = { readonly Person: { name: string; wallet: `0x${string}`; }; readonly Mail: { from: { name: string; wallet: `0x${string}`; }; to: { name: string; wallet: `0x${string}`; }; contents: string; }; }
Try

Released under the MIT License.