npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-typechain-contracts

v0.0.8

Published

React hook to facilitate using TypeChain contract factories.

Readme

use-typechain-contracts

React hook and context provider that allows you to easily consume connected TypeChain contract factories.


Features

  • Access all of your contracts via a single useContracts() hook
  • Contracts are automatically connected to the available ethers Provider
  • Optionally set default contract addresses
  • Contracts returned by useContracts() are entirely type-safe

Requirements

  • react >=16
  • ethers ^5.7.0
  • wagmi ^0.9.0 || ^0.10.0
  • TypeChain
    • Not a dependency of the project, but this library wraps TypeChain's generated contract factories to work.

Usage

Installation

You know the drill...

  • npm install use-typechain-contracts
  • yarn add use-typechain-contracts

Initialization

First, initialize the system by calling the init function and passing all of TypeChain's exports (e.g. do this via import * as typechain from '...') as the first argument.

If you'd like to set up some default contract addresses, you may optionally do so by passing an object as a second argument where the keys are the name of the contracts, and the values are the address for that contract. If you do not pass a default contract address for a contract, you'll have to provide it when using the useContracts hook.

init returns a TypeChainProvider context provider and a useContracts hook. Export these items, as these are what you'll be using in your application.

// src/utils/contracts.ts
import { init } from 'use-typechain-contracts';

import * as typechain from 'path/to/typechain';

/*
 * Assume my project has two contracts, Greeter and Todos. Greeter is a singleton that
 * only has one contract address, and Todos may be deployed multiple times (i.e. it will
 * have multiple contract addresses). I can set the deafult contract address for Greeter here,
 * and now when I use it by using `const greeter = useContracts().Greeter()`, it'll automatically
 * connect to the default contract address.
 *
 * In order to use a Todo instance, I'll have to specify the address when using `useContracts()`.
 * E.g. `const todoInstance = useContracts().Todos('0xEXAMPLExADDRESS')`.
 */
const { TypeChainProvider, useContracts } = init(typechain, {
  Greeter: '0x123xDEMOxADDRESSx420',
});

export { TypeChainProvider, useContracts };

Provider

Next, wrap your application in the TypeChainProvider. Make sure your WagmiConfig provider is higher up in the component tree, as TypeChainProvider uses wagmi under the hood.

// src/app/app.tsx
import { TypeChainProvider } from '../utils/contracts';

export function App() {
  return (
    <WagmiConfig client={...}>
      <TypeChainProvider>
        <Demo />
      </TypeChainProvider>
    </WagmiConfig>
  );
}

useContracts hook

Finally, to use your contracts, import the useContracts hook you created earlier.

If you set a default contract address for a given contract, you can omit the contract address when getting that contract. If you did not set a default contract address, you must provide the contract address or you will see a type error.

// src/app/demo.tsx
import { useState, useEffect } from 'react';
import { useContracts } from '../utils/contracts';

export function Greeter() {
  const contracts = useContracts();
  const greeter = contracts.Greeter();

  const [greeting, setGreeting] = useState('Loading...');

  useEffect(() => {
    const fetchGreeting = async () => {
      const currentGreeting = await greeter.getGreeting();
      setGreeting(currentGreeting);
    };

    fetchGreeting();
  }, []);

  return (
    <div>
      <h1>Greeter</h1>
      <p>{greeting}</p>
    </div>
  );
}