Skip to content

Human-Readable ABI

Type-level and runtime utilities for parsing human-readable ABIs

Human-Readable ABIs compress JSON ABIs into signatures that are nicer to read and less verbose to write. For example:

const abi = [
  'constructor()',
  'function balanceOf(address owner) view returns (uint256)',
  'event Transfer(address indexed from, address indexed to, uint256 amount)',
  'error ApprovalCallerNotOwnerNorApproved()',
] as const

ABIType contains parallel type-level and runtime utilities for parsing and formatting human-readable ABIs, ABI items, and ABI parameters.

Signature Types

For the most part, human-readable signatures match their Solidity counterparts and support function, event, error, struct, constructor, fallback, and receive types.

Functions

Function signatures match the following format:

function name(inputs) scope mutability returns (outputs)
  • name function name.
  • inputs function input parameters (optional).
  • scope function scope (optional). Only supports 'public' | 'external'.
  • mutability function state mutability (optional). Supports AbiStateMutability.
  • outputs function outputs (optional).

Examples

'function mint()' // name
'function withdraw(uint wad)' // name, inputs
'function activate() public' // name, scope
'function deposit() payable' // name, mutability
'function name() returns (string)' // name, outputs
'function tokenURI(uint256 tokenId) pure returns (string)' // name, inputs, mutability, outputs

Events

Event signatures match the following format:

event name(inputs)
  • name event name.
  • inputs event input parameters (optional). Parameters support the indexed modifier.

Examples

'event Mint()' // name
'event Transfer(bytes32 indexed node, address owner)' // name, inputs

Errors

Error signatures match the following format:

error name(inputs)
  • name error name.
  • inputs error input parameters (optional).

Examples

'event CriteriaNotEnabledForItem()' // name
'event InvalidRestrictedOrder(bytes32 orderHash)' // name, inputs

Structs

Struct signatures match the following format:

struct Name { properties }
  • Name struct name.
  • properties struct properties (colon-separated).

Examples

'struct AdditionalRecipient { uint256; address; }' // unnamed properties
'struct AdditionalRecipient { uint256 amount; address recipient; }' // named properties

Constructor

Constructor signatures match the following format:

constructor(parameters) mutability
  • parameters constructor parameters (optional).
  • mutability constructor state mutability (optional). Supports 'payable'.

Examples

'constructor()' // empty parameters
'constructor(address conduitController)' // name, parameters
'constructor(address conduitController) payable' // name, parameters, mutability

Fallback

Fallback signatures match the following format:

fallback() scope mutability

Examples

'fallback() external' // scope
'fallback() external payable' // scope, mutability
  • scope fallback scope. Supports 'external'.
  • mutability fallback state mutability (optional). Supports 'payable'.

Receive

Receive signatures match the following format:

receive() external payable

Examples

'receive() external payable'

Syntax Rules

Some additional rules that apply to human-readable ABIs:

  • Whitespace matters. This allows us to infer TypeScript types at the type-level and make sure signatures are valid. For example, 'function name() returns (string)' is valid, but 'function name()returns(string)' is not.
  • No semi-colons. This is a stylistic choice to make signatures more readable.
  • No recursive structs. Structs can reference other structs, but not themselves or other structs in a circular way. For example, ['struct A { B; }', 'struct B { string; }'] is valid, but 'struct A { A; }' and ['struct A { B; }', 'struct B { A; }'] are not valid.
  • Modifier keywords. Modifier keywords like 'calldata', 'memory', and 'storage' are ignored when parsing signatures. For example, 'function name(string calldata)' is valid and 'calldata' will be ignored when parsing the signature.
  • Inline tuples. Inline tuples are supported for function inputs and outputs, error, event, and constructor inputs, and struct properties. For example, '(uint256, string)' is valid and corresponds to the following JSON ABI parameter: { type: 'tuple', components: [{ type: 'uint256' }, { type: 'string' }] }. You can also nest inline tuples inside inline tuples.
  • Named and unnamed parameters. Named and unnamed parameters/properties are both supported. For example, 'string foo' is named and 'string' is unnamed.

Types

Types for parsing and formatting human-readable ABIs.

ParseAbi

Parses human-readable ABI into JSON Abi.

