Human-Readable ABI
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). SupportsAbiStateMutability
.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 theindexed
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
.
Name | Description | Type |
---|---|---|
TSignatures | Human-Readable ABI. | string[] |
returns | Parsed Abi | TAbi (inferred) |
Example
import { type ParseAbi<TSignatures extends readonly string[]> = string[] extends TSignatures ? Abi : TSignatures extends readonly string[] ? TSignatures extends Signatures<TSignatures> ? ParseStructs<...> extends infer Structs ? { [K in keyof TSignatures]: TSignatures[K] extends string ? ParseSignature<...> : never; } extends infer Mapped extends readonly unknown[] ? Filter<...> extends infer Result ? Result extends readonly [] ? never : Result : never : never : never : never : neverParses 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<TSignatures extends readonly string[]> = string[] extends TSignatures ? Abi : TSignatures extends readonly string[] ? TSignatures extends Signatures<TSignatures> ? ParseStructs<...> extends infer Structs ? { [K in keyof TSignatures]: TSignatures[K] extends string ? ParseSignature<...> : never; } extends infer Mapped extends readonly unknown[] ? Filter<...> extends infer Result ? Result extends readonly [] ? never : Result : never : never : never : never : neverParses 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.
Name | Description | Type |
---|---|---|
TSignature | Human-Readable ABI item. | string[] |
returns | Parsed ABI item | TAbiItem (inferred) |
Example
import { type ParseAbiItem<TSignature extends string | readonly string[] | readonly unknown[]> = (TSignature extends string ? string extends TSignature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : TSignature extends Signature<...> ? ParseSignature<...> : never : never) | (TSignature extends readonly string[] ? string[] extends TSignature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : TSignature extends Signatures<...> ? ParseStructs<...> extends infer Structs ? { [K in keyof TSignature]: 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<TSignature extends string | readonly string[] | readonly unknown[]> = (TSignature extends string ? string extends TSignature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : TSignature extends Signature<...> ? ParseSignature<...> : never : never) | (TSignature extends readonly string[] ? string[] extends TSignature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : TSignature extends Signatures<...> ? ParseStructs<...> extends infer Structs ? { [K in keyof TSignature]: 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<TSignature extends string | readonly string[] | readonly unknown[]> = (TSignature extends string ? string extends TSignature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : TSignature extends Signature<...> ? ParseSignature<...> : never : never) | (TSignature extends readonly string[] ? string[] extends TSignature ? AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive : TSignature extends Signatures<...> ? ParseStructs<...> extends infer Structs ? { [K in keyof TSignature]: 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
.
Name | Description | Type |
---|---|---|
TParam | Human-Readable ABI parameter. | string | string[] |
returns | Parsed AbiParameter | TAbiParameter (inferred) |
Example
import { type ParseAbiParameter<TParam extends string | readonly string[] | readonly unknown[]> = (TParam extends string ? TParam extends "" ? never : string extends TParam ? AbiParameter : ParseAbiParameter_<TParam, {
Modifier: Modifier;
}> : never) | (TParam extends readonly string[] ? string[] extends TParam ? AbiParameter : ParseStructs<...> extends infer Structs ? { [K in keyof TParam]: TParam[K] 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<TParam extends string | readonly string[] | readonly unknown[]> = (TParam extends string ? TParam extends "" ? never : string extends TParam ? AbiParameter : ParseAbiParameter_<TParam, {
Modifier: Modifier;
}> : never) | (TParam extends readonly string[] ? string[] extends TParam ? AbiParameter : ParseStructs<...> extends infer Structs ? { [K in keyof TParam]: TParam[K] 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<TParam extends string | readonly string[] | readonly unknown[]> = (TParam extends string ? TParam extends "" ? never : string extends TParam ? AbiParameter : ParseAbiParameter_<TParam, {
Modifier: Modifier;
}> : never) | (TParam extends readonly string[] ? string[] extends TParam ? AbiParameter : ParseStructs<...> extends infer Structs ? { [K in keyof TParam]: TParam[K] 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 AbiParameter
s.
Name | Description | Type |
---|---|---|
TParams | Human-Readable ABI parameters. | string | string[] |
returns | Parsed AbiParameter s | TAbiParameter[] (inferred) |
Example
import { type ParseAbiParameters<TParams extends string | readonly string[] | readonly unknown[]> = (TParams extends string ? TParams extends "" ? never : string extends TParams ? readonly AbiParameter[] : ParseAbiParameters_<SplitParameters<TParams>, {
...;
}> : never) | (TParams extends readonly string[] ? string[] extends TParams ? AbiParameter : ParseStructs<...> extends infer Structs ? { [K in keyof TParams]: TParams[K] extends string ? IsStructSignature<...> extends true ? never : ParseAbiParameters_<...> : 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 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<TParams extends string | readonly string[] | readonly unknown[]> = (TParams extends string ? TParams extends "" ? never : string extends TParams ? readonly AbiParameter[] : ParseAbiParameters_<SplitParameters<TParams>, {
...;
}> : never) | (TParams extends readonly string[] ? string[] extends TParams ? AbiParameter : ParseStructs<...> extends infer Structs ? { [K in keyof TParams]: TParams[K] extends string ? IsStructSignature<...> extends true ? never : ParseAbiParameters_<...> : 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 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<TParams extends string | readonly string[] | readonly unknown[]> = (TParams extends string ? TParams extends "" ? never : string extends TParams ? readonly AbiParameter[] : ParseAbiParameters_<SplitParameters<TParams>, {
...;
}> : never) | (TParams extends readonly string[] ? string[] extends TParams ? AbiParameter : ParseStructs<...> extends infer Structs ? { [K in keyof TParams]: TParams[K] extends string ? IsStructSignature<...> extends true ? never : ParseAbiParameters_<...> : 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 parameters into AbiParameters
ParseAbiParameters<[
'Baz bar',
'struct Baz { string name; }',
]>
FormatAbi
Formats Abi
into human-readable ABI.
Name | Description | Type |
---|---|---|
TAbi | ABI | Abi |
returns | Human-Readable ABI. | string[] (inferred) |
Example
import { type FormatAbi<TAbi extends Abi | readonly unknown[]> = Abi extends TAbi ? readonly string[] : TAbi extends readonly [] ? never : TAbi extends Abi ? { [K in keyof TAbi]: FormatAbiItem<TAbi[K]>; } : 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<TAbi extends Abi | readonly unknown[]> = Abi extends TAbi ? readonly string[] : TAbi extends readonly [] ? never : TAbi extends Abi ? { [K in keyof TAbi]: FormatAbiItem<TAbi[K]>; } : 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: trueindexed: true },
{ type: "address"type: 'address'; name: "to"name: 'to'; indexed: trueindexed: true },
{ type: "uint256"type: 'uint256'; name: "amount"name: 'amount' },
]
},
]>
FormatAbiItem
Formats Abi item (e.g. error, event, function) into human-readable ABI parameter.
Name | Description | Type |
---|---|---|
TAbiItem | ABI item | Abi[number] |
returns | Human-Readable ABI item. | string (inferred) |
Example
import { type FormatAbiItem<TAbiItem extends AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive> = AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive extends TAbiItem ? string : (TAbiItem extends AbiFunction ? AbiFunction extends TAbiItem ? string : `function ${AssertName<...>}(${FormatAbiParameters<...>})${TAbiItem["stateMutability"] extends "view" | ... 1 more ... | "payable" ? ` ${TAbiItem["stateMutability"]}` : ""}${TAbiItem["outputs"]["length"] extends 0 ? "" : ` returns (${FormatAbiParameters<...>})`}` : never) | ... 4 more ... | (TAbiItem extends AbiReceive ? AbiReceive extends TAbiItem ? 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<TAbiItem extends AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive> = AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive extends TAbiItem ? string : (TAbiItem extends AbiFunction ? AbiFunction extends TAbiItem ? string : `function ${AssertName<...>}(${FormatAbiParameters<...>})${TAbiItem["stateMutability"] extends "view" | ... 1 more ... | "payable" ? ` ${TAbiItem["stateMutability"]}` : ""}${TAbiItem["outputs"]["length"] extends 0 ? "" : ` returns (${FormatAbiParameters<...>})`}` : never) | ... 4 more ... | (TAbiItem extends AbiReceive ? AbiReceive extends TAbiItem ? 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.
Name | Description | Type |
---|---|---|
TAbiParameter | ABI parameter | AbiParameter |
returns | Human-Readable ABI parameters. | string[] (inferred) |
Example
import { type FormatAbiParameter<TAbiParameter extends AbiParameter | AbiEventParameter> = TAbiParameter extends {
name?: infer Name extends string | undefined;
type: `tuple${infer Array}`;
components: infer Components extends readonly AbiParameter[];
indexed?: infer Indexed extends boolean | undefined;
} ? FormatAbiParameter<...> : `${TAbiParameter["type"]}${TAbiParameter extends {
...;
} ? " indexed" : ""}${TAbiParameter["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<TAbiParameter extends AbiParameter | AbiEventParameter> = TAbiParameter extends {
name?: infer Name extends string | undefined;
type: `tuple${infer Array}`;
components: infer Components extends readonly AbiParameter[];
indexed?: infer Indexed extends boolean | undefined;
} ? FormatAbiParameter<...> : `${TAbiParameter["type"]}${TAbiParameter extends {
...;
} ? " indexed" : ""}${TAbiParameter["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 AbiParameter
s into human-readable ABI parameters.
Name | Description | Type |
---|---|---|
TAbiParameters | ABI parameters | AbiParameter[] |
returns | Human-Readable ABI parameter. | string (inferred) |
Example
import { type FormatAbiParameters<TAbiParameters extends readonly [AbiParameter | AbiEventParameter, ...(AbiParameter | AbiEventParameter)[]]> = { [K in keyof TAbiParameters]: FormatAbiParameter<TAbiParameters[K]>; } extends readonly [infer F, ...infer R] ? R["length"] extends 0 ? `${F & string}` : `${F & string}, ${Join<...>}` : neverFormats AbiParameters to human-readable ABI parameter.
FormatAbiParameters } from 'abitype'
type type Result = "address from, uint256 tokenId"Result = type FormatAbiParameters<TAbiParameters extends readonly [AbiParameter | AbiEventParameter, ...(AbiParameter | AbiEventParameter)[]]> = { [K in keyof TAbiParameters]: FormatAbiParameter<TAbiParameters[K]>; } extends readonly [infer F, ...infer R] ? R["length"] extends 0 ? `${F & string}` : `${F & string}, ${Join<...>}` : neverFormats 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
.
Name | Description | Type |
---|---|---|
signatures | Human-Readable ABI. | string[] |
returns | Parsed Abi | TAbi (inferred) |
Example
import { function parseAbi<const TSignatures extends readonly string[]>(signatures: TSignatures['length'] extends 0 ? Error<'At least one signature required'> : Signatures<TSignatures> extends TSignatures ? TSignatures : Signatures<TSignatures>): ParseAbi<TSignatures>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.
Name | Description | Type |
---|---|---|
signature | Human-Readable ABI item. | string | string[] |
returns | Parsed ABI item | TAbiItem (inferred) |
Example
import { function parseAbiItem<TSignature extends string | readonly string[] | readonly unknown[]>(signature: Narrow<TSignature> & ((TSignature extends string ? string extends TSignature ? unknown : Signature<TSignature> : never) | (TSignature extends readonly string[] ? TSignature extends readonly [] ? Error<'At least one signature required.'> : string[] extends TSignature ? unknown : Signatures<TSignature> : never))): ParseAbiItem<TSignature>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
.
Name | Description | Type |
---|---|---|
param | Human-Readable ABI parameter. | string | string[] |
returns | Parsed AbiParameter | TAbiParameter (inferred) |
Example
import { function parseAbiParameter<TParam extends string | readonly string[] | readonly unknown[]>(param: Narrow<TParam> & ((TParam extends string ? TParam extends '' ? Error<'Empty string is not allowed.'> : unknown : never) | (TParam extends readonly string[] ? TParam extends readonly [] ? Error<'At least one parameter required.'> : string[] extends TParam ? unknown : unknown : never))): ParseAbiParameter<TParam>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 AbiParameter
s.
Name | Description | Type |
---|---|---|
params | Human-Readable ABI parameters. | string | string[] |
returns | Parsed AbiParameter s | TAbiParameter[] (inferred) |
Example
import { function parseAbiParameters<TParams extends string | readonly string[] | readonly unknown[]>(params: Narrow<TParams> & ((TParams extends string ? TParams extends '' ? Error<'Empty string is not allowed.'> : unknown : never) | (TParams extends readonly string[] ? TParams extends readonly [] ? Error<'At least one parameter required.'> : string[] extends TParams ? unknown : unknown : never))): ParseAbiParameters<TParams>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.
Name | Description | Type |
---|---|---|
abi | ABI | Abi |
returns | Human-Readable ABI. | string[] (inferred) |
Example
import { function formatAbi<const TAbi extends Abi | readonly unknown[]>(abi: TAbi): FormatAbi<TAbi>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: trueindexed: true },
{ type: "address"type: 'address', name: "to"name: 'to', indexed: trueindexed: true },
{ type: "uint256"type: 'uint256', name: "amount"name: 'amount' },
],
},
])
formatAbiItem
Formats Abi item (e.g. error, event, function) into human-readable ABI parameter.
Name | Description | Type |
---|---|---|
abiItem | ABI item | Abi[number] |
returns | Human-Readable ABI item. | string (inferred) |
Example
import { function formatAbiItem<const TAbiItem extends AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive>(abiItem: TAbiItem): FormatAbiItem<TAbiItem>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: {
...;
}): "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.
Name | Description | Type |
---|---|---|
abiParameter | ABI parameter | AbiParameter |
returns | Human-Readable ABI parameter. | string (inferred) |
Example
import { function formatAbiParameter<const TAbiParameter extends AbiParameter | AbiEventParameter>(abiParameter: TAbiParameter): FormatAbiParameter<TAbiParameter>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 AbiParameter
s into human-readable ABI parameters.
Name | Description | Type |
---|---|---|
abiParameters | ABI parameters | AbiParameter[] |
returns | Human-Readable ABI parameter. | string (inferred) |
Example
import { function formatAbiParameters<const TAbiParameters extends readonly [AbiParameter | AbiEventParameter, ...(AbiParameter | AbiEventParameter)[]]>(abiParameters: TAbiParameters): FormatAbiParameters<TAbiParameters>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 CircularReferenceErrorCircularReferenceError,
class InvalidParenthesisErrorInvalidParenthesisError,
class UnknownSignatureErrorUnknownSignatureError,
class InvalidSignatureErrorInvalidSignatureError,
class InvalidStructSignatureErrorInvalidStructSignatureError,
class InvalidAbiParameterErrorInvalidAbiParameterError,
class InvalidAbiParametersErrorInvalidAbiParametersError,
class InvalidParameterErrorInvalidParameterError,
class SolidityProtectedKeywordErrorSolidityProtectedKeywordError,
class InvalidModifierErrorInvalidModifierError,
class InvalidFunctionModifierErrorInvalidFunctionModifierError,
class InvalidAbiTypeParameterErrorInvalidAbiTypeParameterError,
class InvalidAbiItemErrorInvalidAbiItemError,
class UnknownTypeErrorUnknownTypeError,
} from 'abitype'