TypeChain target for generating TypeScript bindings compatible with ethers-v5
—
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.
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 filesThe target processes files in the following sequence:
.bin (bytecode) or .abi/.json (ABI)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)The target respects these configuration flags:
alwaysGenerateOverloads: Generate function signature overloads even for non-overloaded functionsdiscriminateTypes: Add contractName property to contracts for type discriminationenvironment: Set to "hardhat" to generate Hardhat-specific type definitionsWhen 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// 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