TypeChain
🔌 TypeScript bindings for Ethereum smart contracts
Install / Use
/learn @dethcrypto/TypeChainREADME
Soft deprecation notice
I now consider TypeChain to be legacy software and no longer use it personally. The ecosystem around Abitype, Wagmi, Viem got really good and it provides a superior Developer Experience by parsing ABIs directly within the TypeScript type system. Hardhat can also be used with Viem. I recommend giving it a try!
Critical fixes will be merged, but active development should not be expected. Thanks for all these years!
Features ⚡
- static typing - you will never call not existing method again
- IDE support - works with any IDE supporting Typescript
- extendible - work with many different tools:
ethers.js,hardhat,truffle,Web3.jsor you can create your own target - frictionless - works with simple, JSON ABI files as well as with Truffle/Hardhat artifacts
Installation
npm install --save-dev typechain
You will also need to install a desired target for example @typechain/ethers-v6. Learn more about targets
Take note, that code generated by TypeChain requires TypeScript version 4.3 or newer.
Packages 📦
| Package | Version | Description | Examples |
| ------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| typechain | | Core package | - |
|
@typechain/ethers-v5 | | Ethers ver 5 support (⚠️ requires TS 4.0 >=) | example |
|
@typechain/ethers-v6 | | Ethers ver 6 support (⚠️ requires TS 4.0 >=) | example |
|
@typechain/starknet.js | | Starknet.js ver 3.9 | |
|
@typechain/truffle-v5 | | Truffle ver 5 support | example |
|
@typechain/web3-v1 | | Web3 ver 1 support | example |
|
@typechain/hardhat | | Hardhat plugin | example-ethers example-truffle |
|
@typechain/truffle-v4 | | Truffle ver 4 support (deprecated) | example |
|
@typechain/ethers-v4 | | Ethers ver 4 support (deprecated) | example |
eth-sdk
TypeChain generates only TypeScript typings (d.ts) files, if you're looking for "opinionated", "batteries included"
solution check out our new project: eth-sdk. It generates typesafe, ready to
use ethers.js wrappers and uses etherscan/sourcify to automatically get ABIs based only on smart contract addresses.
Under the hood, eth-sdk relies on TypeChain.
Usage
CLI
Note: If you use hardhat just use hardhat plugin.
typechain --target=(ethers-v5|ethers-v6|truffle-v4|truffle-v5|web3-v1|path-to-custom-target) [glob]
glob- pattern that will be used to find ABIs, remember about adding quotes:typechain "**/*.json", examples:./abis/**/*.abi,./abis/?(Oasis.abi|OasisHelper.abi).--target- ethers-v5, ethers-v6, truffle-v4, truffle-v5, web3-v1 or path to your custom target. Typechain will try to load package named:@typechain/${target}, so make sure that desired package is installed.--out-dir(optional) - put all generated files to a specific dir.--always-generate-overloads(optional) - some targets won't generate unnecessary types for overloaded functions by default, this option forces to always generate them--discriminate-types(optional) - ethers-v5 and ethers-v6 will add an artificial fieldcontractNamethat helps discriminate between contracts
TypeChain always will rewrite existing files. You should not commit them. Read more in FAQ section.
Example:
typechain --target ethers-v6 --out-dir app/contracts './node_modules/neufund-contracts/build/contracts/*.json'
Videos
- [Devcon5 Video (2019)](https://www.youtube.com/w
