TypeChain

8.3.2 · active · verified Sun Apr 19

TypeChain is a foundational development tool for the Ethereum ecosystem, providing TypeScript bindings for smart contracts. It enables developers to generate static, type-safe interfaces for their Solidity contracts, significantly reducing runtime errors and improving developer experience through autocompletion and compile-time checks. The current stable version is 8.3.2, with frequent patch releases addressing bug fixes and minor updates to support new features or align with major dependency versions (like Ethers.js and Hardhat). TypeChain is highly extensible, supporting various popular web3 libraries and frameworks such as Ethers.js (v5 and v6), Hardhat, Truffle, and Web3.js through dedicated 'targets'. This broad compatibility and its ability to work with both simple JSON ABI files and complex Truffle/Hardhat artifacts distinguish it from more tightly coupled alternatives, making it a ubiquitous tool in professional smart contract development workflows.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `typechain` integration with Hardhat by configuring the plugin, specifying an Ethers.js v6 target, and then using the generated TypeScript bindings for deploying and interacting with a sample smart contract.

import { HardhatUserConfig, task } from 'hardhat/config';
import '@nomicfoundation/hardhat-toolbox';
import '@typechain/hardhat'; // Import the Hardhat TypeChain plugin

const config: HardhatUserConfig = {
  solidity: "0.8.20",
  networks: {
    hardhat: {
      chainId: 1337, // Example local network ID
    },
  },
  typechain: {
    outDir: 'typechain-types', // Output directory for generated types
    target: 'ethers-v6', // Specify the target library (e.g., ethers.js v6)
    alwaysConsiderOverloadedFunctions: true,
  },
};

// Example Hardhat task demonstrating usage of TypeChain generated types
task("deploy", "Deploys a sample contract").setAction(async (taskArgs, hre) => {
  const [deployer] = await hre.ethers.getSigners();
  console.log(
    "Deploying contracts with the account:",
    deployer.address
  );

  // Accessing a generated contract factory type (e.g., Greeter__factory)
  // Ensure your contract is named 'Greeter.sol' in the 'contracts' directory
  const GreeterFactory = await hre.ethers.getContractFactory("Greeter");
  const greeter = await GreeterFactory.deploy("Hello, Hardhat!");

  await greeter.waitForDeployment();

  console.log(`Greeter deployed to ${greeter.target}`);

  // Using type-safe method calls on the deployed contract instance
  const message = await greeter.greet();
  console.log("Contract message:", message);
});

export default config;

/*
// Example Solidity contract: contracts/Greeter.sol
// SPDX-License-Identifier: MIT
// pragma solidity ^0.8.20;
// 
// contract Greeter {
//     string private _greeting;
// 
//     constructor(string memory greeting_) {
//         _greeting = greeting_;
//     }
// 
//     function greet() public view returns (string memory) {
//         return _greeting;
//     }
// 
//     function setGreeting(string memory greeting_) public {
//         _greeting = greeting_;
//     }
// }
*/

view raw JSON →