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: z.ZodReadonly<z.ZodArray<z.ZodUnion<[z.ZodObject<{ type: z.ZodLiteral<"error">; inputs: z.ZodReadonly<z.ZodArray<z.ZodType<AbiParameterType, z.ZodTypeDef, AbiParameterType>, "many">>; name: z.ZodString; }, "strip", z.ZodTypeAny, { type: "error"; name: string; inputs: readonly AbiParameterType[]; }, { type: "error"; name: string; inputs: AbiParameterType[]; }>, z.ZodObject<{ type: z.ZodLiteral<"event">; anonymous: z.ZodOptional<z.ZodBoolean>; inputs: z.ZodReadonly<z.ZodArray<z.ZodType<AbiEventParameterType, z.ZodTypeDef, AbiEventParameterType>, "many">>; name: z.ZodString; }, "strip", z.ZodTypeAny, { ...; }, { ...; }>, z.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 | undefined): Promise<Response>
fetch
(
'https://api.etherscan.io/api?module=contract&action=getabi&address=0x…' ) const
const abi: readonly ({ type: "error"; name: string; inputs: readonly AbiParameter[]; } | { type: "event"; name: string; inputs: readonly AbiEventParameter[]; anonymous?: boolean | undefined; } | ({ ...; } & ({ ...; } | ... 2 more ... | { ...; })))[]
abi
=
const Abi: z.ZodReadonly<z.ZodArray<z.ZodUnion<[z.ZodObject<{ type: z.ZodLiteral<"error">; inputs: z.ZodReadonly<z.ZodArray<z.ZodType<AbiParameterType, z.ZodTypeDef, AbiParameterType>, "many">>; name: z.ZodString; }, "strip", z.ZodTypeAny, { type: "error"; name: string; inputs: readonly AbiParameterType[]; }, { type: "error"; name: string; inputs: AbiParameterType[]; }>, z.ZodObject<{ type: z.ZodLiteral<"event">; anonymous: z.ZodOptional<z.ZodBoolean>; inputs: z.ZodReadonly<z.ZodArray<z.ZodType<AbiEventParameterType, z.ZodTypeDef, AbiEventParameterType>, "many">>; name: z.ZodString; }, "strip", z.ZodTypeAny, { ...; }, { ...; }>, z.ZodEffects<...>]>, "many">>

Zod Schema for Contract ABI Specification

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

Schemas

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

Zod Schema for Contract ABI Specification

