TypeChain Support
All of the contract methods support TypeChain, which allows for deeper TypeScript integration, with better typings.
Installing TypeChain
- pnpm
- Yarn
- npm
pnpm add typechain @typechain/ethers-v5 ethers
yarn add typechain @typechain/ethers-v5 ethers
npm add typechain @typechain/ethers-v5 ethers
Configuring TypeChain
TypeChain must be configured to output types for ethers-v5
. This can be done by setting --target=ethers-v5
when running the typechain
command.
Hardhat
If you are using hardhat, we recommend using the hardhat typechain plugin to simplify the process of generating types.
Using TypeChain Generated Contracts
TypeChain emits a contract factory, which can be used in place of the ABI in the useContract
hook. This will automatically preserve the contract typings, which are also supported in the useReadContract
and useWriteContract
hooks.
import { useContract, useReadContract } from "ethereal-react";
import { Greeter__factory } from "./typechain";
function Greeter() {
const contract = useContract(CONTRACT_ADDRESS, Greeter__factory);
// This is type-checked!
const greeting = useReadContract(contract, "greeting");
return <div>Current greeting: {greeting}</div>;
}