- ethers.js: A complete and compact library for interacting with EVM blockchains. It is known for its simplicity and extensive functionality.
- viem: A lightweight and modular TypeScript interface for Ethereum.
- wagmi: A React hooks library, built on top of viem, that simplifies wallet connection and interaction. It has hooks that modern frontend libraries and frameworks can use to interact with Ethereum wallets and contracts.
When to use each library
ethers.js
ethers.js is ideal for developers who want a complete, battle-tested library with a straightforward API design. It works well for both simple and complex dApps, especially when you do not use React or you need custom state management.- Pros:
- Complete and easy-to-use API
- Well documented, with a large community
- Works well with both TypeScript and JavaScript
- All-in-one solution for wallet connection and contract interaction
- Cons:
- Larger bundle size than viem
- Not designed specifically for integration with React hooks
viem
viem suits developers who want fine-grained control over their blockchain interactions and prefer a modular, lightweight approach. It is a good choice when bundle size matters and when you have specific requirements for how contract interactions should work.- Pros:
- Lightweight and modular
- Excellent TypeScript support with better type safety
- Lower-level API that gives more control
- Smaller bundle size
- Cons:
- Steeper learning curve
- Requires more boilerplate code for some operations
- Requires separate handling for wallet connection and contract interactions
wagmi
wagmi is the best choice for React developers who build dApps and want to use React’s state management capabilities. Its hooks hide much of the complexity of blockchain interactions. This makes it easy to build reactive UIs that respond to chain state.- Pros:
- React-specific hooks
- Handles complex state management for you
- Built on top of viem, so it shares the benefits of viem
- Convenient caching and automatic refreshing of contract data
- Cons:
- Only works with React
- Adds another dependency layer
- Opinionated about how you should manage data in your app
Requirements
Before you start, make sure that you have:- Node.js and npm installed
- A wallet from the list of Sei wallets
Creating a React project
Create a new React project with Vite’s TypeScript template:sei-token-interface in your preferred IDE.
This tutorial uses TypeScript. If you do not use TypeScript, you can adjust the code by removing the types.
Project structure
For clarity, each library implementation has its own component. First, set up the project structure:Defining the ERC20 contract details
First, deploy your ERC20 token contract. For example, use Hardhat or Foundry, or generate a contract with the contract wizard (see also deploy and verify). Then replace
TOKEN_CONTRACT_ADDRESS in the constants file with the address of your deployed contract. Also update the RPC URL in the Sei chain configuration. For a list of existing ERC20 contracts on Sei Mainnet, see Sei Assets.src/shared/constants.ts
Option 1: ethers.js implementation
Install ethers.js first:- Checks for any EVM-compatible wallet extension.
- Connects to Sei Mainnet through the connected wallet, with the ethers.js
BrowserProvider. - Creates an ethers.js contract instance with the signer from the wallet. It stores the instance in the contract state for later use.
src/components/EthersInterface.tsx
Option 2: viem implementation
Install viem first:src/components/ViemInterface.tsx
Option 3: wagmi implementation
Install wagmi and configure it:src/wagmi.ts
src/components/WagmiInterface.tsx
Updating the main app to display all implementations
Now update yourApp.tsx to include all three interface options:
src/App.tsx
Polyfills for browser environment
When you develop frontend applications for the blockchain, you might need polyfills for Node.js-specific features, such asBuffer. Add these polyfills to your project:
src/main.tsx
Running the application
To start your app, run this command:http://localhost:5173. You can switch between the library implementations to see how each one works.
To build your application, run this command:
http://localhost:4173, so you can verify it before you deploy. To go live, deploy the contents of the dist directory to any static hosting provider.