Developing the frontend of a dApp on Sei EVM involves connecting to wallets, interacting with the blockchain via RPC endpoints, and signing and broadcasting transactions. This tutorial will demonstrate how to build a simple ERC20 token interface using three popular libraries:Documentation Index
Fetch the complete documentation index at: https://seilabs.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
- Ethers.js - A complete and compact library for interacting with EVM blockchains. 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. Provides hooks for interacting with Ethereum wallets and contracts for use with modern frontend libraries and frameworks.
When to Use Each Library
Ethers.js
Ethers.js is ideal for developers who want a comprehensive, battle-tested library with straightforward API design. It’s great for both simple and complex dApps, especially when you’re not using React or need custom state management.- Pros:
- Comprehensive 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 compared to Viem
- Not specifically designed for React hooks integration
Viem
Viem is perfect for developers who want fine-grained control over their blockchain interactions and appreciate a modular, lightweight approach. It’s 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 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 building dApps who want to leverage React’s state management capabilities. It abstracts away much of the complexity of blockchain interactions via hooks, making it easy to build reactive UIs that respond to chain state.- Pros:
- React-specific hooks for seamless integration
- Handles complex state management for you
- Built on top of Viem, combining its benefits
- Convenient caching and auto-refreshing for contract data
- Cons:
- Only works with React
- Adds another dependency layer
- Opinionated about how data should be managed in your app
Requirements
Before starting, ensure you have:- Node.js & NPM installed
- One of the Sei wallets listed here
Creating a React Project
Start by creating a new React project using Vite’s TypeScript template for streamlined development:sei-token-interface in your favorite IDE.
This tutorial uses TypeScript. If you’re not using TypeScript, you can easily adjust by removing the types.
Project Structure
For clarity, we’ll create separate components for each library implementation. First, let’s set up the project structure:Defining the ERC20 Contract Details
Make sure to deploy your ERC20 token contract first and replace
TOKEN_CONTRACT_ADDRESS in the constants file with your actual deployed contract address, and update the RPC URL in the Sei chain configuration. You can find a list of existing ERC20 contracts on Sei Mainnet here: Sei Assetssrc/shared/constants.ts
Option 1: Ethers.js Implementation
Install ethers.js first:- Checks for any EVM compatible wallet extension.
- Establishes a connection to Sei Mainnet via the connected wallet, using ethers.js BrowserProvider.
- Creates an ethers.js contract instance with the signer from the wallet, setting it 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 developing frontend applications for the blockchain, you might need polyfills for Node.js-specific features likeBuffer. Add these polyfills to your project:
src/main.tsx
Running the Application
To see your app in action, run:http://localhost:5173. You can toggle between the different library implementations to see how each one works.
To build your application, run:
http://localhost:4173. You can toggle between the different library implementations to see how each one works.