NameDescriptionType
signaturesHuman-Readable ABI.string[]
returnsParsed Abiabi (inferred)

Example

import { 
type ParseAbi<signatures extends readonly string[]> = string[] extends signatures ? Abi : signatures extends readonly string[] ? signatures extends Signatures<signatures> ? ParseStructs<...> extends infer sructs ? { [key in keyof signatures]: signatures[key] extends string ? ParseSignature<...> : never; } extends infer mapped extends readonly unknown[] ? Filter<...> extends infer result ? result extends readonly [] ? never : result : never : never : never : never : never

Parses human-readable ABI into JSON Abi

ParseAbi
} from 'abitype'
type
type Result = readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }, { ...; }]
Result
=
type ParseAbi<signatures extends readonly string[]> = string[] extends signatures ? Abi : signatures extends readonly string[] ? signatures extends Signatures<signatures> ? ParseStructs<...> extends infer sructs ? { [key in keyof signatures]: signatures[key] extends string ? ParseSignature<...> : never; } extends infer mapped extends readonly unknown[] ? Filter<...> extends infer result ? result extends readonly [] ? never : result : never : never : never : never : never

Parses human-readable ABI into JSON Abi

ParseAbi
<[
'function balanceOf(address owner) view returns (uint256)', 'event Transfer(address indexed from, address indexed to, uint256 amount)', ]> let
let result: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }, { ...; }]
result
:
type Result = readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }, { ...; }]
Result

ParseAbiItem

Parses human-readable ABI item (e.g. error, event, function) into ABI item.

NameDescriptionType
signatureHuman-Readable ABI item.string[]
returnsParsed ABI itemabiItem (inferred)

Example

import { 
type ParseAbiItem<signature extends string | readonly string[] | readonly unknown[]> = (signature extends string ? string extends signature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : signature extends Signature<...> ? ParseSignature<...> : never : never) | (signature extends readonly string[] ? string[] extends signature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : signature extends Signatures<...> ? ParseStructs<...> extends infer structs ? { [key in keyof signature]: ParseSignature<...>; } extends infer mapped extends readonly unknown[] ? Filter<...>[0] extends infer result ? result extends undefined ? never : result : never : never : never : never : never)

Parses human-readable ABI item (e.g. error, event, function) into Abi item

ParseAbiItem
} from 'abitype'
type
type Result = { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }
Result
=
type ParseAbiItem<signature extends string | readonly string[] | readonly unknown[]> = (signature extends string ? string extends signature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : signature extends Signature<...> ? ParseSignature<...> : never : never) | (signature extends readonly string[] ? string[] extends signature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : signature extends Signatures<...> ? ParseStructs<...> extends infer structs ? { [key in keyof signature]: ParseSignature<...>; } extends infer mapped extends readonly unknown[] ? Filter<...>[0] extends infer result ? result extends undefined ? never : result : never : never : never : never : never)

Parses human-readable ABI item (e.g. error, event, function) into Abi item

ParseAbiItem
<
'function balanceOf(address owner) view returns (uint256)' > let
let result: { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }
result
:
type Result = { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }
Result
type
type ResultStruct = { readonly name: "foo"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }]; readonly outputs: readonly [...]; }
ResultStruct
=
type ParseAbiItem<signature extends string | readonly string[] | readonly unknown[]> = (signature extends string ? string extends signature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : signature extends Signature<...> ? ParseSignature<...> : never : never) | (signature extends readonly string[] ? string[] extends signature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : signature extends Signatures<...> ? ParseStructs<...> extends infer structs ? { [key in keyof signature]: ParseSignature<...>; } extends infer mapped extends readonly unknown[] ? Filter<...>[0] extends infer result ? result extends undefined ? never : result : never : never : never : never : never)

Parses human-readable ABI item (e.g. error, event, function) into Abi item

ParseAbiItem
<[
'function foo(Baz bar) view returns (string)', 'struct Baz { string name; }', ]> let
let resultStruct: { readonly name: "foo"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }]; readonly outputs: readonly [...]; }
resultStruct
:
type ResultStruct = { readonly name: "foo"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }]; readonly outputs: readonly [...]; }
ResultStruct

ParseAbiParameter

Parses human-readable ABI parameter into AbiParameter.

