/** * Smart Accounts React Example * * Complete React example demonstrating Smart Accounts Kit integration */ import React, { useState, useEffect } from 'react'; import { SmartAccountsKit } from '@metamask/smart-accounts-kit'; import { ethers } from 'ethers'; import './App.css'; // Load configuration const config = require('../../config/smart-accounts-config.json'); interface SmartAccount { address: string; owner: string; } interface Delegation { target: string; active: boolean; expiry: number; permissions: string[]; } function App() { const [provider, setProvider] = useState(null); const [signer, setSigner] = useState(null); const [userAddress, setUserAddress] = useState(''); const [smartAccount, setSmartAccount] = useState(null); const [smartAccountsKit, setSmartAccountsKit] = useState(null); const [delegations, setDelegations] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // Initialize MetaMask connection useEffect(() => { if (window.ethereum) { const initProvider = new ethers.BrowserProvider(window.ethereum); setProvider(initProvider); // Initialize Smart Accounts Kit const kit = new SmartAccountsKit({ chainId: config.chainId, rpcUrl: config.rpcUrl, entryPointAddress: config.entryPointAddress, accountFactoryAddress: config.accountFactoryAddress, paymasterAddress: config.paymasterAddress || undefined, }); setSmartAccountsKit(kit); } else { setError('MetaMask is not installed'); } }, []); // Connect to MetaMask const connectWallet = async () => { if (!provider) return; try { setLoading(true); setError(null); const accounts = await provider.send('eth_requestAccounts', []); const signer = await provider.getSigner(); const address = await signer.getAddress(); setSigner(signer); setUserAddress(address); } catch (err: any) { setError(err.message || 'Failed to connect wallet'); } finally { setLoading(false); } }; // Create Smart Account const createSmartAccount = async () => { if (!smartAccountsKit || !userAddress) return; try { setLoading(true); setError(null); const account = await smartAccountsKit.createAccount({ owner: userAddress, }); setSmartAccount({ address: account.address, owner: userAddress, }); } catch (err: any) { setError(err.message || 'Failed to create smart account'); } finally { setLoading(false); } }; // Request Delegation const requestDelegation = async (target: string) => { if (!smartAccountsKit || !smartAccount) return; try { setLoading(true); setError(null); const delegation = await smartAccountsKit.requestDelegation({ target: target, permissions: ['execute_transactions', 'batch_operations'], expiry: Date.now() + 86400000, // 24 hours }); if (delegation.approved) { // Refresh delegations await loadDelegations(); } } catch (err: any) { setError(err.message || 'Failed to request delegation'); } finally { setLoading(false); } }; // Load Delegations const loadDelegations = async () => { if (!smartAccountsKit || !smartAccount) return; try { // This would typically fetch from your backend or contract // For demo purposes, we'll use a placeholder const status = await smartAccountsKit.getDelegationStatus({ target: '0x...', // Replace with actual target account: smartAccount.address, }); // Update delegations state // Implementation depends on your delegation storage } catch (err: any) { console.error('Failed to load delegations:', err); } }; return (

Smart Accounts Example

ChainID 138 - SMOM-DBIS-138

{error && (
Error: {error}
)} {!userAddress ? (

Connect Wallet

) : ( <>

Wallet Connected

Address: {userAddress}

{!smartAccount ? (

Create Smart Account

) : ( <>

Smart Account

Address: {smartAccount.address}

Owner: {smartAccount.owner}

Delegations

{delegations.length > 0 && (
    {delegations.map((delegation, index) => (
  • Target: {delegation.target}
    Active: {delegation.active ? 'Yes' : 'No'}
    Expires: {new Date(delegation.expiry).toLocaleString()}
  • ))}
)}
)} )}
); } export default App;