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 constconst abi = [
'constructor()',
'function balanceOf(address owner) view returns (uint256)',
'event Transfer(address indexed from, address indexed to, uint256 amount)',
'error ApprovalCallerNotOwnerNorApproved()',
] as constconst abi = [
{
inputs: [],
stateMutability: 'nonpayable',
type: 'constructor',
},
{
inputs: [{ name: 'owner', type: 'address' }],
name: 'balanceOf',
outputs: [{ type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
name: 'from',
type: 'address',
indexed: true,
},
{ name: 'to', type: 'address', indexed: true },
{
name: 'tokenId',
type: 'uint256',
indexed: true,
},
],
name: 'Transfer',
type: 'event',
},
{ inputs: [], name: 'ApprovalCallerNotOwnerNorApproved', type: 'error' },
] as constconst abi = [
{
inputs: [],
stateMutability: 'nonpayable',
type: 'constructor',
},
{
inputs: [{ name: 'owner', type: 'address' }],
name: 'balanceOf',
outputs: [{ type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
name: 'from',
type: 'address',
indexed: true,
},
{ name: 'to', type: 'address', indexed: true },
{
name: 'tokenId',
type: 'uint256',
indexed: true,
},
],
name: 'Transfer',
type: 'event',
},
{ inputs: [], name: 'ApprovalCallerNotOwnerNorApproved', type: 'error' },
] as constABIType 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)function name(inputs) scope mutability returns (outputs)namefunction name.inputsfunction input parameters (optional).scopefunction scope (optional). Only supports'public' | 'external'.mutabilityfunction state mutability (optional). SupportsAbiStateMutability.outputsfunction 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'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, outputsEvents
Event signatures match the following format:
event name(inputs)event name(inputs)nameevent name.inputsevent input parameters (optional). Parameters support theindexedmodifier.
Examples
'event Mint()' // name
'event Transfer(bytes32 indexed node, address owner)' // name, inputs'event Mint()' // name
'event Transfer(bytes32 indexed node, address owner)' // name, inputsErrors
Error signatures match the following format:
error name(inputs)error name(inputs)nameerror name.inputserror input parameters (optional).
Examples
'event CriteriaNotEnabledForItem()' // name
'event InvalidRestrictedOrder(bytes32 orderHash)' // name, inputs'event CriteriaNotEnabledForItem()' // name
'event InvalidRestrictedOrder(bytes32 orderHash)' // name, inputsStructs
Struct signatures match the following format:
struct Name { properties }struct Name { properties }Namestruct name.propertiesstruct properties (colon-separated).
Examples
'struct AdditionalRecipient { uint256; address; }' // unnamed properties
'struct AdditionalRecipient { uint256 amount; address recipient; }' // named properties'struct AdditionalRecipient { uint256; address; }' // unnamed properties
'struct AdditionalRecipient { uint256 amount; address recipient; }' // named propertiesConstructor
Constructor signatures match the following format:
constructor(parameters) mutabilityconstructor(parameters) mutabilityparametersconstructor parameters (optional).mutabilityconstructor state mutability (optional). Supports'payable'.
Examples
'constructor()' // empty parameters
'constructor(address conduitController)' // name, parameters
'constructor(address conduitController) payable' // name, parameters, mutability'constructor()' // empty parameters
'constructor(address conduitController)' // name, parameters
'constructor(address conduitController) payable' // name, parameters, mutabilityFallback
Fallback signatures match the following format:
fallback() scope mutabilityfallback() scope mutabilityExamples
'fallback() external' // scope
'fallback() external payable' // scope, mutability'fallback() external' // scope
'fallback() external payable' // scope, mutabilityscopefallback scope. Supports'external'.mutabilityfallback state mutability (optional). Supports'payable'.
Receive
Receive signatures match the following format:
receive() external payablereceive() external payableExamples
'receive() external payable''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
tsTryimport {ParseAbi } from 'abitype'typeResult =ParseAbi <['function balanceOf(address owner) view returns (uint256)','event Transfer(address indexed from, address indexed to, uint256 amount)',]>
tsTryimport {ParseAbi } from 'abitype'typeResult =ParseAbi <['function balanceOf(address owner) view returns (uint256)','event Transfer(address indexed from, address indexed to, uint256 amount)',]>
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
tsTryimport {ParseAbiItem } from 'abitype'typeResult =ParseAbiItem <'function balanceOf(address owner) view returns (uint256)'>typeResultStruct =ParseAbiItem <['function foo(Baz bar) view returns (string)','struct Baz { string name; }',]>
tsTryimport {ParseAbiItem } from 'abitype'typeResult =ParseAbiItem <'function balanceOf(address owner) view returns (uint256)'>typeResultStruct =ParseAbiItem <['function foo(Baz bar) view returns (string)','struct Baz { string name; }',]>
ParseAbiParameter
Parses human-readable ABI parameter into AbiParameter.
| Name | Description | Type |
|---|---|---|
TParam | Human-Readable ABI parameter. | string | string[] |
| returns | Parsed AbiParameter | TAbiParameter (inferred) |
Example
tsTryimport {ParseAbiParameter } from 'abitype'typeResult =ParseAbiParameter <'address from'>typeResultStruct =ParseAbiParameter <['Baz bar','struct Baz { string name; }',]>
tsTryimport {ParseAbiParameter } from 'abitype'typeResult =ParseAbiParameter <'address from'>typeResultStruct =ParseAbiParameter <['Baz bar','struct Baz { string name; }',]>
ParseAbiParameters
Parses human-readable ABI parameters into AbiParameters.
| Name | Description | Type |
|---|---|---|
TParams | Human-Readable ABI parameters. | string | string[] |
| returns | Parsed AbiParameters | TAbiParameter[] (inferred) |
Example
tsTryimport {ParseAbiParameters } from 'abitype'typeResult =ParseAbiParameters <'address from, address to, uint256 amount'>typeResultStruct =ParseAbiParameters <['Baz bar','struct Baz { string name; }',]>
tsTryimport {ParseAbiParameters } from 'abitype'typeResult =ParseAbiParameters <'address from, address to, uint256 amount'>typeResultStruct =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
tsTryimport {FormatAbi } from 'abitype'typeResult =FormatAbi <[{name : 'balanceOf'type : 'function'stateMutability : 'view'inputs : [{type : 'address';name : 'owner' }]outputs : [{type : 'uint256' }]},{name : 'Transfer'type : 'event'inputs : [{type : 'address';name : 'from';indexed : true },{type : 'address';name : 'to';indexed : true },{type : 'uint256';name : 'amount' },]},]>
tsTryimport {FormatAbi } from 'abitype'typeResult =FormatAbi <[{name : 'balanceOf'type : 'function'stateMutability : 'view'inputs : [{type : 'address';name : 'owner' }]outputs : [{type : 'uint256' }]},{name : 'Transfer'type : 'event'inputs : [{type : 'address';name : 'from';indexed : true },{type : 'address';name : 'to';indexed : true },{type : 'uint256';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
tsTryimport {FormatAbiItem } from 'abitype'typeResult =FormatAbiItem <{name : 'balanceOf'type : 'function'stateMutability : 'view'inputs : [{type : 'address';name : 'owner' }]outputs : [{type : 'uint256' }]}>
tsTryimport {FormatAbiItem } from 'abitype'typeResult =FormatAbiItem <{name : 'balanceOf'type : 'function'stateMutability : 'view'inputs : [{type : 'address';name : 'owner' }]outputs : [{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
tsTryimport {FormatAbiParameter } from 'abitype'typeResult =FormatAbiParameter <{type : 'address';name : 'from' }>
tsTryimport {FormatAbiParameter } from 'abitype'typeResult =FormatAbiParameter <{type : 'address';name : 'from' }>
FormatAbiParameters
Formats AbiParameters into human-readable ABI parameters.
| Name | Description | Type |
|---|---|---|
TAbiParameters | ABI parameters | AbiParameter[] |
| returns | Human-Readable ABI parameter. | string (inferred) |
Example
tsTryimport {FormatAbiParameters } from 'abitype'typeResult =FormatAbiParameters <[{type : 'address';name : 'from' },{type : 'uint256';name : 'tokenId' },]>
tsTryimport {FormatAbiParameters } from 'abitype'typeResult =FormatAbiParameters <[{type : 'address';name : 'from' },{type : 'uint256';name : 'tokenId' },]>
Utilities
Runtime functions for parsing and formatting human-readable ABIs.
WARNING
These functions throw errors for invalid inputs. Make sure you handle errors appropriately.
parseAbi
Parses human-readable ABI into JSON Abi.
| Name | Description | Type |
|---|---|---|
signatures | Human-Readable ABI. | string[] |
| returns | Parsed Abi | TAbi (inferred) |
Example
tsTryimport {parseAbi } from 'abitype'constabi =parseAbi (['function balanceOf(address owner) view returns (uint256)','event Transfer(address indexed from, address indexed to, uint256 amount)',])
tsTryimport {parseAbi } from 'abitype'constabi =parseAbi (['function balanceOf(address owner) view returns (uint256)','event Transfer(address indexed from, address indexed to, uint256 amount)',])
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
tsTryimport {parseAbiItem } from 'abitype'constabiItem =parseAbiItem ('function balanceOf(address owner) view returns (uint256)',)constabiItemStruct =parseAbiItem (['function foo(Baz bar) view returns (string)','struct Baz { string name; }',])
tsTryimport {parseAbiItem } from 'abitype'constabiItem =parseAbiItem ('function balanceOf(address owner) view returns (uint256)',)constabiItemStruct =parseAbiItem (['function foo(Baz bar) view returns (string)','struct Baz { string name; }',])
parseAbiParameter
Parses human-readable ABI parameter into AbiParameter.
| Name | Description | Type |
|---|---|---|
param | Human-Readable ABI parameter. | string | string[] |
| returns | Parsed AbiParameter | TAbiParameter (inferred) |
Example
tsTryimport {parseAbiParameter } from 'abitype'constabiParameter =parseAbiParameter ('address from')constabiParameterStruct =parseAbiParameter (['Baz bar','struct Baz { string name; }',])
tsTryimport {parseAbiParameter } from 'abitype'constabiParameter =parseAbiParameter ('address from')constabiParameterStruct =parseAbiParameter (['Baz bar','struct Baz { string name; }',])
parseAbiParameters
Parses human-readable ABI parameters into AbiParameters.
| Name | Description | Type |
|---|---|---|
params | Human-Readable ABI parameters. | string | string[] |
| returns | Parsed AbiParameters | TAbiParameter[] (inferred) |
Example
tsTryimport {parseAbiParameters } from 'abitype'constabiParameters =parseAbiParameters ('address from, address to, uint256 amount',)constabiParametersStruct =parseAbiParameters (['Baz bar','struct Baz { string name; }',])
tsTryimport {parseAbiParameters } from 'abitype'constabiParameters =parseAbiParameters ('address from, address to, uint256 amount',)constabiParametersStruct =parseAbiParameters (['Baz bar','struct Baz { string name; }',])
formatAbi
Formats Abi into human-readable ABI.
| Name | Description | Type |
|---|---|---|
abi | ABI | Abi |
| returns | Human-Readable ABI. | string[] (inferred) |
Example
tsTryimport {formatAbi } from 'abitype'constresult =formatAbi ([{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{type : 'address',name : 'owner' }],outputs : [{type : 'uint256' }],},{name : 'Transfer',type : 'event',inputs : [{type : 'address',name : 'from',indexed : true },{type : 'address',name : 'to',indexed : true },{type : 'uint256',name : 'amount' },],},])
tsTryimport {formatAbi } from 'abitype'constresult =formatAbi ([{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{type : 'address',name : 'owner' }],outputs : [{type : 'uint256' }],},{name : 'Transfer',type : 'event',inputs : [{type : 'address',name : 'from',indexed : true },{type : 'address',name : 'to',indexed : true },{type : 'uint256',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
tsTryimport {formatAbiItem } from 'abitype'constresult =formatAbiItem ({name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{type : 'address',name : 'owner' }],outputs : [{type : 'uint256' }],})
tsTryimport {formatAbiItem } from 'abitype'constresult =formatAbiItem ({name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{type : 'address',name : 'owner' }],outputs : [{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
tsTryimport {formatAbiParameter } from 'abitype'constresult =formatAbiParameter ({type : 'address',name : 'from' })
tsTryimport {formatAbiParameter } from 'abitype'constresult =formatAbiParameter ({type : 'address',name : 'from' })
formatAbiParameters
Formats AbiParameters into human-readable ABI parameters.
| Name | Description | Type |
|---|---|---|
abiParameters | ABI parameters | AbiParameter[] |
| returns | Human-Readable ABI parameter. | string (inferred) |
Example
tsTryimport {formatAbiParameters } from 'abitype'constresult =formatAbiParameters ([{type : 'address',name : 'from' },{type : 'uint256',name : 'tokenId' },])
tsTryimport {formatAbiParameters } from 'abitype'constresult =formatAbiParameters ([{type : 'address',name : 'from' },{type : 'uint256',name : 'tokenId' },])
Errors
tsTryimport {CircularReferenceError ,InvalidParenthesisError ,UnknownSignatureError ,InvalidSignatureError ,InvalidStructSignatureError ,InvalidAbiParameterError ,InvalidAbiParametersError ,InvalidParameterError ,SolidityProtectedKeywordError ,InvalidModifierError ,InvalidFunctionModifierError ,InvalidAbiTypeParameterError ,InvalidAbiItemError ,UnknownTypeError ,} from 'abitype'
tsTryimport {CircularReferenceError ,InvalidParenthesisError ,UnknownSignatureError ,InvalidSignatureError ,InvalidStructSignatureError ,InvalidAbiParameterError ,InvalidAbiParametersError ,InvalidParameterError ,SolidityProtectedKeywordError ,InvalidModifierError ,InvalidFunctionModifierError ,InvalidAbiTypeParameterError ,InvalidAbiItemError ,UnknownTypeError ,} from 'abitype'