NameDescriptionType
paramHuman-Readable ABI parameter.string | string[]
returnsParsed AbiParameterabiParameter (inferred)

Example

import { 
type ParseAbiParameter<param extends string | readonly string[] | readonly unknown[]> = (param extends string ? param extends "" ? never : string extends param ? AbiParameter : ParseAbiParameter<param, { modifier: Modifier; }> : never) | (param extends readonly string[] ? string[] extends param ? AbiParameter : ParseStructs<...> extends infer structs ? { [key in keyof param]: param[key] extends string ? IsStructSignature<...> extends true ? never : ParseAbiParameter<...> : never; } extends infer mapped extends readonly unknown[] ? Filter<...>[0] extends infer result ? result extends undefined ? never : result : never : never : never : never)

Parses human-readable ABI parameter into AbiParameter

ParseAbiParameter
} from 'abitype'
type
type Result = { readonly type: "address"; readonly name: "from"; }
Result
=
type ParseAbiParameter<param extends string | readonly string[] | readonly unknown[]> = (param extends string ? param extends "" ? never : string extends param ? AbiParameter : ParseAbiParameter<param, { modifier: Modifier; }> : never) | (param extends readonly string[] ? string[] extends param ? AbiParameter : ParseStructs<...> extends infer structs ? { [key in keyof param]: param[key] extends string ? IsStructSignature<...> extends true ? never : ParseAbiParameter<...> : never; } extends infer mapped extends readonly unknown[] ? Filter<...>[0] extends infer result ? result extends undefined ? never : result : never : never : never : never)

Parses human-readable ABI parameter into AbiParameter

ParseAbiParameter
<'address from'>
type
type ResultStruct = { readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }
ResultStruct
=
type ParseAbiParameter<param extends string | readonly string[] | readonly unknown[]> = (param extends string ? param extends "" ? never : string extends param ? AbiParameter : ParseAbiParameter<param, { modifier: Modifier; }> : never) | (param extends readonly string[] ? string[] extends param ? AbiParameter : ParseStructs<...> extends infer structs ? { [key in keyof param]: param[key] extends string ? IsStructSignature<...> extends true ? never : ParseAbiParameter<...> : never; } extends infer mapped extends readonly unknown[] ? Filter<...>[0] extends infer result ? result extends undefined ? never : result : never : never : never : never)

Parses human-readable ABI parameter into AbiParameter

ParseAbiParameter
<[
'Baz bar', 'struct Baz { string name; }', ]>

ParseAbiParameters

Parses human-readable ABI parameters into AbiParameters.

NameDescriptionType
paramsHuman-Readable ABI parameters.string | string[]
returnsParsed AbiParametersabiParameter[] (inferred)

Example

import { 
type ParseAbiParameters<params extends string | readonly string[] | readonly unknown[]> = (params extends string ? params extends "" ? never : string extends params ? readonly AbiParameter[] : ParseAbiParameters<SplitParameters<params>, { ...; }> : never) | (params extends readonly string[] ? string[] extends params ? AbiParameter : ParseStructs<...> extends infer structs ? { [key in keyof params]: params[key] extends string ? IsStructSignature<...> extends true ? never : ParseAbiParameters<...> : never; } extends infer mapped extends readonly unknown[] ? Filter<...> extends readonly [......] ? content["length"] extends 0 ? never : DeepFlatten<...> : never : never : never : never)

Parses human-readable ABI parameters into AbiParameters

ParseAbiParameters
} from 'abitype'
type
type Result = readonly [{ readonly type: "address"; readonly name: "from"; }, { readonly type: "uint256"; readonly name: "amount"; }]
Result
=
type ParseAbiParameters<params extends string | readonly string[] | readonly unknown[]> = (params extends string ? params extends "" ? never : string extends params ? readonly AbiParameter[] : ParseAbiParameters<SplitParameters<params>, { ...; }> : never) | (params extends readonly string[] ? string[] extends params ? AbiParameter : ParseStructs<...> extends infer structs ? { [key in keyof params]: params[key] extends string ? IsStructSignature<...> extends true ? never : ParseAbiParameters<...> : never; } extends infer mapped extends readonly unknown[] ? Filter<...> extends readonly [......] ? content["length"] extends 0 ? never : DeepFlatten<...> : never : never : never : never)

