ethereum-hooks
v2.0.4
Published
Resourceful NPM package that contains React hooks for working with the Ethereum Blockchain.
Maintainers
Readme
ethereum-hooks
Useful package that contains custom React hooks that can be used for rapid development while working with the Ethereum blockchain.
These hooks make use of various crypto APIs (free versions only) and save developer time by taking away the need for manual configuration and setup.
The following resources were utilized when building these client hooks:
- Alchemy
- Blocknative
- CoinGecko
- Moralis
- Opensea
- Transpose
React Hooks
This package uses AWS Lambda functions to construct authenticated API calls. The architecture follows a simple flow: Client Hook → useFetch → AWS Lambda → Response.
The AWS Lambda functions handle authentication automatically using pre-configured API keys for various blockchain services:
- OPENSEA_API_KEY - For NFT marketplace data
- MORALIS_API_KEY - For Web3 data aggregation
- BLK_API_KEY - For Blocknative gas and mempool data
- TRANSPOSE_API_KEY - For indexed blockchain data
- ALCHEMY_API_KEY - For Ethereum node access and enhanced APIs
No backend setup or environment variables are required on your end.
React Client Hooks
The hooks cover several areas of the Ethereum blockchain and can be used for Layer Two chains as well.
Each hook automatically connects to its dedicated AWS Lambda endpoint - no manual URL configuration is needed.
Import Structure (v2.0.2+)
Starting with version 2.0.2, hooks are organized into grouped imports for better organization:
// ENS Hooks
import { useAddressENSLookup, useENSNameLookup, useENSAddressLookup, useENSIDLookup } from 'ethereum-hooks/ens';
// ERC20 Token Hooks
import { useERC20Holdings, useERC20Transfers, useERC20CollectionOwners } from 'ethereum-hooks/erc20';
// ERC721 Token Hooks
import { useERC721CollectionData, useERC721Holdings, useERC721LookupData } from 'ethereum-hooks/erc721';
// Gas Hooks
import { useGasLookup } from 'ethereum-hooks/gas';
// Price Hooks
import { useETHPrice, useERC20Price, useERC721Price, useLayerTwoPrice } from 'ethereum-hooks/prices';This structured approach makes it easier to:
- Import only what you need
- Understand which category each hook belongs to
- Maintain cleaner import statements
Here is a quick example of how you can work with client hooks. The following is a code snippet for working with React.js:
ENSToAddressPage.tsx
import React, { FC } from 'react';
import { useENSAddressLookup } from 'ethereum-hooks/ens';
// Incorporating the ENS to Address Client Hook.. using Vitalik Buterin's address
// No server setup required - connects directly to AWS Lambda
const ENSToAddressPage: FC = () => {
// Hook automatically connects to AWS Lambda - no server setup required
const addressInformation = useENSAddressLookup('vitalik.eth');
// Each client hook uses the useFetch custom hook
// It returns three states: data, error, loading
// We capture these states in a variable (like above) and conditionally render the component
if (addressInformation.loading){
return <div>Loading..</div>
}
else if (addressInformation.error){
return <div>Error loading data...</div>
}
else {
// Hardcoded some parts for demonstrative purposes only
return (
<div className='home-page'>
<table>
<tr>
<th>ENS</th>
<th>Address</th>
</tr>
<tr>
<td>vitalik.eth</td>
<td>{ addressInformation.data?.information }</td>
</tr>
</table>
</div>
)
}
}
export default ENSToAddressPage;A list of chains supported is provided below in the Types section.
AWS Lambda Endpoints
The following table shows the 30 different client hooks and their corresponding AWS Lambda endpoints:
Custom Hooks
Custom hooks were incorporated into the main client hooks. The following table details the custom hooks used in this package:
Types
Custom data types were developed for monitoring data fetch status and defining a set of available layer two networks:
