CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-typechain

TypeScript bindings generator for Ethereum smart contracts that transforms ABI files into type-safe interfaces and classes

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

TypeChain

TypeChain is a TypeScript bindings generator for Ethereum smart contracts. It transforms ABI files into type-safe TypeScript interfaces, classes, and types, providing compile-time type checking and IntelliSense support for smart contract interactions.

Package Information

  • Package Name: typechain
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install typechain

Core Imports

import { runTypeChain, Config, PublicConfig, TypeChainTarget } from "typechain";

For CommonJS:

const { runTypeChain } = require("typechain");

Basic Usage

import { runTypeChain } from "typechain";

// Generate TypeScript bindings from ABI files
const config = {
  cwd: process.cwd(),
  filesToProcess: ["./artifacts/**/*.json"],
  allFiles: ["./artifacts/**/*.json"],
  target: "ethers-v5",
  outDir: "./typechain-types"
};

const result = await runTypeChain(config);
console.log(`Generated ${result.filesGenerated} files`);

Architecture

TypeChain is built around several key components:

  • Core Runner: runTypeChain function orchestrates the entire generation process
  • ABI Parser: Converts raw ABI JSON into structured Contract objects with full type information
  • Type System: Rich EVM type system with discriminated unions for all Solidity types
  • Target System: Pluggable architecture supporting multiple target generators (ethers-v4, ethers-v5, truffle, web3)
  • Code Generation: Template-based code generation with barrel files and syntax utilities
  • File Processing: Utilities for path resolution, globbing, and file system operations
  • CLI Interface: Complete command-line interface with extensive configuration options

Capabilities

Core API

Main entry point for programmatic usage of TypeChain with configuration management and target resolution.

function runTypeChain(config: PublicConfig): Promise<{ filesGenerated: number }>;

interface PublicConfig {
  cwd: string;
  target: string;
  outDir?: string;
  prettier?: object;
  filesToProcess: string[];
  allFiles: string[];
  inputDir?: string;
  flags?: CodegenConfig;
}

Core API

ABI Parsing

Complete ABI parsing system that converts raw JSON ABI files into structured TypeScript representations with full type safety.

function parse(abi: RawAbiDefinition[], path: string, documentation?: DocumentationResult): Contract;
function extractAbi(rawJson: string): RawAbiDefinition[];
function extractBytecode(rawContents: string): BytecodeWithLinkReferences | undefined;

interface Contract {
  name: string;
  rawName: string;
  path: string[];
  fallback?: FunctionWithoutInputDeclaration;
  constructor: FunctionWithoutOutputDeclaration[];
  functions: Dictionary<FunctionDeclaration[]>;
  events: Dictionary<EventDeclaration[]>;
  structs: Dictionary<StructType[]>;
  documentation?: object;
}

ABI Parsing

Code Generation

Tools for generating TypeScript code including barrel files, imports, and syntax utilities.

function createBarrelFiles(
  paths: string[], 
  options: { typeOnly: boolean; postfix?: string; moduleSuffix?: string }
): FileDescription[];

interface FileDescription {
  path: string;
  contents: string;
}

Code Generation

CLI Interface

Command-line interface for running TypeChain with extensive configuration options and file pattern matching.

interface ParsedArgs {
  files: string[];
  target: string;
  outDir?: string;
  inputDir?: string;
  flags: {
    discriminateTypes: boolean;
    alwaysGenerateOverloads: boolean;
    tsNocheck: boolean;
    node16Modules: boolean;
  };
}

function parseArgs(): ParsedArgs;

CLI Interface

Types and Interfaces

Rich type system for representing EVM types, function signatures, and contract structures with full TypeScript integration.

type EvmType = BooleanType | IntegerType | UnsignedIntegerType | StringType | 
               BytesType | DynamicBytesType | AddressType | ArrayType | TupleType | UnknownType;

type EvmOutputType = EvmType | VoidType;

interface FunctionDeclaration {
  name: string;
  stateMutability: StateMutability;
  inputs: AbiParameter[];
  outputs: AbiOutputParameter[];
  documentation?: FunctionDocumentation;
}

Types and Interfaces

File Utilities

Comprehensive file system utilities for path manipulation, globbing, and cross-platform compatibility.

function glob(cwd: string, patternsOrFiles: string[], ignoreNodeModules?: boolean): string[];
function detectInputsRoot(allFiles: string[]): string;
function normalizeName(rawName: string): string;

File Utilities

Signature Utilities

Functions for generating contract function and event signatures for ABI compatibility and tooling.

function getSignatureForFn(fn: FunctionDeclaration): string;
function getFullSignatureForEvent(event: EventDeclaration): string;
function getFullSignatureAsSymbolForEvent(event: EventDeclaration): string;

docs

abi-parsing.md

cli-interface.md

code-generation.md

core-api.md

file-utilities.md

index.md

types-interfaces.md

tile.json