Parses human-readable ABI parameters into AbiParameters

ParseAbiParameters
<'address from, uint256 amount'>
type
type ResultStruct = readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }]
ResultStruct
=
type ParseAbiParameters<params extends string | readonly string[] | readonly unknown[]> = (params extends string ? params extends "" ? never : string extends params ? readonly AbiParameter[] : ParseAbiParameters<SplitParameters<params>, { ...; }> : never) | (params extends readonly string[] ? string[] extends params ? AbiParameter : ParseStructs<...> extends infer structs ? { [key in keyof params]: params[key] extends string ? IsStructSignature<...> extends true ? never : ParseAbiParameters<...> : never; } extends infer mapped extends readonly unknown[] ? Filter<...> extends readonly [......] ? content["length"] extends 0 ? never : DeepFlatten<...> : never : never : never : never)

Parses human-readable ABI parameters into AbiParameters

ParseAbiParameters
<[
'Baz bar', 'struct Baz { string name; }', ]>

FormatAbi

Formats Abi into human-readable ABI.

NameDescriptionType
abiABIAbi
returnsHuman-Readable ABI.string[] (inferred)

Example

import { 
type FormatAbi<abi extends Abi | readonly unknown[]> = Abi extends abi ? readonly string[] : abi extends readonly [] ? never : abi extends Abi ? { [key in keyof abi]: FormatAbiItem<abi[key]>; } : readonly string[]

Parses JSON ABI into human-readable ABI

FormatAbi
} from 'abitype'
type
type Result = ["function balanceOf(address owner) view returns (uint256)", "event Transfer(address indexed from, address indexed to, uint256 amount)"]
Result
=
type FormatAbi<abi extends Abi | readonly unknown[]> = Abi extends abi ? readonly string[] : abi extends readonly [] ? never : abi extends Abi ? { [key in keyof abi]: FormatAbiItem<abi[key]>; } : readonly string[]

Parses JSON ABI into human-readable ABI

FormatAbi
<[
{
name: "balanceOf"
name
: 'balanceOf'
type: "function"
type
: 'function'
stateMutability: "view"
stateMutability
: 'view'
inputs: [{ type: "address"; name: "owner"; }]
inputs
: [{
type: "address"
type
: 'address';
name: "owner"
name
: 'owner' }]
outputs: [{ type: "uint256"; }]
outputs
: [{
type: "uint256"
type
: 'uint256' }]
}, {
name: "Transfer"
name
: 'Transfer'
type: "event"
type
: 'event'
inputs: [{ type: "address"; name: "from"; indexed: true; }, { type: "address"; name: "to"; indexed: true; }, { type: "uint256"; name: "amount"; }]
inputs
: [
{
type: "address"
type
: 'address';
name: "from"
name
: 'from';
indexed: true
indexed
: true },
{
type: "address"
type
: 'address';
name: "to"
name
: 'to';
indexed: true
indexed
: true },
{
type: "uint256"
type
: 'uint256';
name: "amount"
name
: 'amount' },
] }, ]>

FormatAbiItem

Formats Abi item (e.g. error, event, function) into human-readable ABI parameter.

NameDescriptionType
abiItemABI itemAbi[number]
returnsHuman-Readable ABI item.string (inferred)

Example

import { 
type FormatAbiItem<abiItem extends Abi[number]> = AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive extends abiItem ? string : (abiItem extends AbiFunction ? AbiFunction extends abiItem ? string : `function ${AssertName<...>}(${FormatAbiParameters<...>})${abiItem["stateMutability"] extends "view" | ... 1 more ... | "payable" ? ` ${abiItem["stateMutability"]}` : ""}${abiItem["outputs"]["length"] extends 0 ? "" : ` returns (${FormatAbiParameters<...>})`}` : never) | ... 4 more ... | (abiItem extends AbiReceive ? AbiReceive extends abiItem ? string : "receive() external payable" : never)

Formats ABI item (e.g. error, event, function) into human-readable ABI item

FormatAbiItem
} from 'abitype'
type
type Result = "function balanceOf(address owner) view returns (uint256)"
Result
=
type FormatAbiItem<abiItem extends Abi[number]> = AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive extends abiItem ? string : (abiItem extends AbiFunction ? AbiFunction extends abiItem ? string : `function ${AssertName<...>}(${FormatAbiParameters<...>})${abiItem["stateMutability"] extends "view" | ... 1 more ... | "payable" ? ` ${abiItem["stateMutability"]}` : ""}${abiItem["outputs"]["length"] extends 0 ? "" : ` returns (${FormatAbiParameters<...>})`}` : never) | ... 4 more ... | (abiItem extends AbiReceive ? AbiReceive extends abiItem ? string : "receive() external payable" : never)

