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, { ...; }, { ...; }>, 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 ({ ...; } | { ...; } | ({ ...; } & ({ ...; } | ... 2 more ... | { ...; })))[]
abi
=
const Abi: ZodReadonly<ZodArray<ZodUnion<[ZodObject<{ type: ZodLiteral<"error">; inputs: ZodReadonly<ZodArray<ZodType<AbiParameter, ZodTypeDef, AbiParameter>, "many">>; name: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>, ZodObject<...>, ZodEffects<...>]>, "many">>

Zod Schema for Contract ABI Specification

Abi
.
ZodType<readonly ({ inputs: readonly AbiParameter[]; type: "error"; name: string; } | { inputs: readonly AbiEventParameter[]; type: "event"; name: string; anonymous?: boolean | undefined; } | ({ ...; } & ({ ...; } | ... 2 more ... | { ...; })))[], ZodReadonlyDef<...>, readonly unknown[]>.parse(data: unknown, params?: Partial<ParseParams>): readonly ({ inputs: readonly AbiParameter[]; type: "error"; name: string; } | { ...; } | ({ ...; } & ({ ...; } | ... 2 more ... | { ...; })))[]
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, { ...; }, { ...; }>, 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, { ...; }, { ...; }>
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, { ...; }, { ...; }>
AbiError
,
const AbiFallback: ZodEffects<ZodObject<{ type: ZodLiteral<"fallback">; payable: ZodOptional<ZodBoolean>; stateMutability: ZodUnion<[ZodLiteral<"nonpayable">, ZodLiteral<"payable">]>; }, "strip", ZodTypeAny, { ...; }, { ...; }>, { ...; }, unknown>
AbiFallback
,
const AbiFunction: ZodEffects<ZodObject<{ type: ZodLiteral<"function">; constant: ZodOptional<ZodBoolean>; gas: ZodOptional<ZodNumber>; ... 4 more ...; 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"; }, { ...; }>
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, { type: string; name: string; }, { ...; }>, "many">>, { ...; }, Record<...>>
TypedData
,
const TypedDataDomain: ZodObject<{ chainId: ZodOptional<ZodNumber>; 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, { type: string; name: string; }, { type: string; name: string; }>
TypedDataParameter
,
const TypedDataType: ZodUnion<[ZodLiteral<"address">, ZodLiteral<"bool">, ZodString, ZodLiteral<"string">, ZodString, ZodUnion<[ZodString, ZodString]>]>
TypedDataType
,
} from 'abitype/zod'