TypeChain target for generating TypeScript bindings compatible with ethers-v5
npx @tessl/cli install tessl/npm-typechain--ethers-v5@10.2.0@typechain/ethers-v5 is a TypeChain target that generates TypeScript bindings compatible with Ethers.js v5. It transforms smart contract ABIs and bytecode into type-safe TypeScript interfaces, providing compile-time verification for Ethereum smart contract interactions.
npm install --save-dev @typechain/ethers-v5import Ethers from "@typechain/ethers-v5";For CommonJS:
const Ethers = require("@typechain/ethers-v5").default;Configure this target in your TypeChain setup:
# CLI usage
typechain --target ethers-v5 --out-dir ./types/ethers-contracts/ './contracts/**/*.json'With TypeChain configuration file:
import { TypeChainConfig } from 'typechain';
const config: TypeChainConfig = {
target: 'ethers-v5',
outDir: './types/ethers-contracts/',
inputDir: './contracts/',
allFiles: ['./contracts/**/*.json'],
flags: {
alwaysGenerateOverloads: false,
discriminateTypes: false,
environment: 'hardhat'
}
};Once TypeChain generates the bindings, use them with ethers.js:
import { ethers } from 'ethers';
import { MyContract, MyContract__factory } from './types/ethers-contracts';
// Connect to a deployed contract
const provider = new ethers.providers.JsonRpcProvider();
const signer = provider.getSigner();
const contract: MyContract = MyContract__factory.connect(contractAddress, signer);
// Call contract methods with full type safety
const result = await contract.myMethod(param1, param2);@typechain/ethers-v5 is built around several key components:
Ethers class extending TypeChainTarget, orchestrates the generation processThe main TypeChain target class that orchestrates the generation of TypeScript bindings for Ethers.js v5 contracts. This is the only public export of the package.
export default class Ethers extends TypeChainTarget {
readonly name: string; // Always "Ethers"
/**
* Creates new Ethers target instance with TypeChain configuration
* @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 supporting files
* @returns Array of additional files (common.ts, index.ts, hardhat.d.ts)
*/
afterRun(): FileDescription[];
}The target generates several types of files for type-safe contract interaction:
// Example generated contract interface
export interface MyContract extends BaseContract {
connect(signerOrProvider: Signer | Provider | string): this;
attach(addressOrName: string): this;
deployed(): Promise<this>;
// Type-safe contract methods
myMethod(param1: string, param2: BigNumberish): Promise<string>;
// Event filters and types
filters: {
MyEvent(param?: string): TypedEventFilter<MyEvent>;
};
}// Example generated factory class
export class MyContract__factory extends ContractFactory {
constructor(signer?: Signer);
deploy(constructorArg: string): Promise<MyContract>;
attach(address: string): MyContract;
connect(signer: Signer): MyContract__factory;
static connect(address: string, signerOrProvider: Signer | Provider): MyContract;
}// Common utility types included in generated output
export interface TypedEvent<TArgsArray extends Array<any> = any, TArgsObject = any>
extends Event {
args: TArgsArray & TArgsObject;
}
export interface TypedEventFilter<_TEvent extends TypedEvent> extends EventFilter {}
export type PromiseOrValue<T> = T | Promise<T>;Comprehensive guide for using the generated TypeScript contract bindings with ethers.js v5, including deployment, method calls, event handling, and type safety best practices.
// Configuration interface for TypeChain target
interface Config {
cwd: string;
outDir?: string;
inputDir: string;
allFiles: string[];
target: string;
flags: CodegenConfig;
}
// Code generation configuration flags
interface CodegenConfig {
alwaysGenerateOverloads?: boolean;
discriminateTypes?: boolean;
environment?: 'hardhat' | 'truffle' | string;
}
// File description for processing
interface FileDescription {
path: string;
contents: string;
}