Formats ABI item (e.g. error, event, function) into human-readable ABI item

FormatAbiItem
<{
name: "balanceOf"
name
: 'balanceOf'
type: "function"
type
: 'function'
stateMutability: "view"
stateMutability
: 'view'
inputs: [{ type: "address"; name: "owner"; }]
inputs
: [{
type: "address"
type
: 'address';
name: "owner"
name
: 'owner' }]
outputs: [{ type: "uint256"; }]
outputs
: [{
type: "uint256"
type
: 'uint256' }]
}>

FormatAbiParameter

Formats AbiParameter into human-readable ABI parameter.

NameDescriptionType
abiParameterABI parameterAbiParameter
returnsHuman-Readable ABI parameters.string[] (inferred)

Example

import { 
type FormatAbiParameter<abiParameter extends AbiParameter | AbiEventParameter> = abiParameter extends { name?: infer name extends string; type: `tuple${infer array}`; components: infer components extends readonly AbiParameter[]; indexed?: infer indexed extends boolean; } ? FormatAbiParameter<...> : `${abiParameter["type"]}${abiParameter extends { ...; } ? " indexed" : ""}${abiParameter["name"] extends infer name extends string ? name extends "" ? "" : ` ${AssertName<...>}` : ""}`

Formats AbiParameter to human-readable ABI parameter.

FormatAbiParameter
} from 'abitype'
type
type Result = "address from"
Result
=
type FormatAbiParameter<abiParameter extends AbiParameter | AbiEventParameter> = abiParameter extends { name?: infer name extends string; type: `tuple${infer array}`; components: infer components extends readonly AbiParameter[]; indexed?: infer indexed extends boolean; } ? FormatAbiParameter<...> : `${abiParameter["type"]}${abiParameter extends { ...; } ? " indexed" : ""}${abiParameter["name"] extends infer name extends string ? name extends "" ? "" : ` ${AssertName<...>}` : ""}`

Formats AbiParameter to human-readable ABI parameter.

FormatAbiParameter
<{
type: "address"
type
: 'address';
name: "from"
name
: 'from' }>

FormatAbiParameters

Formats AbiParameters into human-readable ABI parameters.

NameDescriptionType
abiParametersABI parametersAbiParameter[]
returnsHuman-Readable ABI parameter.string (inferred)

Example

import { 
type FormatAbiParameters<abiParameters extends readonly [AbiParameter | AbiEventParameter, ...(readonly (AbiParameter | AbiEventParameter)[])]> = { [key in keyof abiParameters]: FormatAbiParameter<abiParameters[key]>; } extends readonly [infer head, ...infer tail] ? tail["length"] extends 0 ? `${head & string}` : `${head & string}, ${Join<...>}` : never

Formats AbiParameters to human-readable ABI parameter.

FormatAbiParameters
} from 'abitype'
type
type Result = "address from, uint256 tokenId"
Result
=
type FormatAbiParameters<abiParameters extends readonly [AbiParameter | AbiEventParameter, ...(readonly (AbiParameter | AbiEventParameter)[])]> = { [key in keyof abiParameters]: FormatAbiParameter<abiParameters[key]>; } extends readonly [infer head, ...infer tail] ? tail["length"] extends 0 ? `${head & string}` : `${head & string}, ${Join<...>}` : never

Formats AbiParameters to human-readable ABI parameter.

FormatAbiParameters
<[
{
type: "address"
type
: 'address';
name: "from"
name
: 'from' },
{
type: "uint256"
type
: 'uint256';
name: "tokenId"
name
: 'tokenId' },
]>

