@@ -4,7 +4,6 @@ import { usePrivy, useWallets } from "@privy-io/react-auth";
4
4
import Head from "next/head" ;
5
5
import { createWalletClient , custom , Hex } from "viem" ;
6
6
import { useSignAuthorization } from "@privy-io/react-auth" ;
7
- // import { sepolia } from "viem/chains";
8
7
import {
9
8
AuthorizationRequest ,
10
9
SmartAccountSigner ,
@@ -21,6 +20,10 @@ export default function DashboardPage() {
21
20
const router = useRouter ( ) ;
22
21
const { ready, authenticated, logout } = usePrivy ( ) ;
23
22
const { client } = usePrivy7702 ( ) ;
23
+ const { wallets } = useWallets ( ) ;
24
+ const embeddedWallet = wallets . find ( ( x ) => x . walletClientType === "privy" ) ;
25
+ const [ isLoading , setIsLoading ] = useState ( false ) ;
26
+ const [ transactionHash , setTransactionHash ] = useState < string | null > ( null ) ;
24
27
25
28
useEffect ( ( ) => {
26
29
if ( ready && ! authenticated ) {
@@ -33,41 +36,111 @@ export default function DashboardPage() {
33
36
console . error ( "No client yet" ) ;
34
37
return ;
35
38
}
36
- // Delegating (if not already) and sending a sponsored user operation
37
- const uoHash = await client . sendUserOperation ( {
38
- uo : {
39
- target : "0xc0ffee254729296a45a3885639AC7E10F9d54979" ,
40
- value : 0n ,
41
- data : "0x0" ,
42
- } ,
43
- } ) ;
44
- const txnHash = await client . waitForUserOperationTransaction ( uoHash ) ;
45
- console . log ( "transaction hash" , txnHash ) ;
39
+ setIsLoading ( true ) ;
40
+ try {
41
+ const uoHash = await client . sendUserOperation ( {
42
+ uo : {
43
+ target : "0xc0ffee254729296a45a3885639AC7E10F9d54979" ,
44
+ value : 0n ,
45
+ data : "0x0" ,
46
+ } ,
47
+ } ) ;
48
+ const txnHash = await client . waitForUserOperationTransaction ( uoHash ) ;
49
+ setTransactionHash ( txnHash ) ;
50
+ console . log ( "transaction hash" , txnHash ) ;
51
+ } catch ( error ) {
52
+ console . error ( "Transaction failed:" , error ) ;
53
+ } finally {
54
+ setIsLoading ( false ) ;
55
+ }
46
56
}
47
57
48
58
return (
49
59
< div >
50
60
< Head >
51
- < title > Privy Auth Demo </ title >
61
+ < title > Alchemy Smart Wallets + EIP-7702 </ title >
52
62
</ Head >
53
63
54
- < main className = "flex flex-col min-h-screen px-4 sm:px-20 py-6 sm:py-10 bg-privy-light- blue" >
64
+ < main className = "min-h-screen px-4 sm:px-20 py-6 sm:py-10 bg-gradient-to-br from- blue-50 to-indigo-100 " >
55
65
{ ready && authenticated ? (
56
- < >
57
- < div className = "flex flex-row justify-between" >
58
- < h1 className = "text-2xl font-semibold" > Privy Auth Demo</ h1 >
66
+ < div className = "max-w-3xl mx-auto" >
67
+ < header className = "flex flex-row justify-between items-center mb-8" >
68
+ < h1 className = "text-3xl font-bold text-indigo-900" >
69
+ Alchemy Smart Wallets + EIP-7702
70
+ </ h1 >
59
71
< button
60
72
onClick = { logout }
61
- className = "text-sm bg-violet-200 hover:text-violet-900 py-2 px-4 rounded-md text-violet-700 "
73
+ className = "text-sm bg-indigo-100 hover:bg-indigo-200 text-indigo-700 font-medium py-2 px-4 rounded-lg transition-colors "
62
74
>
63
75
Logout
64
76
</ button >
65
- </ div >
66
- < div >
67
- < button onClick = { delegateandauth } > delegate</ button >
68
- < br />
69
- </ div >
70
- </ >
77
+ </ header >
78
+
79
+ < section className = "bg-white rounded-xl shadow-lg p-6 mb-8" >
80
+ < p className = "text-gray-700 mb-4" >
81
+ This demo showcases how to upgrade an existing embedded Privy
82
+ EOA to a smart wallet using Alchemy's EIP-7702 support to send
83
+ sponsored transactions from an EOA. Learn more about EIP-7702{ " " }
84
+ < a
85
+ href = "https://www.alchemy.com/docs/wallets/react/using-7702"
86
+ className = "text-indigo-600 hover:underline"
87
+ target = "_blank"
88
+ rel = "noopener noreferrer"
89
+ >
90
+ here
91
+ </ a >
92
+ .
93
+ </ p >
94
+ { embeddedWallet && (
95
+ < div className = "mb-4" >
96
+ < h2 className = "text-lg font-semibold text-gray-900" >
97
+ Embedded EOA Address
98
+ </ h2 >
99
+ < p className = "text-gray-600 font-mono break-all" >
100
+ { embeddedWallet . address }
101
+ </ p >
102
+ </ div >
103
+ ) }
104
+ < button
105
+ onClick = { delegateandauth }
106
+ disabled = { isLoading }
107
+ className = { `w-full py-3 px-4 rounded-lg font-semibold text-white transition-colors ${
108
+ isLoading
109
+ ? "bg-indigo-400 cursor-not-allowed"
110
+ : "bg-indigo-600 hover:bg-indigo-700"
111
+ } `}
112
+ >
113
+ { isLoading
114
+ ? "Processing..."
115
+ : "Upgrade & Send Sponsored Transaction" }
116
+ </ button >
117
+ </ section >
118
+
119
+ { transactionHash && (
120
+ < section className = "bg-green-50 rounded-xl shadow-lg p-6 border border-green-200" >
121
+ < h2 className = "text-lg font-semibold text-green-900 mb-4" >
122
+ Congrats! Sponsored transaction successful!
123
+ </ h2 >
124
+ < p className = "text-green-700 mb-4" >
125
+ You've successfully upgraded your EOA to a smart account and
126
+ sent your first sponsored transaction.{ " " }
127
+ < a
128
+ href = "https://www.alchemy.com/docs/wallets/react/using-7702"
129
+ className = "text-indigo-600 hover:underline"
130
+ target = "_blank"
131
+ rel = "noopener noreferrer"
132
+ >
133
+ Keep building
134
+ </ a >
135
+ .
136
+ </ p >
137
+ < p className = "text-green-700" >
138
+ < strong > Transaction Hash:</ strong > { " " }
139
+ < span className = "font-mono break-all" > { transactionHash } </ span >
140
+ </ p >
141
+ </ section >
142
+ ) }
143
+ </ div >
71
144
) : null }
72
145
</ main >
73
146
</ div >
0 commit comments