Skip to main content
To build the frontend of a dApp on Sei EVM, you connect to wallets and interact with the blockchain through RPC endpoints. You also sign and broadcast transactions. This tutorial shows how to build a simple ERC20 token interface with three popular libraries:
  1. ethers.js: A complete and compact library for interacting with EVM blockchains. It is 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. It has hooks that modern frontend libraries and frameworks can use to interact with Ethereum wallets and contracts.
The tutorial implements the same functionality with each library. You can then compare their approaches and choose the one that best fits your development style.
Deploy to Sei Testnet firstIt is highly recommended that you deploy to Sei Testnet first. Verify that everything works as expected before you commit to Sei Mainnet. This 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 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:

Creating a React project

Create a new React project with Vite’s TypeScript template:
This command creates a new folder with a TypeScript React project. Open 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.
Create a shared constants file for the project:
src/shared/constants.ts

Option 1: ethers.js implementation

Install ethers.js first:
Start with the ethers.js implementation. The component does these things:
  • 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:
Now implement the viem interface:
src/components/ViemInterface.tsx

Option 3: wagmi implementation

Install wagmi and configure it:
First, 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 you develop frontend applications for the blockchain, you might need polyfills for Node.js-specific features, such as Buffer. Add these polyfills to your project:
src/main.tsx

Running the application

To start your app, run this command:
This command starts a local development server at http://localhost:5173. You can switch between the library implementations to see how each one works. To build your application, run this command:
Then preview the production build locally:
This command serves the built app at 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.