Utilities

Runtime functions for parsing and formatting human-readable ABIs.

parseAbi

Parses human-readable ABI into JSON Abi.

NameDescriptionType
signaturesHuman-Readable ABI.string[]
returnsParsed Abiabi (inferred)

Example

import { 
function parseAbi<const signatures extends readonly string[]>(signatures: signatures["length"] extends 0 ? Error<"At least one signature required"> : Signatures<signatures> extends signatures ? signatures : Signatures<signatures>): ParseAbi<signatures>

Parses human-readable ABI into JSON Abi

parseAbi
} from 'abitype'
const
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }, { ...; }]
abi
=
parseAbi<["function balanceOf(address owner) view returns (uint256)", "event Transfer(address indexed from, address indexed to, uint256 amount)"]>(signatures: ["function balanceOf(address owner) view returns (uint256)", "event Transfer(address indexed from, address indexed to, uint256 amount)"]): readonly [...]

Parses human-readable ABI into JSON Abi

parseAbi
([
'function balanceOf(address owner) view returns (uint256)', 'event Transfer(address indexed from, address indexed to, uint256 amount)', ])
const abi: readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }, { ...; }]
abi

parseAbiItem

Parses human-readable ABI item (e.g. error, event, function) into ABI item.

NameDescriptionType
signatureHuman-Readable ABI item.string | string[]
returnsParsed ABI itemabiItem (inferred)

Example

import { 
function parseAbiItem<signature extends string | readonly string[] | readonly unknown[]>(signature: Narrow<signature> & ((signature extends string ? string extends signature ? unknown : Signature<signature> : never) | (signature extends readonly string[] ? signature extends readonly [] ? Error<"At least one signature required."> : string[] extends signature ? unknown : Signatures<signature> : never))): ParseAbiItem<signature>

Parses human-readable ABI item (e.g. error, event, function) into Abi item

parseAbiItem
} from 'abitype'
const
const abiItem: { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }
abiItem
=
parseAbiItem<"function balanceOf(address owner) view returns (uint256)">(signature: "function balanceOf(address owner) view returns (uint256)"): { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [...]; readonly outputs: readonly [...]; }

Parses human-readable ABI item (e.g. error, event, function) into Abi item

parseAbiItem
(
'function balanceOf(address owner) view returns (uint256)', )
const abiItem: { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }
abiItem
const
const abiItemStruct: { readonly name: "foo"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }]; readonly outputs: readonly [...]; }
abiItemStruct
=
parseAbiItem<["function foo(Baz bar) view returns (string)", "struct Baz { string name; }"]>(signature: ["function foo(Baz bar) view returns (string)", "struct Baz { string name; }"]): { ...; }

Parses human-readable ABI item (e.g. error, event, function) into Abi item

parseAbiItem
([
'function foo(Baz bar) view returns (string)', 'struct Baz { string name; }', ])
const abiItemStruct: { readonly name: "foo"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }]; readonly outputs: readonly [...]; }
abiItemStruct

parseAbiParameter

Parses human-readable ABI parameter into AbiParameter.

NameDescriptionType
paramHuman-Readable ABI parameter.string | string[]
returnsParsed AbiParameterabiParameter (inferred)

Example

import { 
function parseAbiParameter<param extends string | readonly string[] | readonly unknown[]>(param: Narrow<param> & ((param extends string ? param extends "" ? Error<"Empty string is not allowed."> : unknown : never) | (param extends readonly string[] ? param extends readonly [] ? Error<"At least one parameter required."> : string[] extends param ? unknown : unknown : never))): ParseAbiParameter<param>

Parses human-readable ABI parameter into AbiParameter

parseAbiParameter
} from 'abitype'
const
const abiParameter: { readonly type: "address"; readonly name: "from"; }
abiParameter
=
parseAbiParameter<"address from">(param: "address from"): { readonly type: "address"; readonly name: "from"; }

Parses human-readable ABI parameter into AbiParameter

parseAbiParameter
('address from')
const
const abiParameterStruct: { readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }
abiParameterStruct
=
parseAbiParameter<["Baz bar", "struct Baz { string name; }"]>(param: ["Baz bar", "struct Baz { string name; }"]): { readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }

