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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@web3-ui/hooks

v0.13.0

Published

React hooks for web3

Downloads

67

Readme

❗️Important: This package has been deprecated.

@web3-ui/hooks

A set of React hooks developed for web3 use cases.

yarn add @web3-ui/hooks ethers

Getting started

At the root of your React app, wrap your app in the component:

// _app.tsx (or the root of your app)
import { Provider, NETWORKS } from '@web3-ui/hooks';

function MyApp({ Component, pageProps }) {
  return (
    <Provider network={NETWORKS.mainnet}>
      <Component {...pageProps} />
    </Provider>
  );
}

Hooks

The following hooks are available:


useWallet

The useWallet hook lets you request and interact with the active wallet connection.

  • connectWallet: Calling this function will open up a modal that will let the user connect their wallet.
  • disconnectWallet
  • connection: This object contains the following properties:
    • userAddress
    • network
    • signer
    • ens
  • connected: a boolean indicating if the user has connected their wallet
  • provider
  • correctNetwork: a boolean indicating if the user is connected to the correct network
  • switchToCorrectNetwork: a function that will switch to the correct network. (only MetaMask supported)
import { useWallet } from '@web3-ui/hooks';

const {
  connection,
  connectWallet,
  disconnectWallet,
  connected,
  correctNetwork,
  switchToCorrectNetwork,
} = useWallet();

useWriteContract

The useWriteContract hook takes the ABI and address of a contract and returns the contract instance. This hook requires the user to have connected their wallet. If you don't want to force your users to connect their wallet in order to read from a contract, use useReadOnlyContract instead.

import { useWriteContract } from '@web3-ui/hooks';

const [contract, isReady] = useWriteContract(
  'CONTRACT_ADDRESS',
  'CONTRACT_ABI'
);

// check that the contract has been loaded
if (isReady) {
  await contract.greeting();
}

A generic type argument can be passed down to the hook to create the type definitions based on the ABIs stored in a directory. To auto-generate the types it is highly recommended to use typechain package.

Install typechain

yarn add typechain @typechain/ethers-v5 --dev # or `npm i -D typechain @typechain/ethers-v5`

Add a "typechain" script to your package.json file as well as a "postinstall" script that executes the script after installing dependencies.

"scripts": {
    "postinstall": "yarn typechain",
    "typechain": "typechain --target=ethers-v5 <ABI_DIRECTORY_PATH> --out-dir=<OUTPUT_DIRECTORY_PATH>",
}
  • The <ABI_DIRECTORY_PATH> should be the path to where all of the ABIs are stored. e.g. src/abis/*.json (This depends on your preferred file structure)
  • The <OUTPUT_DIRECTORY_PATH> will be your preferred path to where the generated type definitions should be placed. e.g. src/types/contracts
  • For the <OUTPUT_DIRECTORY_PATH> it is also recommended to add the directory path on .gitignore since these can be generated via typechain script

For an actual example check below,

"scripts": {
    "postinstall": "yarn typechain",
    "typechain": "typechain --target=ethers-v5 src/abis/**/*.json --out-dir=src/types/contracts",
}

Next is to put all of your ABI JSON files stored to the defined ABI_DIRECTORY_PATH directory.

  • src/abis/ERC20Token/ERC20Token.json
  • src/abis/CoolProtocol/CoolProtocolLendingPool.json

Then finally run the script to generate the type definitions.

yarn typechain # or `npm run typechain`

Example usage in utilizing the generic type argument for useWriteContract hook

import React from 'react';
import { useWriteContract } from '@web3-ui/hooks';
import { ERC20Token } from 'types/contracts/ERC20Token';
import ERC20TokenABI from 'abis/ERC20Token/ERC20Token.json';

function App() {
  const [contract, isReady] = useWriteContract<ERC20Token>(
    'CONTRACT_ADDRESS',
    ERC20TokenABI
  );

  async function checkBalance() {
    const response = await contract.balanceOf('0x...');

    console.log('checkBalance', response);
  }

  return (
    <>{isReady ? <button onClick={checkBalance}></button> : 'Connect Wallet'}</>
  );
}

export default App;

useTransaction

The useTransaction hook takes in a contract function. It returns an array of three elements:

  • execute: Calling this function will execute the transaction. You should pass your arguments/parameters to this function as an array.

  • loading: Will be true when the transaction is executing and will be false once the transaction has been confirmed.

  • error

import { useTransaction, useWriteContract } from '@web3-ui/hooks';

const greeterContract = useWriteContract('CONTRACT_ADDRESS', 'CONTRACT_ABI');
const [execute, loading, error] = useTransaction(greeter.setGreeting);

await execute('Hello, world!', {
  value: ethers.utils.parseEther('0.1'), // you can also use this for payable transactions
}); // will execute the transaction

useTokenBalance

The useTokenBalance hook takes in a ERC20 token contract address and an account address as arguments and returns an object with the following properties:

  • balance: The balance of the token for the given account in wei.
  • loading
  • error
  • decimals: The number of decimals the token contract is using.
  • formattedBalance: Balance in ethers. eg. 20 GTC, 31.2 USDT, etc.
  • balanceInBigNumber
import { useTokenBalance } from '@web3-ui/hooks';

const {
  balance,
  loading,
  error,
  decimals,
  formattedBalance,
  balanceInBigNumber,
} = useTokenBalance('TOKEN_CONTRACT_ADDRESS', 'ACCOUNT_ADDRESS');

useReadOnlyContract

The useReadOnlyContract hook takes in a contract address and an ABI and returns a read-only contract instance. This is especially useful when you want to read data from a function without asking the user to connect their wallet. eg. When you are only 'reading' from a contract and not interacting with it.

In order for this hook to work, you need to pass in a rpcUrl to the <Provider />. eg. https://rinkeby.infura.io/v3/YOUR_INFURA_ID

<Provider network={NETWORKS.rinkeby} rpcUrl='https://rinkeby.infura.io/v3/YOUR_INFURA_ID'>
import { useReadOnlyContract } from '@web3-ui/hooks';

const [contract, isReady] = useReadOnlyContract(
  'CONTRACT_ADDRESS',
  'CONTRACT_ABI'
);

useReadOnlyProvider

The useReadOnlyProvider takes in a RPC URL (think Infura, Alchemy, etc.) and returns a provider. This provider can be used to read data from the blockchain and from any contract.

import { useReadOnlyProvider } from '@web3-ui/hooks';

const provider = useReadOnlyProvider(
  'https://rinkeby.infura.io/v3/YOUR_INFURA_ID'
);

usePoller

The usePoller hook takes in a function and a delay in milliseconds. It will call the function in every delay.

import { usePoller } from '@web3-ui/hooks';

usePoller(() => console.log(contract.balanceOf(account)), 1000); // will log the balance every second