abi-to-zod
v0.2.0
Published
Generate zod schemas from Ethereum ABIs
Downloads
403
Maintainers
Readme
abi-to-zod
Generate Zod schemas from Ethereum ABIs.
Install
npm install abi-to-zodUsage
buildSchemas
Build runtime Zod schemas for an ABI's function inputs.
import { buildSchemas } from 'abi-to-zod';
const abi = [
{
type: 'function',
name: 'transfer',
inputs: [
{ name: 'to', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [{ type: 'bool' }],
stateMutability: 'nonpayable',
},
] as const;
const schemas = buildSchemas(abi);
schemas.transfer.parse(['0x0000000000000000000000000000000000000000', '1']);renderSchemas
Render the schemas as a TypeScript source file.
import { renderSchemas } from 'abi-to-zod';
import { writeFileSync } from 'node:fs';
import { abi } from './my-abi.js';
writeFileSync('schemas.ts', renderSchemas(abi));CLI
abi-to-zod ./MyContract.abi.json ./schemas.tsWithout an output path, the source is written to stdout.
