TypeChain Documentation
repository·master·Indexed 25 days ago
https://github.com/dethcrypto/typechainA code generator that provides static TypeScript bindings for Ethereum smart contracts by converting JSON ABI files into type-safe TypeScript classes. It supports libraries such as ethers.js (v5 and v6), web3.js, and Truffle, and includes a dedicated Hardhat plugin for automatic type generation during compilation.
What's inside TypeChain
- Typechain target Starknet.js provides TypeScript bindings for Starknet.js 3.x. It allows developers to interact with Starknet smart contracts using strongly typed interfaces, improving developer experience and reducing errors when working with the Starknet ecosystem.
Soft deprecation notice: Alternatives to TypeChain
masterTypeChain is considered legacy software. For a superior Developer Experience, the author recommends using the ecosystem around Abitype, Wagmi, and Viem, which parse ABIs directly within the TypeScript type system. Hardhat can also be used with Viem.Use TypeChain target Web3-v1
masterTypeChain target Web3-v1 provides TypeScript bindings for Web3 1.x.x smart contracts. It allows you to interact with Ethereum smart contracts using type-safe interfaces when using the Web3.js library.Understand TypeChain target Ethers-v5 contract typings
masterTypeChain generates
<contract-name>.tsfiles that provide type-safe interfaces for Ethers 5.x.xContractinstances. These typings allow you to interact with contracts with full TypeScript support for:- Methods: Accessible via
contract.someMethod(...)orcontract.functions.someMethod(...). - Events & Filters: Typed events available at
contract.interface.events.AnEventand filters atcontract.filters.AnEvent. - Gas Estimation: Typed method gas estimates via
contract.estimateGas.someMethod. - Event Listeners: Typed overrides for listener methods like
onandoncethat return the same contract type.
Note: These are type declarations only and are not available at runtime; all generated contracts remain instances of the standard Ethers
Contractclass.- Methods: Accessible via
Use TypeChain target Truffle-v5
masterTypeChain target Truffle-v5 provides TypeScript bindings for smart contracts used with Truffle 5.x.x. It allows you to interact with your Truffle-based smart contracts using strongly typed TypeScript interfaces.Generate typings for Ethers.js v6
masterUse the
ethers-v6target to generate wrappers for the Ethers.js v6 library. For optimal integration with Hardhat, use the official Hardhat plugin.If your project uses
nodenext(also known asnode16modules), use the--node16-modulesflag to ensure the generated typings are compatible.Configure TypeChain in Hardhat
masterAfter installation, you must register the plugin in your Hardhat configuration file.
For JavaScript (
hardhat.config.js):require('@typechain/hardhat') require('@nomicfoundation/hardhat-ethers') require('@nomicfoundation/hardhat-chai-matchers')For TypeScript (
hardhat.config.ts):import '@typechain/hardhat' import '@nomicfoundation/hardhat-ethers' import '@nomicfoundation/hardhat-chai-matchers'Important: Before running for the first time, execute
npx hardhat clean. This ensures TypeChain performs a full generation rather than an incremental one. You should also runhardhat cleanif you modify any TypeChain configuration options.Manage generated files in Git
masterIt is recommended not to commit generated files to your Git repository. Instead, add them to your.gitignoreand automate generation using apostinstallhook in yourpackage.jsonto ensure they are always up-to-date with your ABIs.Install TypeChain and targets
masterTo use TypeChain, install the core package as a development dependency. You must also install a specific target package corresponding to the library you are using (e.g.,
@typechain/ethers-v6).Note: Generated code requires TypeScript version 4.3 or newer.
npm install --save-dev typechainRun the TypeChain x Hardhat x Truffle example
masterAfter the initial setup, you can run the example's tests using
pnpm test. Note thatpnpm installautomatically triggers TypeChain type generation. If you need to manually regenerate the types, use thepnpm generate-typescommand.pnpm install # it will automatically run TypeChain types generation pnpm test # To manually regenerate types: pnpm generate-typesSetup the TypeChain x Hardhat example
masterTo run the TypeChain x Hardhat example, you must first build the monorepo. Follow these steps from the root of the monorepo:
- Install dependencies:
pnpm install - Build the monorepo:
pnpm build
Once built, you can enter the example directory and run
pnpm install. This will automatically trigger the TypeChain types generation.# in the root of monorepo pnpm install pnpm build- Install dependencies:
Run migrations in the Truffle v5 example
masterTo run migrations, use the
pnpm migratecommand.Important Limitation: Migrations must be transpiled from TypeScript to JavaScript before they can be executed by Truffle. This requirement does not apply to running tests.
pnpm migrate