Skip to content

ssghost/abitype

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

67 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

ABIType

npm Downloads per month

Strict TypeScript types for Ethereum ABIs. ABIType provides utilities and type definitions for ABI properties and values, covering the Contract ABI Specification, as well as EIP-712 Typed Data.

import { ExtractAbiFunctions } from 'abitype'

const erc721Abi = [...] as const

type Result = ExtractAbiFunctions<typeof erc721Abi, 'payable'>

Works great for adding blazing fast autocomplete and type checking to functions, variables, or your own types (see examples). No need to generate types with third-party tools โ€“ just use your ABI and let TypeScript do the rest!

abitype.mp4

Installation

npm install abitype

Usage

Since ABIs can contain deeply nested arrays and objects, you must assert your ABIs to constants using const assertions. This allows TypeScript to take the most specific types for expressions and avoid type widening (e.g. no going from "hello" to string).

const erc721Abi = [...] as const
const erc721Abi = <const>[...]

Utilities

AbiParameterToPrimitiveType

Converts AbiParameter to corresponding TypeScript primitive type.

import { AbiParameterToPrimitiveType } from 'abitype'

type Result = AbiParameterToPrimitiveType<{
  name: 'owner'
  type: 'address'
}>

AbiParametersToPrimitiveTypes

Converts array of AbiParameter to corresponding TypeScript primitive types.

import { AbiParametersToPrimitiveTypes } from 'abitype'

type Result = AbiParametersToPrimitiveTypes<
  [
    {
      name: 'to'
      type: 'address'
    },
    {
      name: 'tokenId'
      type: 'uint256'
    },
  ]
>

AbiTypeToPrimitiveType

Converts AbiType to corresponding TypeScript primitive type.

import { AbiParametersToPrimitiveTypes } from 'abitype'

type Result = AbiTypeToPrimitiveType<'address'>

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

ExtractAbiError

Extracts all AbiError types from Abi

import { ExtractAbiError } from 'abitype'

type Result = ExtractAbiError<typeof erc721Abi, 'SomeError'>

ExtractAbiErrorNames

Extracts all AbiError names from Abi

import { ExtractAbiErrorNames } from 'abitype'

type Result = ExtractAbiErrorNames<typeof erc721Abi>

ExtractAbiErrors

Extracts all AbiError types from Abi

import { ExtractAbiErrors } from 'abitype'

type Result = ExtractAbiErrors<typeof erc721Abi>

ExtractAbiEvent

Extracts AbiEvent with name from Abi

import { ExtractAbiEvent } from 'abitype'

type Result = ExtractAbiEvent<typeof erc721Abi, 'Transfer'>

ExtractAbiEventNames

Extracts all AbiEvent names from Abi

import { ExtractAbiEventNames } from 'abitype'

type Result = ExtractAbiEventNames<typeof erc721Abi>

ExtractAbiEvents

Extracts all AbiEvent types from Abi

import { ExtractAbiEvents } from 'abitype'

type Result = ExtractAbiEvents<typeof erc721Abi>

ExtractAbiFunction

Extracts AbiFunction with name from Abi

import { AbiFunction } from 'abitype'

type Result = ExtractAbiFunction<typeof erc721Abi, 'balanceOf'>

ExtractAbiFunctionNames

Extracts all AbiFunction names from Abi

import { ExtractAbiFunctionNames } from 'abitype'

type Result = ExtractAbiFunctionNames<typeof erc721Abi>

ExtractAbiFunctions

Extracts all AbiFunction types from Abi

import { ExtractAbiFunctions } from 'abitype'

type Result = ExtractAbiFunctions<typeof erc721Abi>

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

type Result = ExtractAbiFunctions<typeof erc721Abi, 'view'>

IsAbi

Checks if type is Abi

import { IsAbi } from 'abitype'

type Result = IsAbi<typeof erc721Abi>

IsTypedData

Checks if type is TypedData

import { IsTypedData } from 'abitype'