Abi
,
const AbiConstructor: z.ZodEffects<z.ZodObject<{ type: z.ZodLiteral<"constructor">; inputs: z.ZodReadonly<z.ZodArray<z.ZodType<AbiParameterType, z.ZodTypeDef, AbiParameterType>, "many">>; payable: z.ZodOptional<z.ZodBoolean>; stateMutability: z.ZodUnion<[z.ZodLiteral<"nonpayable">, z.ZodLiteral<"payable">]>; }, "strip", z.ZodTypeAny, { type: "constructor"; inputs: readonly AbiParameterType[]; stateMutability: "nonpayable" | "payable"; payable?: boolean | undefined; }, { type: "constructor"; inputs: AbiParameterType[]; stateMutability: "nonpayable" | "payable"; payable?: boolean | undefined; }>, { ...; }, unknown>
AbiConstructor
,
const AbiEvent: z.ZodObject<{ type: z.ZodLiteral<"event">; anonymous: z.ZodOptional<z.ZodBoolean>; inputs: z.ZodReadonly<z.ZodArray<z.ZodType<AbiEventParameterType, z.ZodTypeDef, AbiEventParameterType>, "many">>; name: z.ZodString; }, "strip", z.ZodTypeAny, { type: "event"; name: string; inputs: readonly AbiEventParameterType[]; anonymous?: boolean | undefined; }, { type: "event"; name: string; inputs: AbiEventParameterType[]; anonymous?: boolean | undefined; }>
AbiEvent
,
const AbiEventParameter: z.ZodType<AbiEventParameterType, z.ZodTypeDef, AbiEventParameterType>
AbiEventParameter
,
const AbiError: z.ZodObject<{ type: z.ZodLiteral<"error">; inputs: z.ZodReadonly<z.ZodArray<z.ZodType<AbiParameterType, z.ZodTypeDef, AbiParameterType>, "many">>; name: z.ZodString; }, "strip", z.ZodTypeAny, { type: "error"; name: string; inputs: readonly AbiParameterType[]; }, { type: "error"; name: string; inputs: AbiParameterType[]; }>
AbiError
,
const AbiFallback: z.ZodEffects<z.ZodObject<{ type: z.ZodLiteral<"fallback">; inputs: z.ZodOptional<z.ZodTuple<[], null>>; payable: z.ZodOptional<z.ZodBoolean>; stateMutability: z.ZodUnion<[z.ZodLiteral<"nonpayable">, z.ZodLiteral<"payable">]>; }, "strip", z.ZodTypeAny, { type: "fallback"; stateMutability: "nonpayable" | "payable"; inputs?: [] | undefined; payable?: boolean | undefined; }, { type: "fallback"; stateMutability: "nonpayable" | "payable"; inputs?: [] | undefined; payable?: boolean | undefined; }>, { ...; }, unknown>
AbiFallback
,
const AbiFunction: z.ZodEffects<z.ZodObject<{ type: z.ZodLiteral<"function">; constant: z.ZodOptional<z.ZodBoolean>; gas: z.ZodOptional<z.ZodNumber>; inputs: z.ZodReadonly<z.ZodArray<z.ZodType<AbiParameterType, z.ZodTypeDef, AbiParameterType>, "many">>; name: z.ZodString; outputs: z.ZodReadonly<z.ZodArray<z.ZodType<AbiParameterType, z.ZodTypeDef, AbiParameterType>, "many">>; payable: z.ZodOptional<z.ZodBoolean>; stateMutability: z.ZodUnion<[z.ZodLiteral<"pure">, z.ZodLiteral<"view">, z.ZodLiteral<"nonpayable">, z.ZodLiteral<"payable">]>; }, "strip", z.ZodTypeAny, { type: "function"; name: string; inputs: readonly AbiParameterType[]; outputs: readonly AbiParameterType[]; stateMutability: "pure" | "view" | "nonpayable" | "payable"; constant?: boolean | undefined; gas?: number | undefined; payable?: boolean | undefined; }, { ...; }>, { ...; }, unknown>
AbiFunction
,
const AbiParameter: z.ZodType<AbiParameterType, z.ZodTypeDef, AbiParameterType>
AbiParameter
,
const Address: z.ZodEffects<z.ZodString, `0x${string}`, string>
Address
,
const AbiReceive: z.ZodObject<{ type: z.ZodLiteral<"receive">; stateMutability: z.ZodLiteral<"payable">; }, "strip", z.ZodTypeAny, { type: "receive"; stateMutability: "payable"; }, { type: "receive"; stateMutability: "payable"; }>
AbiReceive
,
const AbiStateMutability: z.ZodUnion<[z.ZodLiteral<"pure">, z.ZodLiteral<"view">, z.ZodLiteral<"nonpayable">, z.ZodLiteral<"payable">]>
AbiStateMutability
,
const SolidityAddress: z.ZodLiteral<"address">
SolidityAddress
,
const SolidityArray: z.ZodUnion<[z.ZodString, z.ZodString]>
SolidityArray
,
const SolidityArrayWithoutTuple: z.ZodString
SolidityArrayWithoutTuple
,
const SolidityArrayWithTuple: z.ZodString
SolidityArrayWithTuple
,
const SolidityBool: z.ZodLiteral<"bool">
SolidityBool
,
const SolidityBytes: z.ZodString
SolidityBytes
,
const SolidityFunction: z.ZodLiteral<"function">
SolidityFunction
,
const SolidityInt: z.ZodString
SolidityInt
,
const SolidityString: z.ZodLiteral<"string">
SolidityString
,
const SolidityTuple: z.ZodLiteral<"tuple">
SolidityTuple
,
const TypedData: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodObject<{ name: z.ZodString; type: z.ZodString; }, "strip", z.ZodTypeAny, { type: string; name: string; }, { type: string; name: 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 ...
TypedData
,
const TypedDataDomain: z.ZodObject<{ chainId: z.ZodOptional<z.ZodNumber>; name: z.ZodOptional<z.ZodString>; salt: z.ZodOptional<z.ZodString>; verifyingContract: z.ZodOptional<z.ZodEffects<z.ZodString, `0x${string}`, string>>; version: z.ZodOptional<z.ZodString>; }, "strip", z.ZodTypeAny, { chainId?: number | undefined; name?: string | undefined; salt?: string | undefined; verifyingContract?: `0x${string}` | undefined; version?: string | undefined; }, { chainId?: number | undefined; name?: string | undefined; salt?: string | undefined; verifyingContract?: string | undefined; version?: string | undefined; }>
TypedDataDomain
,
const TypedDataParameter: z.ZodObject<{ name: z.ZodString; type: z.ZodString; }, "strip", z.ZodTypeAny, { type: string; name: string; }, { type: string; name: string; }>
TypedDataParameter
,
const TypedDataType: z.ZodUnion<[z.ZodLiteral<"address">, z.ZodLiteral<"bool">, z.ZodString, z.ZodLiteral<"string">, z.ZodString, z.ZodUnion<[z.ZodString, z.ZodString]>]>
TypedDataType
,
} from 'abitype/zod'