CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-typechain--ethers-v5

TypeChain target for generating TypeScript bindings compatible with ethers-v5

Pending
Overview
Eval results
Files

target-class.mddocs/

Target Class

Core TypeChain target class that orchestrates the generation of TypeScript bindings for Ethers.js v5 contracts. The Ethers class extends TypeChainTarget and handles the complete pipeline from ABI/bytecode input to generated TypeScript files.

Capabilities

Ethers Class

Main target class that processes contract files and generates typed Ethers.js v5 bindings.

/**
 * TypeChain target for generating Ethers v5 compatible TypeScript bindings
 */
export default class Ethers extends TypeChainTarget {
  readonly name: string; // Always "Ethers"
  
  /**
   * Creates new Ethers target instance
   * @param config - TypeChain configuration including output directory and file list
   */
  constructor(config: Config);
  
  /**
   * Transforms a single contract file (ABI/bytecode) into TypeScript bindings
   * @param file - File description containing path and contents
   * @returns Array of generated files or void if file cannot be processed
   */
  transformFile(file: FileDescription): FileDescription[] | void;
  
  
  /**
   * Called after all files are processed to generate additional support files
   * @returns Array of additional files (common.ts, hardhat.d.ts, barrel files)
   */
  afterRun(): FileDescription[];
}

Usage Example:

import { Config } from "typechain";
import Ethers from "@typechain/ethers-v5";

const config: Config = {
  cwd: process.cwd(),
  outDir: "./types/ethers-contracts/",
  inputDir: "./contracts/",
  allFiles: ["./contracts/ERC20.json", "./contracts/MyContract.json"],
  target: "ethers-v5",
  flags: {
    alwaysGenerateOverloads: false,
    discriminateTypes: false,
    environment: "hardhat"
  }
};

const target = new Ethers(config);

// TypeChain will call these methods automatically:
// 1. transformFile() for each contract file
// 2. afterRun() to generate support files

File Processing Flow

The target processes files in the following sequence:

  1. File Classification: Determines if file is .bin (bytecode) or .abi/.json (ABI)
  2. ABI Processing: Extracts ABI and documentation from JSON files
  3. Bytecode Matching: Pairs bytecode files with their corresponding ABI files
  4. Type Generation: Creates contract interface and type definitions
  5. Factory Generation: Creates factory classes for deployment (concrete contracts) or connection (abstract contracts)
  6. Support Files: Generates common utilities, barrel exports, and optional Hardhat integration

Generated File Structure

The target produces the following file structure:

types/ethers-contracts/
├── ContractName.ts              # Contract interface and types
├── factories/
│   └── ContractName__factory.ts # Factory class for deployment/connection
├── common.ts                    # Common utility types
├── index.ts                     # Barrel exports
└── hardhat.d.ts                # Hardhat integration (optional)

Configuration Options

The target respects these configuration flags:

  • alwaysGenerateOverloads: Generate function signature overloads even for non-overloaded functions
  • discriminateTypes: Add contractName property to contracts for type discrimination
  • environment: Set to "hardhat" to generate Hardhat-specific type definitions

Hardhat Integration

When the environment is set to "hardhat", the target automatically generates a hardhat.d.ts file that extends Hardhat's type system to include generated contract factories. This enables usage like:

// In Hardhat scripts/tests
import { ethers } from "hardhat";

const factory = await ethers.getContractFactory("MyContract");
const contract = await factory.deploy();
// Both factory and contract are fully typed

Types

// File description for generated output
interface FileDescription {
  path: string;
  contents: string;
}

// TypeChain configuration
interface Config {
  cwd: string;
  outDir?: string;
  inputDir: string;
  allFiles: string[];
  target: string;
  flags: CodegenConfig;
}

// Code generation configuration
interface CodegenConfig {
  alwaysGenerateOverloads: boolean;
  discriminateTypes: boolean;
  environment?: string;
}

// Bytecode with external library references
interface BytecodeWithLinkReferences {
  bytecode: string;
  linkReferences?: LinkReference[];
}

Install with Tessl CLI

npx tessl i tessl/npm-typechain--ethers-v5

docs

index.md

target-class.md

using-generated-types.md

tile.json