Parses human-readable ABI parameter into AbiParameter

parseAbiParameter
([
'Baz bar', 'struct Baz { string name; }', ])
const abiParameterStruct: { readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }
abiParameterStruct

parseAbiParameters

Parses human-readable ABI parameters into AbiParameters.

NameDescriptionType
paramsHuman-Readable ABI parameters.string | string[]
returnsParsed AbiParametersabiParameter[] (inferred)

Example

import { 
function parseAbiParameters<params extends string | readonly string[] | readonly unknown[]>(params: Narrow<params> & ((params extends string ? params extends "" ? Error<"Empty string is not allowed."> : unknown : never) | (params extends readonly string[] ? params extends readonly [] ? Error<"At least one parameter required."> : string[] extends params ? unknown : unknown : never))): ParseAbiParameters<params>

Parses human-readable ABI parameters into AbiParameters

parseAbiParameters
} from 'abitype'
const
const abiParameters: readonly [{ readonly type: "address"; readonly name: "from"; }, { readonly type: "address"; readonly name: "to"; }, { readonly type: "uint256"; readonly name: "amount"; }]
abiParameters
=
parseAbiParameters<"address from, address to, uint256 amount">(params: "address from, address to, uint256 amount"): readonly [{ readonly type: "address"; readonly name: "from"; }, { readonly type: "address"; readonly name: "to"; }, { ...; }]

Parses human-readable ABI parameters into AbiParameters

parseAbiParameters
(
'address from, address to, uint256 amount', )
const abiParameters: readonly [{ readonly type: "address"; readonly name: "from"; }, { readonly type: "address"; readonly name: "to"; }, { readonly type: "uint256"; readonly name: "amount"; }]
abiParameters
const
const abiParametersStruct: readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }]
abiParametersStruct
=
parseAbiParameters<["Baz bar", "struct Baz { string name; }"]>(params: ["Baz bar", "struct Baz { string name; }"]): readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }]

Parses human-readable ABI parameters into AbiParameters

parseAbiParameters
([
'Baz bar', 'struct Baz { string name; }', ])
const abiParametersStruct: readonly [{ readonly type: "tuple"; readonly components: readonly [{ readonly type: "string"; readonly name: "name"; }]; readonly name: "bar"; }]
abiParametersStruct

formatAbi

Formats Abi into human-readable ABI.

NameDescriptionType
abiABIAbi
returnsHuman-Readable ABI.string[] (inferred)

Example

import { 
function formatAbi<const abi extends Abi | readonly unknown[]>(abi: abi): FormatAbi<abi>

Parses JSON ABI into human-readable ABI

formatAbi
} from 'abitype'
const
const result: readonly ["function balanceOf(address owner) view returns (uint256)", "event Transfer(address indexed from, address indexed to, uint256 amount)"]
result
=
formatAbi<readonly [{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }, { ...; }]>(abi: readonly [...]): readonly [...]

Parses JSON ABI into human-readable ABI

formatAbi
([
{
name: "balanceOf"
name
: 'balanceOf',
type: "function"
type
: 'function',
stateMutability: "view"
stateMutability
: 'view',
inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]
inputs
: [{
type: "address"
type
: 'address',
name: "owner"
name
: 'owner' }],
outputs: readonly [{ readonly type: "uint256"; }]
outputs
: [{
type: "uint256"
type
: 'uint256' }],
}, {
name: "Transfer"
name
: 'Transfer',
type: "event"
type
: 'event',
inputs: readonly [{ readonly type: "address"; readonly name: "from"; readonly indexed: true; }, { readonly type: "address"; readonly name: "to"; readonly indexed: true; }, { readonly type: "uint256"; readonly name: "amount"; }]
inputs
: [
{
type: "address"
type
: 'address',
name: "from"
name
: 'from',
indexed: true
indexed
: true },
{
type: "address"
type
: 'address',
name: "to"
name
: 'to',
indexed: true
indexed
: true },
{
type: "uint256"
type
: 'uint256',
name: "amount"
name
: 'amount' },
], }, ])

formatAbiItem

Formats Abi item (e.g. error, event, function) into human-readable ABI parameter.

NameDescriptionType
abiItemABI itemAbi[number]
returnsHuman-Readable ABI item.string (inferred)

