or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdtarget-class.mdusing-generated-types.md
tile.json

tessl/npm-typechain--ethers-v5

TypeChain target for generating TypeScript bindings compatible with ethers-v5

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@typechain/ethers-v5@10.2.x

To install, run

npx @tessl/cli install tessl/npm-typechain--ethers-v5@10.2.0

index.mddocs/

TypeChain Ethers v5 Target

@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.

Package Information

  • Package Name: @typechain/ethers-v5
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev @typechain/ethers-v5

Core Imports

import Ethers from "@typechain/ethers-v5";

For CommonJS:

const Ethers = require("@typechain/ethers-v5").default;

Basic Usage

As TypeChain Target (Recommended)

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'
  }
};

Using Generated Types

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);

Architecture

@typechain/ethers-v5 is built around several key components:

  • Main Target Class: Ethers class extending TypeChainTarget, orchestrates the generation process
  • Code Generation Modules: Specialized modules for generating functions, events, types, and struct definitions
  • Factory Generation: Creates both concrete and abstract factory classes for contract deployment and connection
  • Type System: Comprehensive TypeScript type generation for all contract interfaces, events, and data structures
  • Static Utilities: Common type definitions and utility types copied to generated output

Capabilities

Ethers Target Class

The 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[];
}

Target Class

Generated Output Types

The target generates several types of files for type-safe contract interaction:

Contract Interfaces

// 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>;
  };
}

Factory Classes

// 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 Types

// 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>;

Usage Guide

Comprehensive guide for using the generated TypeScript contract bindings with ethers.js v5, including deployment, method calls, event handling, and type safety best practices.

Using Generated Types

Types

// 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;
}