What is @typechain/ethers-v5?
@typechain/ethers-v5 is a TypeScript code generator that generates TypeScript typings for Ethereum smart contracts. It is specifically designed to work with the ethers.js library (version 5). This package helps developers interact with Ethereum smart contracts in a type-safe manner, reducing the likelihood of runtime errors and improving the developer experience.
What are @typechain/ethers-v5's main functionalities?
Generating TypeScript typings for smart contracts
This feature allows you to generate TypeScript typings for your Ethereum smart contracts. By running the TypeChain command with the ethers-v5 target, you can create type-safe contract interfaces that can be used in your TypeScript project.
const { execSync } = require('child_process');
execSync('npx typechain --target ethers-v5 --out-dir src/types "./artifacts/**/*.json"');
Interacting with smart contracts using type-safe methods
This feature demonstrates how to interact with a smart contract using the generated TypeScript typings. By importing the contract interface and using ethers.js, you can call contract methods in a type-safe manner, ensuring that the correct types are used and reducing the risk of runtime errors.
import { MyContract } from './types/MyContract';
import { ethers } from 'ethers';
async function main() {
const provider = new ethers.providers.JsonRpcProvider('http://localhost:8545');
const signer = provider.getSigner();
const myContract = new ethers.Contract('0xYourContractAddress', MyContract.abi, signer) as MyContract;
const result = await myContract.myMethod();
console.log(result);
}
main();
Other packages similar to @typechain/ethers-v5
@typechain/truffle-v5
@typechain/truffle-v5 is a TypeScript code generator that generates TypeScript typings for Ethereum smart contracts specifically designed to work with the Truffle framework. It provides similar functionality to @typechain/ethers-v5 but is tailored for projects using Truffle instead of ethers.js.
ethers
ethers is a library for interacting with the Ethereum blockchain and its ecosystem. While it does not generate TypeScript typings for smart contracts, it provides a comprehensive set of tools for interacting with Ethereum, including contract interaction, wallet management, and more. @typechain/ethers-v5 builds on top of ethers.js to provide type-safe contract interactions.
Typechain target Ethers-v5
TypeChain target Ethers-v5
🔌 TypeScript bindings for Ethers 5.x.x smartcontracts
Medium post | DappCon Video
This package requires TypeScript >= 4.0. If you need support for earlier TS versions check out: 1.0 version of this
package.
Contract typings
The main files generated by this target are <contract-name>.ts
. They declare typesafe interfaces for your contracts
on top of ethers Contract
instances:
- typed contract's methods, available both at
contract.someMethod(...)
and contract.functions.someMethod(...)
- typed events in
contract.interface.events.AnEvent
and filters in contract.filters.AnEvent
- typed method gas estimates in
contract.estimateGas.someMethod
- overrides for the event listener methods (
on
, once
, etc) that return the same contract type.
Note: these are just type declarations to help you call the blockchain properly, so they're not available at runtime,
and all of the contracts are still instances of the same Contract
class.
Contract factories
This target also generates a concrete factory class for each contract, to help you deploy or connect to contract
instances. The factory classes are an extension of ethers' ContractFactory
. They serve two main purposes:
- wrap passing contract ABI and bytecode to the
ContractFactory
class, so you don't have to load and parse the JSON
manually - provide a correctly typed interface to
ContractFactory
(since it returns plain Contract
instances).
Abstract contracts or solidity interfaces are handled a bit different, because they have no bytecode. For those, a
simplified factory is generated that doesn't extends ContractFactory
, and only includes the static connect
method,
so you can easily connect to a deployed instance without having to pass the ABI manually.
Basic example
Suppose you have an Erc20Token.sol
solidity interface and a DummyToken.sol
contract implementing it.
import { BigNumber } from 'ethers';
import { Wallet } from 'ethers';
import { DummyTokenFactory } from 'typechain-out-dir/DummyTokenFactory';
import { DummyToken } from 'typechain-out-dir/DummyToken';
import { Erc20TokenFactory } from 'typechain-out-dir/Erc20TokenFactory';
const provider = getYourProvider(...);
async function deployTestToken(ownerPK: string): Promise<DummyToken> {
const owner = new Wallet(ownerPK, provider);
return new DummyTokenFactory(owner).deploy();
}
async function getTokenBalance(walletAddress: string, tokenAddress: string): Promise<BigNumber> {
const token = Erc20TokenFactory.connect(tokenAddress, provider);
return token.balanceOf(walletAddress);
}