Skip to content

hoh-zone/Nextjs-Sui-Dapp-Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

70 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Next.js Sui dApp Template

English | δΈ­ζ–‡

A comprehensive Next.js template for building Sui blockchain dApps with advanced features including optimized transaction hooks, type-safe query system, and asset management.

✨ Features

  • Next.js 15.4.1 - Latest stable version with App Router
  • πŸ’Ž Sui SDK Integration - Full @mysten/sui support
  • πŸ”Œ dApp Kit - Complete wallet integration with @mysten/dapp-kit
  • πŸ’« Enoki Sponsorship - Sponsored transactions with @mysten/enoki
  • 🎨 Modern UI - Tailwind CSS with shadcn/ui components
  • πŸ“¦ TypeScript - Full type safety throughout
  • πŸ”„ React Query - Optimized data fetching with @tanstack/react-query
  • πŸ› οΈ Advanced Hooks - Optimized transaction hooks with 60% code reduction
  • πŸ” Type-Safe Query System - Comprehensive decoder system for query Contract Getter Function
  • Asset Management - Built-in asset categorization and balance calculation
  • 🌐 Multi-Network Support - Testnet and mainnet configuration
  • πŸ“± Responsive Design - Mobile-first approach

πŸš€ Quick Start

Installation

npx create-nextjs-sui-dapp-template

Follow the prompts to set up your project:

  1. Enter your project name
  2. Choose Sui network (devnet/testnet/mainnet)
  3. Configure environment variables

Development

cd your-project-name

# Install dependencies (recommended: use Bun for faster installation)
bun install
# or
npm install

# Start development server
bun run dev
# or
npm run dev

Your Sui dApp will be running at http://localhost:3000.

βš™οΈ Configuration

Environment Variables

Copy .env.example to .env and configure:

NEXT_PUBLIC_NETWORK=testnet
ENOKI_SECRET_KEY=your_enoki_api_key  # Optional: for sponsored transactions
MAINNET_PACKAGE_ID=your_mainnet_package_id
TESTNET_PACKAGE_ID=your_testnet_package_id

Network Configuration

The template automatically handles network switching based on NEXT_PUBLIC_NETWORK:

import { getNetworkVariables, network } from '@/contracts'

// Automatically uses correct network
const variables = getNetworkVariables()

πŸ”§ Advanced Features

1. Optimized Transaction Hooks

60% code reduction with centralized callback management:

import { useBetterSignAndExecuteTransaction } from '@/hooks/useBetterTx'
import { createBetterTxFactory } from '@/contracts'

// Create transaction function
const borrowTX = createBetterTxFactory<BorrowRequest>((tx, networkVariables, params) => {
    tx.moveCall({
        package: networkVariables.package,
        module: "lending",
        function: "borrow",
        arguments: [
            tx.object(params.collateral),
            tx.pure.u64(params.amount)
        ]
    })
    return tx
})

// Use in component
const { handleSignAndExecuteTransaction, isLoading } = useBetterSignAndExecuteTransaction({
    tx: borrowTX
})

// Execute with full callback chain
const result = await handleSignAndExecuteTransaction({
    collateral: "0x...",
    amount: 1000
})
.beforeExecute(async () => {
    // Validation logic
    return true
})
.onSuccess((result) => {
    console.log('Success:', result)
})
.onError((error) => {
    console.error('Error:', error)
})
.execute()

2. Type-Safe Query System

Comprehensive decoder system for query contract getter function data;

import { QueryBuilder } from '@/utils'
import { MyProjectDecoders } from './decoders'

// Create type-safe queries
const getUserInfo = QueryBuilder.withArgs<[string], UserInfo>(
    'my_module',
    'get_user_info',
    (tx, userAddress) => [tx.pure.address(userAddress)],
    MyProjectDecoders.UserInfo  // Type-safe decoder
)

// Use query
const userInfo = await getUserInfo('0x123...')
if (userInfo) {
    console.log(userInfo.name, userInfo.balance)  // Type-safe access
}

3. Asset Management

Built-in asset categorization and balance calculation:

import { getUserProfile, categorizeSuiObjects } from '@/utils'

// Get user's categorized assets
const userAssets = await getUserProfile(userAddress)

// Access categorized coins and objects
console.log(userAssets.coins)     // All user's coins by type
console.log(userAssets.objects)   // All user's objects by type

// Calculate total balance
const totalBalance = calculateTotalBalance(userAssets.coins['0x2::sui::SUI'])
console.log(formatBalance(totalBalance))  // "1.234567890"

4. Sponsored Transactions

Optional Enoki integration for sponsored transactions:

import { useBetterSignAndExecuteTransactionWithSponsor } from '@/hooks/useBetterTx'

const { handleSponsoredTransaction } = useBetterSignAndExecuteTransactionWithSponsor({
    tx: myTransaction
})

// Execute sponsored transaction
const result = await handleSponsoredTransaction(params)
    .onSuccess((result) => {
        console.log('Sponsored transaction successful')
    })
    .execute()

πŸ“ Project Structure

