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.
- 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
npx create-nextjs-sui-dapp-template
Follow the prompts to set up your project:
- Enter your project name
- Choose Sui network (devnet/testnet/mainnet)
- Configure environment variables
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
.
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
The template automatically handles network switching based on NEXT_PUBLIC_NETWORK
:
import { getNetworkVariables, network } from '@/contracts'
// Automatically uses correct network
const variables = getNetworkVariables()
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()
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
}
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"
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()
βββ 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
- 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
- 60% code reduction in transaction hooks
- Centralized callback management
- useCallback optimizations for React performance
- Async transaction support with proper error handling
- Complete TypeScript support throughout
- Type-safe decoder system for query contract getter function data
- Compile-time error checking
- IDE intelligent suggestions
- Global decoder registration for Next.js apps
- Comprehensive examples and documentation
- Modular architecture with clear separation of concerns
- Backward compatibility with existing code
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
If you prefer manual control over the update process:
-
Backup your project:
cp -r my-project my-project-backup
-
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
-
Copy core files from the latest template:
utils/sui-query/
entire directoryhooks/useBetterTx.ts
utils/registerDecoders.ts
utils/assetsHelpers.ts
utils/index.ts
-
Update imports:
// Old import { ProjectDecoders } from '@/utils' // New import { createTypeSafeDecoders, addProjectDecoder } from '@/utils'
-
Initialize decoders in your app:
// In app/providers.tsx import { initializeAllDecoders } from '@/utils/registerDecoders' useEffect(() => { initializeAllDecoders() }, [])
-
Use new query system:
import { QueryBuilder } from '@/utils' const query = QueryBuilder.withArgs(module, function, argsBuilder, decoder)
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 |
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
We welcome contributions! Please feel free to submit Pull Requests.
This project is licensed under the MIT License.
For issues and questions:
- Check the examples directory
- Review the sui-query README
- Open an issue on GitHub
Built with β€οΈ for the Sui ecosystem