Example

import { 
function formatAbiItem<const abiItem extends Abi[number]>(abiItem: abiItem): FormatAbiItem<abiItem>

Formats ABI item (e.g. error, event, function) into human-readable ABI item

formatAbiItem
} from 'abitype'
const
const result: "function balanceOf(address owner) view returns (uint256)"
result
=
formatAbiItem<{ readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }>(abiItem: { readonly name: "balanceOf"; readonly type: "function"; readonly stateMutability: "view"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]; readonly outputs: readonly [{ readonly type: "uint256"; }]; }): "function balanceOf(address owner) view returns (uint256)"

Formats ABI item (e.g. error, event, function) into human-readable ABI item

formatAbiItem
({
name: "balanceOf"
name
: 'balanceOf',
type: "function"
type
: 'function',
stateMutability: "view"
stateMutability
: 'view',
inputs: readonly [{ readonly type: "address"; readonly name: "owner"; }]
inputs
: [{
type: "address"
type
: 'address',
name: "owner"
name
: 'owner' }],
outputs: readonly [{ readonly type: "uint256"; }]
outputs
: [{
type: "uint256"
type
: 'uint256' }],
})

formatAbiParameter

Formats AbiParameter into human-readable ABI parameter.

NameDescriptionType
abiParameterABI parameterAbiParameter
returnsHuman-Readable ABI parameter.string (inferred)

Example

import { 
function formatAbiParameter<const abiParameter extends AbiParameter | AbiEventParameter>(abiParameter: abiParameter): FormatAbiParameter<abiParameter>

Formats AbiParameter to human-readable ABI parameter.

formatAbiParameter
} from 'abitype'
const
const result: "address from"
result
=
formatAbiParameter<{ readonly type: "address"; readonly name: "from"; }>(abiParameter: { readonly type: "address"; readonly name: "from"; }): "address from"

Formats AbiParameter to human-readable ABI parameter.

formatAbiParameter
({
type: "address"
type
: 'address',
name: "from"
name
: 'from' })

formatAbiParameters

Formats AbiParameters into human-readable ABI parameters.

NameDescriptionType
abiParametersABI parametersAbiParameter[]
returnsHuman-Readable ABI parameter.string (inferred)

Example

import { 
function formatAbiParameters<const abiParameters extends readonly [AbiParameter | AbiEventParameter, ...(readonly (AbiParameter | AbiEventParameter)[])]>(abiParameters: abiParameters): FormatAbiParameters<abiParameters>

Formats AbiParameters to human-readable ABI parameters.

formatAbiParameters
} from 'abitype'
const
const result: "address from, uint256 tokenId"
result
=
formatAbiParameters<readonly [{ readonly type: "address"; readonly name: "from"; }, { readonly type: "uint256"; readonly name: "tokenId"; }]>(abiParameters: readonly [{ readonly type: "address"; readonly name: "from"; }, { readonly type: "uint256"; readonly name: "tokenId"; }]): "address from, uint256 tokenId"

Formats AbiParameters to human-readable ABI parameters.

formatAbiParameters
([
{
type: "address"
type
: 'address',
name: "from"
name
: 'from' },
{
type: "uint256"
type
: 'uint256',
name: "tokenId"
name
: 'tokenId' },
])

Errors

import {
  
class CircularReferenceError
CircularReferenceError
,
class InvalidParenthesisError
InvalidParenthesisError
,
class UnknownSignatureError
UnknownSignatureError
,
class InvalidSignatureError
InvalidSignatureError
,
class InvalidStructSignatureError
InvalidStructSignatureError
,
class InvalidAbiParameterError
InvalidAbiParameterError
,
class InvalidAbiParametersError
InvalidAbiParametersError
,
class InvalidParameterError
InvalidParameterError
,
class SolidityProtectedKeywordError
SolidityProtectedKeywordError
,
class InvalidModifierError
InvalidModifierError
,
class InvalidFunctionModifierError
InvalidFunctionModifierError
,
class InvalidAbiTypeParameterError
InvalidAbiTypeParameterError
,
class InvalidAbiItemError
InvalidAbiItemError
,
class UnknownTypeError
UnknownTypeError
,
} from 'abitype'