or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

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

target-class.mddocs/

0

# Target Class

1

2

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.

3

4

## Capabilities

5

6

### Ethers Class

7

8

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

9

10

```typescript { .api }

11

/**

12

* TypeChain target for generating Ethers v5 compatible TypeScript bindings

13

*/

14

export default class Ethers extends TypeChainTarget {

15

readonly name: string; // Always "Ethers"

16

17

/**

18

* Creates new Ethers target instance

19

* @param config - TypeChain configuration including output directory and file list

20

*/

21

constructor(config: Config);

22

23

/**

24

* Transforms a single contract file (ABI/bytecode) into TypeScript bindings

25

* @param file - File description containing path and contents

26

* @returns Array of generated files or void if file cannot be processed

27

*/

28

transformFile(file: FileDescription): FileDescription[] | void;

29

30

31

/**

32

* Called after all files are processed to generate additional support files

33

* @returns Array of additional files (common.ts, hardhat.d.ts, barrel files)

34

*/

35

afterRun(): FileDescription[];

36

}

37

```

38

39

**Usage Example:**

40

41

```typescript

42

import { Config } from "typechain";

43

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

44

45

const config: Config = {

46

cwd: process.cwd(),

47

outDir: "./types/ethers-contracts/",

48

inputDir: "./contracts/",

49

allFiles: ["./contracts/ERC20.json", "./contracts/MyContract.json"],

50

target: "ethers-v5",

51

flags: {

52

alwaysGenerateOverloads: false,

53

discriminateTypes: false,

54

environment: "hardhat"

55

}

56

};

57

58

const target = new Ethers(config);

59

60

// TypeChain will call these methods automatically:

61

// 1. transformFile() for each contract file

62

// 2. afterRun() to generate support files

63

```

64

65

### File Processing Flow

66

67

The target processes files in the following sequence:

68

69

1. **File Classification**: Determines if file is `.bin` (bytecode) or `.abi`/`.json` (ABI)

70

2. **ABI Processing**: Extracts ABI and documentation from JSON files

71

3. **Bytecode Matching**: Pairs bytecode files with their corresponding ABI files

72

4. **Type Generation**: Creates contract interface and type definitions

73

5. **Factory Generation**: Creates factory classes for deployment (concrete contracts) or connection (abstract contracts)

74

6. **Support Files**: Generates common utilities, barrel exports, and optional Hardhat integration

75

76

### Generated File Structure

77

78

The target produces the following file structure:

79

80

```

81

types/ethers-contracts/

82

├── ContractName.ts # Contract interface and types

83

├── factories/

84

│ └── ContractName__factory.ts # Factory class for deployment/connection

85

├── common.ts # Common utility types

86

├── index.ts # Barrel exports

87

└── hardhat.d.ts # Hardhat integration (optional)

88

```

89

90

### Configuration Options

91

92

The target respects these configuration flags:

93

94

- `alwaysGenerateOverloads`: Generate function signature overloads even for non-overloaded functions

95

- `discriminateTypes`: Add `contractName` property to contracts for type discrimination

96

- `environment`: Set to "hardhat" to generate Hardhat-specific type definitions

97

98

### Hardhat Integration

99

100

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:

101

102

```typescript

103

// In Hardhat scripts/tests

104

import { ethers } from "hardhat";

105

106

const factory = await ethers.getContractFactory("MyContract");

107

const contract = await factory.deploy();

108

// Both factory and contract are fully typed

109

```

110

111

## Types

112

113

```typescript { .api }

114

// File description for generated output

115

interface FileDescription {

116

path: string;

117

contents: string;

118

}

119

120

// TypeChain configuration

121

interface Config {

122

cwd: string;

123

outDir?: string;

124

inputDir: string;

125

allFiles: string[];

126

target: string;

127

flags: CodegenConfig;

128

}

129

130

// Code generation configuration

131

interface CodegenConfig {

132

alwaysGenerateOverloads: boolean;

133

discriminateTypes: boolean;

134

environment?: string;

135

}

136

137

// Bytecode with external library references

138

interface BytecodeWithLinkReferences {

139

bytecode: string;

140

linkReferences?: LinkReference[];

141

}

142

```