ethereum-hooks v1.0.2
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 Client-Server Setup
When working with this package, you will need to implement the typical MERN design pattern, set up .env variables, and set up the server to allow the client hooks to establish communication to the back-end.
You will need to set the following environment variables in your .env file:
- ALCHEMY_API_KEY
- BLK_API_KEY
- CLIENT_URL
- COINGECKO_API_KEY
- MORALIS_API_KEY
- OPENSEA_API_KEY
- PORT
- TRANSPOSE_API_KEY
The following diagram will help you understand the workflow:
Server Setup
For this part, you will need to incorporate the server object exported from the server.ts file inside the server directory.
You will need to build on top of this built-in server, any additional routes and configurations as this server contains all the routes needed to effectively use the client hooks.
This is how you can incorporate the built-in server for additional add-ons and configuration in a custom server file of your own:
customServer.ts
import { server } from 'server'; // Import the built-in server
// Environment variables, PORT, and CLIENT_URL will be used by this built-in server
// PORT specifies the port to activate the Node server
// CLIENT_URL specifies the URL that is CORS enabled
// Add any additional routes you may have on top of this server, etc.
server.use("", "/xxx-xxxx");
...
...
...
React Client Hooks
The hooks cover several areas of the Ethereum blockchain and can be used for Layer Two chains as well.
You will need to specify the BACKEND URL + ENDPOINT as the Server URL in one of the parameter values to be passed in each of the client hooks you will be using.
For local development, you will need to specify the full localhost address (http://localhost:PORT/ENDPOINT).
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 { useFetchENSAddressLookup } from '../crypto_hooks/ens/useFetchENSAddressLookup'; // Import hook
// Incorporating the ENS to Address Client Hook.. using Vitalik Buterin's address ;)
// Server URL + Endpoint
// Example uses local development
const ENSToAddressPage: FC = () => {
const addressInformation = useFetchENSAddressLookup('vitalik.eth', 'http://localhost:5000/additional-address-to-ens-information');
// 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.
The following table highlights the 30 different client hooks and their 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: