Skip to main content
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:
  1. Ethers.js - A complete and compact library for interacting with EVM blockchains. Known for its simplicity and extensive functionality.
  2. Viem - A lightweight and modular TypeScript interface for Ethereum
  3. 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.
We’ll implement the same functionality using each library so you can compare their approaches and choose the one that best fits your development style.
Deploy to Testnet FirstIt is highly recommended that you deploy to testnet (atlantic-2) first and verify everything works as expected before committing to mainnet. Doing so helps you catch bugs early, avoid unnecessary gas costs, and keep your users safe.

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:
This command creates a new folder with a React project using TypeScript. Open 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 — for example with Hardhat or Foundry, or generate one with the contract wizard (see also deploy & verify) — 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 Assets
Let’s create a shared constants file for our project:
src/shared/constants.ts

Option 1: Ethers.js Implementation

Install ethers.js first:
Let’s start with the Ethers.js implementation:
  • 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:
Now implement the Viem interface:
src/components/ViemInterface.tsx

Option 3: Wagmi Implementation

Install Wagmi and configure it:
First, let’s create a Wagmi configuration file:
src/wagmi.ts
Now implement the Wagmi interface:
src/components/WagmiInterface.tsx

Updating the Main App to Display all Implementations

Now update your App.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 like Buffer. Add these polyfills to your project:
src/main.tsx

Running the Application

To see your app in action, run:
This will start a local development server, at http://localhost:5173. You can toggle between the different library implementations to see how each one works. To build your application, run:
Then preview the production build locally:
This serves the built app at http://localhost:4173 so you can verify it before deploying. To go live, deploy the contents of the dist directory to any static hosting provider.