Skip to content

Zod

ABIType exports the core types as Zod schemas from the 'abitype/zod' entrypoint.

Install

Install the Zod peer dependency:

pnpm add zod

Usage

Import and use schemas:

import { 
const Abi: ZodReadonly<ZodArray<ZodUnion<[ZodObject<{ type: ZodLiteral<"error">; inputs: ZodReadonly<ZodArray<ZodType<AbiParameter, ZodTypeDef, AbiParameter>, "many">>; name: ZodString; }, "strip", ZodTypeAny, { inputs: readonly AbiParameter[]; name: string; type: "error"; }, { inputs: readonly AbiParameter[]; name: string; type: "error"; }>, ZodObject<...>, ZodEffects<...>]>, "many">>

Zod Schema for Contract ABI Specification

Abi
} from 'abitype/zod'
const
const result: Response
result
= await
function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>
fetch
(
'https://api.etherscan.io/api?module=contract&action=getabi&address=0x…' ) const
const abi: readonly ({ inputs: readonly AbiParameter[]; name: string; type: "error"; } | { inputs: readonly AbiEventParameter[]; name: string; type: "event"; anonymous?: boolean | undefined; } | ({ payable?: boolean | undefined; gas?: number | undefined; constant?: boolean | undefined; } & ({ inputs: readonly AbiParameter[]; outputs: readonly AbiParameter[]; name: string; type: "function"; stateMutability: "pure" | "view" | "nonpayable" | "payable"; } | { ...; } | { ...; } | { ...; })))[]
abi
=
const Abi: ZodReadonly<ZodArray<ZodUnion<[ZodObject<{ type: ZodLiteral<"error">; inputs: ZodReadonly<ZodArray<ZodType<AbiParameter, ZodTypeDef, AbiParameter>, "many">>; name: ZodString; }, "strip", ZodTypeAny, { inputs: readonly AbiParameter[]; name: string; type: "error"; }, { inputs: readonly AbiParameter[]; name: string; type: "error"; }>, ZodObject<...>, ZodEffects<...>]>, "many">>

Zod Schema for Contract ABI Specification

Abi
.
ZodType<readonly ({ inputs: readonly AbiParameter[]; name: string; type: "error"; } | { inputs: readonly AbiEventParameter[]; name: string; type: "event"; anonymous?: boolean | undefined; } | ({ ...; } & ({ ...; } | ... 2 more ... | { ...; })))[], ZodReadonlyDef<...>, readonly unknown[]>.parse(data: unknown, params?: Partial<ParseParams>): readonly ({ inputs: readonly AbiParameter[]; name: string; type: "error"; } | { inputs: readonly AbiEventParameter[]; name: string; type: "event"; anonymous?: boolean | undefined; } | ({ payable?: boolean | undefined; gas?: number | undefined; constant?: boolean | undefined; } & ({ inputs: readonly AbiParameter[]; outputs: readonly AbiParameter[]; name: string; type: "function"; stateMutability: "pure" | "view" | "nonpayable" | "payable"; } | { ...; } | { ...; } | { ...; })))[]
parse
(
const result: Response
result
)

Schemas