type Result = IsTypedData<{
  Person: [
    { name: 'name'; type: 'string' },
    { name: 'wallet'; type: 'address' },
  ]
  Mail: [
    { name: 'from'; type: 'Person' },
    { name: 'to'; type: 'Person' },
    { name: 'contents'; type: 'string' },
  ]
}>

TypedDataToPrimitiveTypes

Converts EIP-712 TypedData to corresponding TypeScript primitive type.

import { TypedDataToPrimitiveTypes } from 'abitype'

type Result = TypedDataToPrimitiveTypes<{
  Person: [
    { name: 'name'; type: 'string' },
    { name: 'wallet'; type: 'address' },
  ]
  Mail: [
    { name: 'from'; type: 'Person' },
    { name: 'to'; type: 'Person' },
    { name: 'contents'; type: 'string' },
  ]
}>

Types

Abi

Type matching the Contract ABI Specification

import { Abi } from 'abitype'

AbiError

ABI Error type

import { AbiError } from 'abitype'

AbiEvent

ABI Event type

import { AbiEvent } from 'abitype'

AbiFunction

ABI Function type

import { AbiFunction } from 'abitype'

AbiInternalType

Representation used by Solidity compiler (e.g. 'string', 'int256', 'struct Foo')

import { AbiInternalType } from 'abitype'

AbiParameter

inputs and ouputs item for ABI functions, events, and errors

import { AbiParameter } from 'abitype'

AbiParameterType

Type of ABI parameter: 'inputs' | 'outputs'

import { AbiParameterType } from 'abitype'

AbiStateMutability

ABI Function behavior

import { AbiStateMutability } from 'abitype'

AbiType

ABI canonical types

import { AbiType } from 'abitype'

Solidity types

Solidity types as template strings

import {
  SolidityAddress,
  SolidityArray,
  SolidityBool,
  SolidityBytes,
  SolidityFunction,
  SolidityInt,
  SolidityString,
  SolidityTuple,
} from 'abitype'

TypedDataParameter

Entry in TypedData type items

import { TypedDataParameter } from 'abitype'

TypedDataType

Subset of AbiType that excludes tuple and function

import { TypedDataType } from 'abitype'

TypedData

EIP-712 Typed Data Specification

import { TypedData } from 'abitype'

Configuration

ABIType tries to strike a balance between type exhaustiveness and speed with sensible defaults. In some cases, you might want to tune your configuration (e.g. fixed array length). To do this, the following configuration options are available:

Option Type Default Description
AddressType any `0x${string}` TypeScript type to use for address values.
ArrayMaxDepth number | false 2 Maximum depth for nested array types (e.g. string[][]). When false, there is no maximum array depth.
BigIntType any bigint TypeScript type to use for int<M> and uint<M> values, where M > 48.
BytesType any string | ArrayLike<number> TypeScript type to use for bytes<M> values.
FixedArrayMinLength number 1 Lower bound for fixed-length arrays
FixedArrayMaxLength number 5 Upper bound for fixed-length arrays
IntType any number TypeScript type to use for int<M> and uint<M> values, where M <= 48.

Configuration options are customizable using declaration merging. Just extend the Config interface either directly in your code or in a d.ts file (e.g. abi.d.ts):

declare module 'abitype' {
  export interface Config {
    FixedArrayMaxLength: 6
  }
}

Warning When configuring ArrayMaxDepth, FixedArrayMinLength, and FixedArrayMaxLength, there are trade-offs. For example, choosing large numbers for ArrayMaxDepth and increasing the range between FixedArrayMinLength and FixedArrayMaxLength will make your types more exhaustive, but will also slow down the compiler for type checking, autocomplete, etc.

Support

If you find ABIType useful, please consider supporting development. Thank you ๐Ÿ™

Contributing

If you're interested in contributing, please read the contributing docs before submitting a pull request.

Authors

License

WAGMIT License

About

Strict TypeScript types for Ethereum ABIs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%