Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dgca/use-typechain-contracts
React hook to facilitate using TypeChain contract factories.
https://github.com/dgca/use-typechain-contracts
ethereum react solidity typescript
Last synced: 5 days ago
JSON representation
React hook to facilitate using TypeChain contract factories.
- Host: GitHub
- URL: https://github.com/dgca/use-typechain-contracts
- Owner: dgca
- Created: 2022-12-28T21:58:18.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-30T01:04:53.000Z (almost 2 years ago)
- Last Synced: 2024-10-18T14:59:29.598Z (30 days ago)
- Topics: ethereum, react, solidity, typescript
- Language: TypeScript
- Homepage:
- Size: 622 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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](https://github.com/dethcrypto/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.
```tsx
// 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.
```tsx
// src/app/app.tsx
import { TypeChainProvider } from '../utils/contracts';export function App() {
return (
);
}
```### `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.
```tsx
// 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 (
Greeter
{greeting}
);
}
```