import {
  
const Abi: ZodReadonly<ZodArray<ZodUnion<[ZodObject<{ type: ZodLiteral<"error">; inputs: ZodReadonly<ZodArray<ZodType<AbiParameter, ZodTypeDef, AbiParameter>, "many">>; name: ZodString; }, "strip", ZodTypeAny, { inputs: readonly AbiParameter[]; name: string; type: "error"; }, { inputs: readonly AbiParameter[]; name: string; type: "error"; }>, ZodObject<...>, ZodEffects<...>]>, "many">>

Zod Schema for Contract ABI Specification

Abi
,
const AbiConstructor: ZodEffects<ZodObject<{ type: ZodLiteral<"constructor">; inputs: ZodReadonly<ZodArray<ZodType<AbiParameter, ZodTypeDef, AbiParameter>, "many">>; payable: ZodOptional<ZodBoolean>; stateMutability: ZodUnion<[ZodLiteral<"nonpayable">, ZodLiteral<"payable">]>; }, "strip", ZodTypeAny, { ...; }, { ...; }>, { ...; }, unknown>
AbiConstructor
,
const AbiEvent: ZodObject<{ type: ZodLiteral<"event">; anonymous: ZodOptional<ZodBoolean>; inputs: ZodReadonly<ZodArray<ZodType<AbiEventParameter, ZodTypeDef, AbiEventParameter>, "many">>; name: ZodString; }, "strip", ZodTypeAny, { inputs: readonly AbiEventParameter[]; name: string; type: "event"; anonymous?: boolean | undefined; }, { ...; }>
AbiEvent
,
const AbiEventParameter: ZodType<AbiEventParameter, ZodTypeDef, AbiEventParameter>
AbiEventParameter
,
const AbiError: ZodObject<{ type: ZodLiteral<"error">; inputs: ZodReadonly<ZodArray<ZodType<AbiParameter, ZodTypeDef, AbiParameter>, "many">>; name: ZodString; }, "strip", ZodTypeAny, { inputs: readonly AbiParameter[]; name: string; type: "error"; }, { inputs: readonly AbiParameter[]; name: string; type: "error"; }>
AbiError
,
const AbiFallback: ZodEffects<ZodObject<{ type: ZodLiteral<"fallback">; payable: ZodOptional<ZodBoolean>; stateMutability: ZodUnion<[ZodLiteral<"nonpayable">, ZodLiteral<"payable">]>; }, "strip", ZodTypeAny, { type: "fallback"; stateMutability: "nonpayable" | "payable"; payable?: boolean | undefined; }, { type: "fallback"; stateMutability: "nonpayable" | "payable"; payable?: boolean | undefined; }>, { ...; }, unknown>
AbiFallback
,
const AbiFunction: ZodEffects<ZodObject<{ type: ZodLiteral<"function">; constant: ZodOptional<ZodBoolean>; gas: ZodOptional<ZodNumber>; inputs: ZodReadonly<ZodArray<ZodType<AbiParameter, ZodTypeDef, AbiParameter>, "many">>; name: ZodString; outputs: ZodReadonly<ZodArray<ZodType<AbiParameter, ZodTypeDef, AbiParameter>, "many">>; payable: ZodOptional<ZodBoolean>; stateMutability: ZodUnion<[ZodLiteral<"pure">, ZodLiteral<"view">, ZodLiteral<"nonpayable">, ZodLiteral<"payable">]>; }, "strip", ZodTypeAny, { ...; }, { ...; }>, { ...; }, unknown>
AbiFunction
,
const AbiParameter: ZodType<AbiParameter, ZodTypeDef, AbiParameter>
AbiParameter
,
const Address: ZodEffects<ZodString, `0x${string}`, string>
Address
,
const AbiReceive: ZodObject<{ type: ZodLiteral<"receive">; stateMutability: ZodLiteral<"payable">; }, "strip", ZodTypeAny, { type: "receive"; stateMutability: "payable"; }, { type: "receive"; stateMutability: "payable"; }>
AbiReceive
,
const AbiStateMutability: ZodUnion<[ZodLiteral<"pure">, ZodLiteral<"view">, ZodLiteral<"nonpayable">, ZodLiteral<"payable">]>
AbiStateMutability
,
const SolidityAddress: ZodLiteral<"address">
SolidityAddress
,
const SolidityArray: ZodUnion<[ZodString, ZodString]>
SolidityArray
,
const SolidityArrayWithoutTuple: ZodString
SolidityArrayWithoutTuple
,
const SolidityArrayWithTuple: ZodString
SolidityArrayWithTuple
,
const SolidityBool: ZodLiteral<"bool">
SolidityBool
,
const SolidityBytes: ZodString
SolidityBytes
,
const SolidityFunction: ZodLiteral<"function">
SolidityFunction
,
const SolidityInt: ZodString
SolidityInt
,
const SolidityString: ZodLiteral<"string">
SolidityString
,
const SolidityTuple: ZodLiteral<"tuple">
SolidityTuple
,
const TypedData: ZodEffects<ZodRecord<ZodString, ZodArray<ZodObject<{ name: ZodString; type: ZodString; }, "strip", ZodTypeAny, { name: string; type: string; }, { name: string; type: string; }>, "many">>, { [x: string]: readonly TypedDataParameter[]; [x: `string[${string}]`]: undefined; [x: `function[${string}]`]: undefined; [x: `bytes[${string}]`]: undefined; [x: `bytes1[${string}]`]: undefined; [x: `bytes2[${string}]`]: undefined; [x: `bytes3[${string}]`]: undefined; [x: `bytes4[${string}]`]: undefined; [x: `bytes5[${string}]`]: undefined; [x: `bytes6[${string}]`]: undefined; [x: `bytes7[${string}]`]: undefined; [x: `bytes8[${string}]`]: undefined; [x: `bytes9[${string}]`]: undefined; [x: `bytes10[${string}]`]: undefined; [x: `bytes11[${string}]`]: undefined; [x: `bytes12[${string}]`]: undefined; [x: `bytes13[${string}]`]: undefined; [x: `bytes14[${string}]`]: undefined; [x: `bytes15[${string}]`]: undefined; [x: `bytes16[${string}]`]: undefined; [x: `bytes17[${string}]`]: undefined; [x: `bytes18[${string}]`]: undefined; [x: `bytes19[${string}]`]: undefined; [x: `bytes20[${string}]`]: undefined; [x: `bytes21[${string}]`]: undefined; [x: `bytes22[${string}]`]: undefined; [x: `bytes23[${string}]`]: undefined; [x: `bytes24[${string}]`]: undefined; [x: `bytes25[${string}]`]: undefined; [x: `bytes26[${string}]`]: undefined; [x: `bytes27[${string}]`]: undefined; [x: `bytes28[${string}]`]: undefined; [x: `bytes29[${string}]`]: undefined; [x: `bytes30[${string}]`]: undefined; [x: `bytes31[${string}]`]: undefined; [x: `bytes32[${string}]`]: undefined; [x: `int[${string}]`]: undefined; [x: `int8[${string}]`]: undefined; [x: `int16[${string}]`]: undefined; [x: `int24[${string}]`]: undefined; [x: `int32[${string}]`]: undefined; [x: `int40[${string}]`]: undefined; [x: `int48[${string}]`]: undefined; [x: `int56[${string}]`]: undefined; [x: `int64[${string}]`]: undefined; [x: `int72[${string}]`]: undefined; [x: `int80[${string}]`]: undefined; [x: `int88[${string}]`]: undefined; [x: `int96[${string}]`]: undefined; [x: `int104[${string}]`]: undefined; [x: `int112[${string}]`]: undefined; [x: `int120[${string}]`]: undefined; [x: `int128[${string}]`]: undefined; [x: `int136[${string}]`]: undefined; [x: `int144[${string}]`]: undefined; [x: `int152[${string}]`]: undefined; [x: `int160[${string}]`]: undefined; [x: `int168[${string}]`]: undefined; [x: `int176[${string}]`]: undefined; [x: `int184[${string}]`]: undefined; [x: `int192[${string}]`]: undefined; [x: `int200[${string}]`]: undefined; [x: `int208[${string}]`]: undefined; [x: `int216[${string}]`]: undefined; [x: `int224[${string}]`]: undefined; [x: `int232[${string}]`]: undefined; [x: `int240[${string}]`]: undefined; [x: `int248[${string}]`]: undefined; [x: `int256[${string}]`]: undefined; [x: `uint[${string}]`]: undefined; [x: `uint8[${string}]`]: undefined; [x: `uint16[${string}]`]: undefined; [x: `uint24[${string}]`]: undefined; [x: `uint32[${string}]`]: undefined; [x: `uint40[${string}]`]: undefined; [x: `uint48[${string}]`]: undefined; [x: `uint56[${string}]`]: undefined; [x: `uint64[${string}]`]: undefined; [x: `uint72[${string}]`]: undefined; [x: `uint80[${string}]`]: undefined; [x: `uint88[${string}]`]: undefined; [x: `uint96[${string}]`]: undefined; [x: `uint104[${string}]`]: undefined; [x: `uint112[${string}]`]: undefined; [x: `uint120[${string}]`]: undefined; [x: `uint128[${string}]`]: undefined; [x: `uint136[${string}]`]: undefined; [x: `uint144[${string}]`]: undefined; [x: `uint152[${string}]`]: undefined; [x: `uint160[${string}]`]: undefined; [x: `uint168[${string}]`]: undefined; [x: `uint176[${string}]`]: undefined; [x: `uint184[${string}]`]: undefined; [x: `uint192[${string}]`]: undefined; [x: `uint200[${string}]`]: undefined; [x: `uint208[${string}]`]: undefined; [x: `uint216[${string}]`]: undefined; [x: `uint224[${string}]`]: undefined; [x: `uint232[${string}]`]: undefined; [x: `uint240[${string}]`]: undefined; [x: `uint248[${string}]`]: undefined; [x: `uint256[${string}]`]: undefined; [x: `address[${string}]`]: undefined; [x: `bool[${string}]`]: undefined; ... 99 more ...; bool?: never; }, Record<...>>
TypedData
,
const TypedDataDomain: ZodObject<{ chainId: ZodOptional<ZodUnion<[ZodNumber, ZodBigInt]>>; name: ZodOptional<ZodString>; salt: ZodOptional<ZodString>; verifyingContract: ZodOptional<ZodEffects<ZodString, `0x${string}`, string>>; version: ZodOptional<ZodString>; }, "strip", ZodTypeAny, { ...; }, { ...; }>
TypedDataDomain
,
const TypedDataParameter: ZodObject<{ name: ZodString; type: ZodString; }, "strip", ZodTypeAny, { name: string; type: string; }, { name: string; type: string; }>
TypedDataParameter
,
const TypedDataType: ZodUnion<[ZodLiteral<"address">, ZodLiteral<"bool">, ZodString, ZodLiteral<"string">, ZodString, ZodUnion<[ZodString, ZodString]>]>
TypedDataType
,
} from 'abitype/zod'