The Somnia mission is to enable the development of mass-consumer real-time applications. The Somnia Network allows developers to build a unique experience by implementing Smart Contract Wallets with gasless transactions via Account Abstraction (ERC-4337). In this tutorial, we'll use the to:
Connect Smart Wallets (Account Abstraction)
Read Wallet Balance
Send STT Tokens
Pre-requisites
Before we start, ensure you have:
Basic knowledge of React
A account & Client ID
Node.js & npm installed
Install Dependencies
Run the following command to set up your project:
npx create-next-app@latest somnia-thirdweb
cd somnia-thirdweb
npm install thirdweb ethers dotenv
This installs:
thirdweb → The Thirdweb React SDK.
ethers → To interact with blockchain transactions.
dotenv → To securely store API keys.
Define the Somnia Network
Since Somnia is not preconfigured in Thirdweb, we manually define the network. Create a chain.ts file and add the following:
The Thirdweb client allows the app to communicate with the blockchain. Create a client.ts file and add:
import { createThirdwebClient } from "thirdweb";
export const client = createThirdwebClient({
clientId: process.env.NEXT_PUBLIC_THIRDWEB_CLIENT_ID as string, // Replace with your actual Client ID
});
Get your Client ID: Register at thirdweb.com/dashboard. 💡
The useActiveAccount hook allows us to detect the connected Smart Wallet Account. We use ConnectButton to handle authentication and connection to the blockchain.
This button will connect the user's Smart Contract Wallet and authenticate the user with Thirdweb. After connection, it will also display the wallet address.
To show the connected address, we add the following UI component:
The useSendTransaction hook is used to send STT tokens to another address. The function sendTokens will check that the Smart Account is connected and then send 0.01 STT tokens to a recipient address. First, copy your Smart Wallet Address and request for Tokens on Discord in the dev-chat, you can also Transfer some from your EOA.
Log transaction success or failure messages to the console.
The button UI provides a clear interaction for sending STT tokens. The button shows a loading state (Sending...) when the transaction is pending and displays a success or failure message once the transaction is complete.
The full implementation includes wallet connection, balance retrieval, and token transfer.
Conclusion
Congratulations! 🎉 You have successfully connected a smart contract wallet using Thirdweb. Read wallet balances and Transferred STT tokens on Somnia Testnet. You can explore additional features such as gasless transactions, NFT integration, and DeFi applications.