Codegen API

Programmatic API for TypeScript code generation.

Codegen API

polyq/codegen — Generate TypeScript clients programmatically.

generateFromSchema(schemaPath, outDir, config?, chain?)

Generate TypeScript client from a contract schema file. Auto-detects chain if not specified.

import { generateFromSchema } from 'polyq/codegen'

const result = generateFromSchema(
  'target/idl/my_program.json',
  'generated/',
)

Parameters:

ParamTypeDefaultDescription
schemaPathstringPath to IDL or ABI JSON file
outDirstringOutput directory for generated files
configPartial<CodegenConfig>all featuresFeature flags
chain'svm' \| 'evm'auto-detectedForce chain type

Returns: CodegenOutput

interface CodegenOutput {
  files: { path: string, content: string }[]
}

generateFromIdl(idlPath, outDir, config?)

SVM-specific alias. Calls generateFromSchema with chain: 'svm'.

import { generateFromIdl } from 'polyq/codegen'

generateFromIdl('target/idl/my_program.json', 'generated/')

CodegenConfig

interface CodegenConfig {
  outDir: string
  programs?: string[]
  features?: {
    types?: boolean         // TypeScript interfaces
    instructions?: boolean  // Instruction builders (SVM) / function types (EVM)
    accounts?: boolean      // Account fetchers with Borsh deserialization (SVM)
    pda?: boolean          // PDA derivation helpers (SVM)
    errors?: boolean       // Error enum + lookup
    events?: boolean       // Event types
  }
}

SVM Generated Files

FileContains
types.tsInterfaces for all IDL type definitions (structs + enums)
pda.tsderiveFoo() functions using PublicKey.findProgramAddressSync
instructions.tscreateFooInstruction() with full Borsh serialization via @coral-xyz/borsh
accounts.tsfetchFoo() with discriminator check + Borsh deserialization
errors.tsProgramError enum + getProgramError(code) lookup
index.tsBarrel re-export

EVM Generated Files

FileContains
contract.tsABI as const assertion + typed address map per network
types.tsFooArgs input interfaces + FooReturn output interfaces
events.tsFooEvent interfaces with indexed fields
errors.tsCustom error types (parameterized and empty)
index.tsBarrel re-export

Type Mapping

SVM (Anchor IDL → TypeScript)

IDL TypeTypeScriptBorsh Codec
boolbooleanborsh.bool()
u8, u16, u32numberborsh.u8(), etc.
u64, u128, u256bigintborsh.u64(), etc.
i8i256number / bigintborsh.i8(), etc.
stringstringborsh.str()
pubkeyPublicKeyborsh.publicKey()
bytesUint8Arrayborsh.vecU8()
vec<T>T[]borsh.vec(T)
option<T>T \| nullborsh.option(T)
defined<Name>Name (interface)NameLayout (struct)

EVM (Solidity ABI → TypeScript)

ABI TypeTypeScript
address`0x${string}`
boolboolean
stringstring
uint* / int*bigint
bytes / bytes32`0x${string}`
T[]T[]
tuple{ field: Type }