Utilities
Utility types for working with ABIs and EIP-712 Typed Data.
AbiParameterToPrimitiveType
Converts AbiParameter
to corresponding TypeScript primitive type.
Name | Description | Type |
---|---|---|
TAbiParameter | Parameter to convert to TypeScript representation. | AbiParameter |
TAbiParameterKind | Kind to narrow by parameter type. | AbiParameterKind (optional) |
returns | TypeScript primitive type. | TType (inferred) |
Example
tsTry
import {AbiParameterToPrimitiveType } from 'abitype'typeResult =AbiParameterToPrimitiveType <{name : 'owner'type : 'address'}>
tsTry
import {AbiParameterToPrimitiveType } from 'abitype'typeResult =AbiParameterToPrimitiveType <{name : 'owner'type : 'address'}>
AbiParametersToPrimitiveTypes
Converts array of AbiParameter
to corresponding TypeScript primitive types.
Name | Description | Type |
---|---|---|
TAbiParameters | Parameters to convert to TypeScript representations. | readonly AbiParameter[] |
TAbiParameterKind | Kind to narrow by parameter type. | AbiParameterKind (optional) |
returns | TypeScript primitive types. | TType[] (inferred) |
Example
tsTry
import {AbiParametersToPrimitiveTypes } from 'abitype'typeResult =AbiParametersToPrimitiveTypes <[{name : 'to';type : 'address'; },{name : 'tokenId';type : 'uint256'; },]>
tsTry
import {AbiParametersToPrimitiveTypes } from 'abitype'typeResult =AbiParametersToPrimitiveTypes <[{name : 'to';type : 'address'; },{name : 'tokenId';type : 'uint256'; },]>
AbiTypeToPrimitiveType
Converts AbiType
to corresponding TypeScript primitive type.
Name | Description | Type |
---|---|---|
TAbiType | ABI type to convert to TypeScript representation. | AbiType |
TAbiParameterKind | Kind to narrow by parameter type. | AbiParameterKind (optional) |
returns | TypeScript primitive type. | TType (inferred) |
NOTE
Does not include full array or tuple conversion. Use AbiParameterToPrimitiveType
to fully convert array and tuple types.
Example
tsTry
import {AbiTypeToPrimitiveType } from 'abitype'typeResult =AbiTypeToPrimitiveType <'address'>
tsTry
import {AbiTypeToPrimitiveType } from 'abitype'typeResult =AbiTypeToPrimitiveType <'address'>
ExtractAbiError
Extracts AbiError
with name from Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
TErrorName | Name of error. | string (inferred) |
returns | ABI Error. | AbiError |
Example
tsTry
import {ExtractAbiError } from 'abitype'constabi = [{name : 'ApprovalCallerNotOwnerNorApproved',type : 'error',inputs : [] },{name : 'ApprovalQueryForNonexistentToken',type : 'error',inputs : [] },] asconst typeResult =ExtractAbiError <typeofabi , 'ApprovalQueryForNonexistentToken'>
tsTry
import {ExtractAbiError } from 'abitype'constabi = [{name : 'ApprovalCallerNotOwnerNorApproved',type : 'error',inputs : [] },{name : 'ApprovalQueryForNonexistentToken',type : 'error',inputs : [] },] asconst typeResult =ExtractAbiError <typeofabi , 'ApprovalQueryForNonexistentToken'>
ExtractAbiErrorNames
Extracts all AbiError
names from Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
returns | ABI Error names. | string (inferred) |
Example
tsTry
import {ExtractAbiErrorNames } from 'abitype'constabi = [{name : 'ApprovalCallerNotOwnerNorApproved',type : 'error',inputs : [] },{name : 'ApprovalQueryForNonexistentToken',type : 'error',inputs : [] },] asconst typeResult =ExtractAbiErrorNames <typeofabi >
tsTry
import {ExtractAbiErrorNames } from 'abitype'constabi = [{name : 'ApprovalCallerNotOwnerNorApproved',type : 'error',inputs : [] },{name : 'ApprovalQueryForNonexistentToken',type : 'error',inputs : [] },] asconst typeResult =ExtractAbiErrorNames <typeofabi >
ExtractAbiErrors
Extracts all AbiError
types from Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
returns | ABI Errors. | AbiError (union) |
Example
tsTry
import {ExtractAbiErrors } from 'abitype'constabi = [{name : 'ApprovalCallerNotOwnerNorApproved',type : 'error',inputs : [] },{name : 'ApprovalQueryForNonexistentToken',type : 'error',inputs : [] },] asconst typeResult =ExtractAbiErrors <typeofabi >
tsTry
import {ExtractAbiErrors } from 'abitype'constabi = [{name : 'ApprovalCallerNotOwnerNorApproved',type : 'error',inputs : [] },{name : 'ApprovalQueryForNonexistentToken',type : 'error',inputs : [] },] asconst typeResult =ExtractAbiErrors <typeofabi >
ExtractAbiEvent
Extracts AbiEvent
with name from Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
TEventName | Name of event. | string (inferred) |
returns | ABI Event. | AbiEvent |
Example
tsTry
import {ExtractAbiEvent } from 'abitype'constabi = [{name : 'Approval',type : 'event',anonymous : false,inputs : [{name : 'owner',type : 'address',indexed : true },{name : 'approved',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},{name : 'Transfer',type : 'event',anonymous : false,inputs : [{name : 'from',type : 'address',indexed : true },{name : 'to',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},] asconst typeResult =ExtractAbiEvent <typeofabi , 'Transfer'>
tsTry
import {ExtractAbiEvent } from 'abitype'constabi = [{name : 'Approval',type : 'event',anonymous : false,inputs : [{name : 'owner',type : 'address',indexed : true },{name : 'approved',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},{name : 'Transfer',type : 'event',anonymous : false,inputs : [{name : 'from',type : 'address',indexed : true },{name : 'to',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},] asconst typeResult =ExtractAbiEvent <typeofabi , 'Transfer'>
ExtractAbiEventNames
Extracts all AbiEvent
names from Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
returns | ABI Error names. | string (inferred) |
Example
tsTry
import {ExtractAbiEventNames } from 'abitype'constabi = [{name : 'Approval',type : 'event',anonymous : false,inputs : [{name : 'owner',type : 'address',indexed : true },{name : 'approved',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},{name : 'Transfer',type : 'event',anonymous : false,inputs : [{name : 'from',type : 'address',indexed : true },{name : 'to',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},] asconst typeResult =ExtractAbiEventNames <typeofabi >
tsTry
import {ExtractAbiEventNames } from 'abitype'constabi = [{name : 'Approval',type : 'event',anonymous : false,inputs : [{name : 'owner',type : 'address',indexed : true },{name : 'approved',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},{name : 'Transfer',type : 'event',anonymous : false,inputs : [{name : 'from',type : 'address',indexed : true },{name : 'to',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},] asconst typeResult =ExtractAbiEventNames <typeofabi >
ExtractAbiEvents
Extracts all AbiEvent
types from Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
returns | ABI Events. | AbiEvent (union) |
Example
tsTry
import {ExtractAbiEvents } from 'abitype'constabi = [{name : 'Approval',type : 'event',anonymous : false,inputs : [{name : 'owner',type : 'address',indexed : true },{name : 'approved',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},{name : 'Transfer',type : 'event',anonymous : false,inputs : [{name : 'from',type : 'address',indexed : true },{name : 'to',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},] asconst typeResult =ExtractAbiEvents <typeofabi >
tsTry
import {ExtractAbiEvents } from 'abitype'constabi = [{name : 'Approval',type : 'event',anonymous : false,inputs : [{name : 'owner',type : 'address',indexed : true },{name : 'approved',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},{name : 'Transfer',type : 'event',anonymous : false,inputs : [{name : 'from',type : 'address',indexed : true },{name : 'to',type : 'address',indexed : true },{name : 'tokenId',type : 'uint256',indexed : true },],},] asconst typeResult =ExtractAbiEvents <typeofabi >
ExtractAbiFunction
Extracts AbiFunction
with name from Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
TFunctionName | Name of function. | string (inferred) |
TAbiStateMutability | ABI state mutability. | AbiStateMutability (optional) |
returns | ABI Function. | AbiFunction |
Example
tsTry
import {ExtractAbiFunction } from 'abitype'constabi = [{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{name : 'owner',type : 'address' }],outputs : [{name : 'balance',type : 'uint256' }],},{name : 'safeTransferFrom',type : 'function',stateMutability : 'nonpayable',inputs : [{name : 'from',type : 'address' },{name : 'to',type : 'address' },{name : 'tokenId',type : 'uint256' },],outputs : [],},] asconst typeResult =ExtractAbiFunction <typeofabi , 'balanceOf'>
tsTry
import {ExtractAbiFunction } from 'abitype'constabi = [{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{name : 'owner',type : 'address' }],outputs : [{name : 'balance',type : 'uint256' }],},{name : 'safeTransferFrom',type : 'function',stateMutability : 'nonpayable',inputs : [{name : 'from',type : 'address' },{name : 'to',type : 'address' },{name : 'tokenId',type : 'uint256' },],outputs : [],},] asconst typeResult =ExtractAbiFunction <typeofabi , 'balanceOf'>
ExtractAbiFunctionNames
Extracts all AbiFunction
names from Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
TAbiStateMutability | ABI state mutability. | AbiStateMutability (optional) |
returns | ABI Event names. | string (inferred) |
Example
tsTry
import {ExtractAbiFunctionNames } from 'abitype'constabi = [{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{name : 'owner',type : 'address' }],outputs : [{name : 'balance',type : 'uint256' }],},{name : 'safeTransferFrom',type : 'function',stateMutability : 'nonpayable',inputs : [{name : 'from',type : 'address' },{name : 'to',type : 'address' },{name : 'tokenId',type : 'uint256' },],outputs : [],},] asconst typeResult =ExtractAbiFunctionNames <typeofabi >
tsTry
import {ExtractAbiFunctionNames } from 'abitype'constabi = [{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{name : 'owner',type : 'address' }],outputs : [{name : 'balance',type : 'uint256' }],},{name : 'safeTransferFrom',type : 'function',stateMutability : 'nonpayable',inputs : [{name : 'from',type : 'address' },{name : 'to',type : 'address' },{name : 'tokenId',type : 'uint256' },],outputs : [],},] asconst typeResult =ExtractAbiFunctionNames <typeofabi >
ExtractAbiFunctions
Extracts all AbiFunction
types from Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
returns | ABI Functions. | AbiFunction (union) |
Example
tsTry
import {ExtractAbiFunctions } from 'abitype'constabi = [{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{name : 'owner',type : 'address' }],outputs : [{name : 'balance',type : 'uint256' }],},{name : 'safeTransferFrom',type : 'function',stateMutability : 'nonpayable',inputs : [{name : 'from',type : 'address' },{name : 'to',type : 'address' },{name : 'tokenId',type : 'uint256' },],outputs : [],},] asconst typeResult =ExtractAbiFunctions <typeofabi >
tsTry
import {ExtractAbiFunctions } from 'abitype'constabi = [{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{name : 'owner',type : 'address' }],outputs : [{name : 'balance',type : 'uint256' }],},{name : 'safeTransferFrom',type : 'function',stateMutability : 'nonpayable',inputs : [{name : 'from',type : 'address' },{name : 'to',type : 'address' },{name : 'tokenId',type : 'uint256' },],outputs : [],},] asconst typeResult =ExtractAbiFunctions <typeofabi >
By default, extracts all functions, but you can also filter by AbiStateMutability
:
type Result = ExtractAbiFunctions<typeof erc721Abi, 'view'>
type Result = ExtractAbiFunctions<typeof erc721Abi, 'view'>
IsAbi
Checks if type is Abi
.
Name | Description | Type |
---|---|---|
TAbi | ABI. | Abi |
returns | Boolean value. true if valid Abi , false if not. | boolean |
Example
tsTry
import {IsAbi } from 'abitype'constabi = [{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{name : 'owner',type : 'address' }],outputs : [{name : 'balance',type : 'uint256' }],},{name : 'safeTransferFrom',type : 'function',stateMutability : 'nonpayable',inputs : [{name : 'from',type : 'address' },{name : 'to',type : 'address' },{name : 'tokenId',type : 'uint256' },],outputs : [],},] asconst typeResult =IsAbi <typeofabi >
tsTry
import {IsAbi } from 'abitype'constabi = [{name : 'balanceOf',type : 'function',stateMutability : 'view',inputs : [{name : 'owner',type : 'address' }],outputs : [{name : 'balance',type : 'uint256' }],},{name : 'safeTransferFrom',type : 'function',stateMutability : 'nonpayable',inputs : [{name : 'from',type : 'address' },{name : 'to',type : 'address' },{name : 'tokenId',type : 'uint256' },],outputs : [],},] asconst typeResult =IsAbi <typeofabi >
IsTypedData
Checks if type is TypedData
.
Name | Description | Type |
---|---|---|
TTypedData | EIP-712 Typed Data schema. | TypedData |
returns | Boolean value. true if valid TypedData , false if not. | boolean |
Example
tsTry
import {IsTypedData } from 'abitype'consttypes = {Person : [{name : 'name',type : 'string' },{name : 'wallet',type : 'address' },],{name : 'from',type : 'Person' },{name : 'to',type : 'Person' },{name : 'contents',type : 'string' },],} asconst typeResult =IsTypedData <typeoftypes >
tsTry
import {IsTypedData } from 'abitype'consttypes = {Person : [{name : 'name',type : 'string' },{name : 'wallet',type : 'address' },],{name : 'from',type : 'Person' },{name : 'to',type : 'Person' },{name : 'contents',type : 'string' },],} asconst typeResult =IsTypedData <typeoftypes >
TypedDataToPrimitiveTypes
Converts EIP-712 TypedData
to corresponding TypeScript primitive type.
Name | Description | Type |
---|---|---|
TTypedData | EIP-712 Typed Data schema. | TypedData |
returns | TypeScript representation of schema. | { [name: string]: TType } (inferred) |
Example
tsTry
import {TypedDataToPrimitiveTypes } from 'abitype'consttypes = {Person : [{name : 'name',type : 'string' },{name : 'wallet',type : 'address' },],{name : 'from',type : 'Person' },{name : 'to',type : 'Person' },{name : 'contents',type : 'string' },],} asconst typeResult =TypedDataToPrimitiveTypes <typeoftypes >
tsTry
import {TypedDataToPrimitiveTypes } from 'abitype'consttypes = {Person : [{name : 'name',type : 'string' },{name : 'wallet',type : 'address' },],{name : 'from',type : 'Person' },{name : 'to',type : 'Person' },{name : 'contents',type : 'string' },],} asconst typeResult =TypedDataToPrimitiveTypes <typeoftypes >