TypeChain Documentation

repository·master·Indexed 25 days ago

https://github.com/dethcrypto/typechain

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

Tokens
34.3K
Snippets
68
Records
280
Agent score
83%

What's inside TypeChain

  1. Soft deprecation notice: Alternatives to TypeChain

    master
    TypeChain 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.
  2. Understand TypeChain target Ethers-v5 contract typings

    master

    TypeChain generates <contract-name>.ts files that provide type-safe interfaces for Ethers 5.x.x Contract instances. These typings allow you to interact with contracts with full TypeScript support for:

    • Methods: Accessible via contract.someMethod(...) or contract.functions.someMethod(...).
    • Events & Filters: Typed events available at contract.interface.events.AnEvent and filters at contract.filters.AnEvent.
    • Gas Estimation: Typed method gas estimates via contract.estimateGas.someMethod.
    • Event Listeners: Typed overrides for listener methods like on and once that 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 Contract class.

  3. Generate typings for Ethers.js v6

    master

    Use the ethers-v6 target 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 as node16modules), use the --node16-modules flag to ensure the generated typings are compatible.

  4. Configure TypeChain in Hardhat

    master

    After 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 run hardhat clean if you modify any TypeChain configuration options.

  5. Manage generated files in Git

    master
    It is recommended not to commit generated files to your Git repository. Instead, add them to your .gitignore and automate generation using a postinstall hook in your package.json to ensure they are always up-to-date with your ABIs.
  6. Install TypeChain and targets

    master

    To 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 typechain
  7. Run the TypeChain x Hardhat x Truffle example

    master

    After the initial setup, you can run the example's tests using pnpm test. Note that pnpm install automatically triggers TypeChain type generation. If you need to manually regenerate the types, use the pnpm generate-types command.

    pnpm install # it will automatically run TypeChain types generation
    
    pnpm test
    
    # To manually regenerate types:
    pnpm generate-types
  8. Setup the TypeChain x Hardhat example

    master

    To run the TypeChain x Hardhat example, you must first build the monorepo. Follow these steps from the root of the monorepo:

    1. Install dependencies: pnpm install
    2. 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