{"id":16469274,"url":"https://github.com/dgca/use-typechain-contracts","last_synced_at":"2026-05-02T17:31:14.788Z","repository":{"id":65141260,"uuid":"583136650","full_name":"dgca/use-typechain-contracts","owner":"dgca","description":"React hook to facilitate using TypeChain contract factories.","archived":false,"fork":false,"pushed_at":"2022-12-30T01:04:53.000Z","size":637,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-10T18:48:22.079Z","etag":null,"topics":["ethereum","react","solidity","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dgca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-12-28T21:58:18.000Z","updated_at":"2024-08-03T15:15:54.000Z","dependencies_parsed_at":"2023-01-09T13:01:19.493Z","dependency_job_id":null,"html_url":"https://github.com/dgca/use-typechain-contracts","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgca%2Fuse-typechain-contracts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgca%2Fuse-typechain-contracts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgca%2Fuse-typechain-contracts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgca%2Fuse-typechain-contracts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dgca","download_url":"https://codeload.github.com/dgca/use-typechain-contracts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241061935,"owners_count":19902794,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ethereum","react","solidity","typescript"],"created_at":"2024-10-11T12:06:44.129Z","updated_at":"2026-05-02T17:31:09.702Z","avatar_url":"https://github.com/dgca.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-typechain-contracts\n\nReact hook and context provider that allows you to easily consume connected TypeChain contract factories.\n\n---\n\n## Features\n\n* Access all of your contracts via a single `useContracts()` hook\n* Contracts are automatically connected to the available ethers Provider\n* Optionally set default contract addresses\n* Contracts returned by `useContracts()` are entirely type-safe\n\n## Requirements\n\n* `react \u003e=16`\n* `ethers ^5.7.0`\n* `wagmi ^0.9.0 || ^0.10.0`\n* [TypeChain](https://github.com/dethcrypto/TypeChain)\n  * Not a dependency of the project, but this library wraps TypeChain's generated contract factories to work.\n\n## Usage\n\n### Installation\n\nYou know the drill...\n\n* `npm install use-typechain-contracts`\n* `yarn add use-typechain-contracts`\n\n### Initialization\n\nFirst, 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.\n\nIf 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.\n\n`init` returns a `TypeChainProvider` context provider and a `useContracts` hook. Export these items, as these are what you'll be using in your application.\n\n```tsx\n// src/utils/contracts.ts\nimport { init } from 'use-typechain-contracts';\n\nimport * as typechain from 'path/to/typechain';\n\n/*\n * Assume my project has two contracts, Greeter and Todos. Greeter is a singleton that\n * only has one contract address, and Todos may be deployed multiple times (i.e. it will\n * have multiple contract addresses). I can set the deafult contract address for Greeter here,\n * and now when I use it by using `const greeter = useContracts().Greeter()`, it'll automatically\n * connect to the default contract address.\n *\n * In order to use a Todo instance, I'll have to specify the address when using `useContracts()`.\n * E.g. `const todoInstance = useContracts().Todos('0xEXAMPLExADDRESS')`.\n */\nconst { TypeChainProvider, useContracts } = init(typechain, {\n  Greeter: '0x123xDEMOxADDRESSx420',\n});\n\nexport { TypeChainProvider, useContracts };\n```\n\n### Provider\n\nNext, 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.\n\n```tsx\n// src/app/app.tsx\nimport { TypeChainProvider } from '../utils/contracts';\n\nexport function App() {\n  return (\n    \u003cWagmiConfig client={...}\u003e\n      \u003cTypeChainProvider\u003e\n        \u003cDemo /\u003e\n      \u003c/TypeChainProvider\u003e\n    \u003c/WagmiConfig\u003e\n  );\n}\n```\n\n### `useContracts` hook\n\nFinally, to use your contracts, import the `useContracts` hook you created earlier.\n\nIf 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.\n\n```tsx\n// src/app/demo.tsx\nimport { useState, useEffect } from 'react';\nimport { useContracts } from '../utils/contracts';\n\nexport function Greeter() {\n  const contracts = useContracts();\n  const greeter = contracts.Greeter();\n\n  const [greeting, setGreeting] = useState('Loading...');\n\n  useEffect(() =\u003e {\n    const fetchGreeting = async () =\u003e {\n      const currentGreeting = await greeter.getGreeting();\n      setGreeting(currentGreeting);\n    };\n\n    fetchGreeting();\n  }, []);\n\n  return (\n    \u003cdiv\u003e\n      \u003ch1\u003eGreeter\u003c/h1\u003e\n      \u003cp\u003e{greeting}\u003c/p\u003e\n    \u003c/div\u003e\n  );\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgca%2Fuse-typechain-contracts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgca%2Fuse-typechain-contracts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgca%2Fuse-typechain-contracts/lists"}