Gasless Transactions with Thirdw

This tutorial demonstrates how to build a gasless NFT minting application on Somnia using Thirdweb's Account Abstraction infrastructure. Users can mint NFTs without holding STT tokens by using Smart Accounts with sponsored transactions.

Prerequisites

  • Basic knowledge of React and Next.js

  • A Thirdweb account and API key

  • A deployed ERC721 NFT Smart Contract on Somnia

What You'll Build

A web application where users can:

  • Connect using email or social accounts (via in-app wallets)

  • Mint NFTs without paying gas fees

  • View their NFT balance

  • Experience seamless Web3 interactions

Create a Next.js Project

npx create-next-app@latest somnia-gasless-nft --typescript --tailwind --app
cd somnia-gasless-nft

Install Thirdweb SDK

Set Up Environment Variables

Create a .env.local file in your project root:

Get your Client ID from the Thirdweb Dashboard.

Deploy a Simple NFT Contract

If you haven't already, deploy this simple ERC721 contract on Somnia.

SimpleNFT.sol

Create Constants Configuration

Now we need to set up our configuration file that will handle all the blockchain connections and smart account setup. This file will:

  • Initialize the Thirdweb client with your API key

  • Set up both traditional wallet and in-app wallet options

  • Define the smart account infrastructure

Create a new folder called constants in your project root, then create constants/index.ts.

constants

Create the Main Minting Page

This is the main component where users will interact with your NFT minting application. We'll break this into three parts: imports and setup, the component logic, and the UI rendering.

Imports and Component Setup

First, let's set up our imports and initialize the component. Create app/page.tsx.

page.tsx

These imports provide:

  • ERC721 Extensions: Functions to read NFT data (balance and total supply)

  • React Components: Pre-built UI components for wallet connection and transactions

  • Hooks: To access the connected account and read blockchain data

  • All our constants from the previous step

Component Logic and Data Fetching

Now let's add the component logic that handles wallet connections and reads blockchain data:

page.tsx
  • useActiveAccount(): Gets the connected wallet/smart account

  • useState: Manages transaction status messages

  • useReadContract: Automatically fetches and updates blockchain data

The queries will re-fetch automatically when accounts change or transactions complete

User Interface

Now let's build the complete UI that users will interact with

page.tsx

Understanding the UI Components

ConnectButton: This component handles both connection methods:

  • In-App Wallets: Users can sign in with email, Google, Apple, etc.

  • Traditional Wallets: Users can connect MetaMask, WalletConnect, etc.

Both methods automatically create smart accounts for gasless transactions

TransactionButton: This component prepares and sends the transaction automatically. Handling all wallet interactions and confirmations, and provides callbacks for different transaction states. It also shows loading states automatically

Error Handling: The component provides user-friendly error messages instead of technical blockchain errors, improving the user experience.

Update the Layout

The layout file wraps your entire application and is where we set up the Thirdweb provider. This provider is essential as it:

  • Manages wallet connections across your app

  • Handles blockchain interactions

  • Provides React hooks for reading blockchain data

  • Manages transaction states

Update app/layout.tsx:

layout.tsx

Conclusion

You've successfully built a gasless NFT minting application on Somnia! This implementation leverages Thirdweb's Account Abstraction to provide a seamless Web3 experience where users can interact with blockchain without holding native tokens.

For more advanced features and updates on Somnia support, check:

Resources

Last updated