Base Account Demo React 18.2.0 • TypeScript 5.0.0 • Vite 4.4.0 • Viem 2.41.2
A comprehensive demo application showcasing Base Account integration with React
- Features
- Quick Start
- Installation
- Configuration
- Usage
- Development
- Testing
- Deployment
- API Reference
- Contributing
- License
- Support
- Sign In with Base: Seamless authentication using Base Account
- Account Creation: Create new Base Accounts with sub-account support
- Network Switching: Toggle between Base Sepolia Testnet and Base Mainnet
- Secure Wallet Integration: EIP-1193 compliant provider integration
- USDC Payments: Send USDC payments on Base network
- Real-time Status: Live payment status tracking with auto-updates
- Gasless Transactions: Paymaster integration for sponsored transactions
- Multi-network Support: Testnet and Mainnet payment processing
- Modern UI: Built with shadcn/ui and Tailwind CSS
- Responsive Design: Mobile-first approach with adaptive layouts
- Dark Theme: Consistent dark theme matching Base branding
- Loading States: Comprehensive loading and error handling
- Toast Notifications: User-friendly feedback system
- TypeScript: Full type safety throughout the application
- Hot Reload: Fast development with Vite
- ESLint: Code quality and consistency
- Viem Integration: Ethereum account management and cryptographic operations
- Modular Architecture: Clean, maintainable code structure
Get up and running in less than 5 minutes!
# Clone the repository
git clone https://github.com/your-username/base-account-demo.git
cd base-account-demo
# Install dependencies
pnpm install
# Start development server
pnpm devVisit http://localhost:5173 and start exploring Base Account features!
graph TB
A[User Interface] --> B[React Application]
B --> C[Base Account SDK]
C --> D[Base Blockchain]
B --> E[Viem Library]
E --> F[Ethereum Accounts]
B --> G[shadcn/ui Components]
B --> H[Tailwind CSS]
D --> I[Base Sepolia Testnet]
D --> J[Base Mainnet]
C --> K[Sub-Accounts]
C --> L[Universal Accounts]
C --> M[Paymaster Service]
N[Wallet Integration] --> C
O[Payment Processing] --> C
subgraph "Frontend Layer"
B
G
H
end
subgraph "Blockchain Layer"
D
I
J
K
L
M
end
subgraph "Integration Layer"
C
E
N
O
end
The Base Account Demo follows a layered architecture:
- Frontend Layer: React application with modern UI components
- Integration Layer: Base Account SDK and Viem for blockchain interactions
- Blockchain Layer: Base network with sub-accounts and paymaster services
- Node.js: Version 18.0.0 or higher
- Package Manager: pnpm (recommended), npm, or yarn
- Git: For cloning the repository
-
Clone the Repository
git clone https://github.com/your-username/base-account-demo.git cd base-account-demo -
Install Dependencies
# Using pnpm (recommended) pnpm install # Or using npm npm install # Or using yarn yarn install
-
Environment Setup (Optional)
# Copy environment template cp .env.example .env.local # Edit environment variables nano .env.local
-
Start Development Server
# Using pnpm pnpm dev # Or using npm npm run dev # Or using yarn yarn dev
The application will be available at http://localhost:5173.
Create a .env.local file in the root directory:
# Base Account Configuration
VITE_APP_NAME=Base Account Demo
VITE_APP_LOGO_URL=https://base.org/logo.png
# Network Configuration
VITE_DEFAULT_NETWORK=testnet # 'testnet' or 'mainnet'
# Payment Configuration
VITE_RECIPIENT_ADDRESS=0xc5983e0b551a7c60d62177cccadf199b9eeac54b
# Optional: Custom Paymaster URLs
VITE_PAYMASTER_BASE_SEPOLIA=https://paymaster.base-sepolia.org/api/v1/sponsor
VITE_PAYMASTER_BASE=https://paymaster.base.org/api/v1/sponsorThe Base Account SDK is configured in src/pages/Index.tsx:
const sdk = createBaseAccountSDK({
appName: 'Base Account Demo',
appLogoUrl: 'https://base.org/logo.png',
appChainIds: [isTestnet ? 84532 : 8453], // Dynamic network selection
subAccounts: {
creation: 'manual',
defaultAccount: 'sub',
funding: 'spend-permissions',
toOwnerAccount: async () => ({ account: ownerAccount }),
},
});Viem (^2.41.2) is used for Ethereum account management and cryptographic operations:
import { privateKeyToAccount } from 'viem/accounts';
// Create demo owner account for sub-account signing
const ownerAccount = privateKeyToAccount(process.env.DEMO_PRIVATE_KEY || 'your-private-key-here');
// Account properties available:
console.log(ownerAccount.address); // '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
console.log(ownerAccount.publicKey); // Public key for signing operationsViem provides:
- Account Management: Local account creation and management
- Cryptographic Operations: Secure signing and verification
- Type Safety: Full TypeScript support for Ethereum operations
- Performance: Optimized for modern Ethereum interactions
- Select Network: Choose between Testnet and Mainnet
- Connect Account: Click "Sign In with Base" or "Create a Base Account"
- Make Payment: Enter amount and send USDC
- Track Status: Monitor payment progress in real-time
// Manual sub-account creation
await sdk.subAccount.create({
type: 'create',
keys: [{
type: 'p256',
publicKey: ownerAccount.publicKey,
}],
});// Send USDC payment
const { id } = await pay({
amount: '1.0',
to: RECIPIENT_ADDRESS,
testnet: true,
});
// Check payment status
const { status } = await getPaymentStatus({
id,
testnet: true,
});// Toggle between networks
const [isTestnet, setIsTestnet] = useState(true);
// SDK automatically updates chain IDs
const chainId = isTestnet ? '0x14a34' : '0x2105';src/
├── components/ # Reusable UI components
│ ├── ui/ # shadcn/ui components
│ ├── BaseLogo.tsx # Base logo component
│ ├── AmountSelector.tsx
│ ├── PaymentStatus.tsx
│ └── AddressDisplay.tsx
├── pages/ # Page components
│ └── Index.tsx # Main application page
├── lib/ # Utility functions
│ └── utils.ts
├── hooks/ # Custom React hooks
└── types/ # TypeScript type definitions
# Start development server
pnpm dev
# Build for production
pnpm build
# Preview production build
pnpm preview
# Run linter
pnpm lint
# Type checking
pnpm type-checkThis project uses:
- ESLint: Code linting and formatting
- TypeScript: Static type checking
- Prettier: Code formatting (via ESLint)
- Create Component: Add new components in
src/components/ - Update Types: Define types in component files or
src/types/ - Add Styling: Use Tailwind CSS classes or custom CSS
- Test Integration: Test with both testnet and mainnet
# Run all tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run tests with coverage
pnpm test:coveragesrc/
├── __tests__/ # Unit tests
├── __mocks__/ # Mock data and functions
└── test-utils/ # Testing utilities
- Unit Tests: Component and utility function testing
- Integration Tests: SDK integration and API calls
- E2E Tests: Full user flow testing with Playwright
# Build the application
pnpm build
# The built files will be in the `dist` directory# Install Vercel CLI
npm i -g vercel
# Deploy
vercel
# For production deployment
vercel --prod# Build command: pnpm build
# Publish directory: distFROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 80
CMD ["npm", "run", "preview"]Ensure these environment variables are set in your deployment platform:
NODE_ENV=production- Custom configuration variables as needed
Creates a Base Account SDK instance.
Parameters:
options.appName(string): Application nameoptions.appLogoUrl(string): Application logo URLoptions.appChainIds(number[]): Supported chain IDsoptions.subAccounts(object): Sub-account configuration
Returns: BaseAccountSDK instance
Initiates a USDC payment.
Parameters:
options.amount(string): Payment amountoptions.to(string): Recipient addressoptions.testnet(boolean): Use testnet or mainnet
Returns: Promise with payment ID
Retrieves payment status.
Parameters:
options.id(string): Payment IDoptions.testnet(boolean): Network flag
Returns: Promise with status information
Base Account sign-in button component.
USDC payment button component.
Base logo component with customizable sizing.
Viem (^2.41.2) is a TypeScript interface for Ethereum that provides low-level utilities for interacting with Ethereum.
Creates a local account from a private key.
Parameters:
privateKey(string): Private key in hex format (with or without 0x prefix)
Returns: LocalAccount object with address, publicKey, and signing methods
const account = privateKeyToAccount('your-private-key-here');
console.log(account.address); // '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'- Account Creation: Generate and manage Ethereum accounts
- Cryptographic Operations: Secure signing for sub-account transactions
- Type Safety: Full TypeScript support for all operations
- Performance: Optimized for modern Ethereum applications
We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
- Follow the existing code style
- Write clear, concise commit messages
- Add tests for new features
- Update documentation as needed
- Ensure all tests pass
- Use GitHub Issues for bug reports
- Provide detailed reproduction steps
- Include browser/console logs if applicable
- Specify your environment (OS, browser, Node version)
This project is licensed under the MIT License - see the LICENSE file for details.
Built with ❤️ for the Base ecosystem