β”œβ”€β”€ app/                    # Next.js App Router
β”‚   β”œβ”€β”€ api/               # API routes (Sui client, sponsored transactions)
β”‚   β”œβ”€β”€ layout.tsx         # Root layout with providers
β”‚   β”œβ”€β”€ page.tsx           # Home page with asset display
β”‚   └── providers.tsx      # Global providers with decoder initialization
β”œβ”€β”€ contracts/             # Contract configuration and queries
β”‚   β”œβ”€β”€ config.ts          # Network-specific contract addresses
β”‚   β”œβ”€β”€ index.ts           # Network configuration
β”‚   └── query.ts           # Contract query functions
β”œβ”€β”€ hooks/                 # Custom React hooks
β”‚   └── useBetterTx.ts     # Optimized transaction hooks
β”œβ”€β”€ utils/                 # Utility functions
β”‚   β”œβ”€β”€ assetsHelpers.ts   # Asset categorization and balance calculation
β”‚   β”œβ”€β”€ sui-query/         # Type-safe query system
β”‚   β”‚   β”œβ”€β”€ decoders.ts    # Core decoder system
β”‚   β”‚   β”œβ”€β”€ query.ts       # Query functions
β”‚   β”‚   └── index.ts       # Unified exports
β”‚   β”œβ”€β”€ registerDecoders.ts # Global decoder registration
β”‚   └── index.ts           # Main exports
β”œβ”€β”€ examples/              # Example implementations
β”‚   β”œβ”€β”€ projectDecoders.example.ts
β”‚   β”œβ”€β”€ nextjs-initialization.example.tsx
β”‚   └── typeSafeQueryExamples.ts
β”œβ”€β”€ types/                 # TypeScript type definitions
└── public/                # Static assets

πŸ› οΈ Technical Stack

  • Framework: Next.js 15.4.1 with App Router
  • Blockchain: Sui (@mysten/sui v1.36.0)
  • Wallet Integration: @mysten/dapp-kit v0.16.15
  • Sponsored Transactions: @mysten/enoki v0.6.20
  • State Management: @tanstack/react-query v5.83.0
  • Styling: Tailwind CSS with shadcn/ui
  • Type Safety: TypeScript 5.8.3
  • Code Quality: ESLint with Next.js config
  • Package Manager: Bun (recommended) or npm

🎯 Key Improvements

Performance Optimizations

  • 60% code reduction in transaction hooks
  • Centralized callback management
  • useCallback optimizations for React performance
  • Async transaction support with proper error handling

Type Safety

  • Complete TypeScript support throughout
  • Type-safe decoder system for query contract getter function data
  • Compile-time error checking
  • IDE intelligent suggestions

Developer Experience

  • Global decoder registration for Next.js apps
  • Comprehensive examples and documentation
  • Modular architecture with clear separation of concerns
  • Backward compatibility with existing code

πŸ”„ Migration Guide

Automatic Update (Recommended)

The easiest way to update your existing project is using our automated update script:

# Install or update the template tool globally
npm install -g create-nextjs-sui-dapp-template@latest

# Run the update script in your project directory
npx update-nextjs-sui-dapp-template

# Or if you have it installed globally
update-nextjs-sui-dapp-template

The update script will:

  • βœ… Automatically create a backup of your project
  • βœ… Allow you to selectively update framework components
  • βœ… Update dependency versions
  • βœ… Provide detailed next steps guidance

Manual Migration from Previous Versions

If you prefer manual control over the update process:

  1. Backup your project:

    cp -r my-project my-project-backup
  2. Update core dependencies:

    npm install @mysten/dapp-kit@^0.16.15 @mysten/enoki@^0.6.20 @mysten/sui@^1.36.0 @tanstack/react-query@^5.83.0 next@^15.4.1
  3. Copy core files from the latest template:

    • utils/sui-query/ entire directory
    • hooks/useBetterTx.ts
    • utils/registerDecoders.ts
    • utils/assetsHelpers.ts
    • utils/index.ts
  4. Update imports:

    // Old
    import { ProjectDecoders } from '@/utils'
    
    // New
    import { createTypeSafeDecoders, addProjectDecoder } from '@/utils'
  5. Initialize decoders in your app:

    // In app/providers.tsx
    import { initializeAllDecoders } from '@/utils/registerDecoders'
    
    useEffect(() => {
        initializeAllDecoders()
    }, [])
  6. Use new query system:

    import { QueryBuilder } from '@/utils'
    
    const query = QueryBuilder.withArgs(module, function, argsBuilder, decoder)

Version Comparison

Feature Old Version New Version (v1.2.0)
Transaction Hooks Basic version 60% code reduction, centralized callback management
Query System Basic queries Type-safe decoder system
Asset Management Manual handling Built-in categorization and balance calculation
Decoder Registration Manual management Global automatic registration mechanism
Next.js Version 14.x 15.4.1 Latest version

Update Verification Checklist

After updating, please verify:

  • All import statements are correct
  • Decoder initialization runs properly
  • Transaction functionality works normally
  • Query functions return correct data
  • Assets display correctly
  • No TypeScript errors

🀝 Contributing

We welcome contributions! Please feel free to submit Pull Requests.

πŸ“„ License

This project is licensed under the MIT License.

πŸ†˜ Support

For issues and questions:

  1. Check the examples directory
  2. Review the sui-query README
  3. Open an issue on GitHub

Built with ❀️ for the Sui ecosystem

About

A generator for Sui dApp projects with Nextjs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors 8