@@ -12,6 +12,7 @@ import { ToastContainer } from '../components/Toast';
1212import { ReconnectionModal } from '../components/ReconnectionModal' ;
1313import { SVM_NETWORKS , getNetworkConfig , getDefaultNetworkConfig } from '../config/networks' ;
1414import { ERROR_CATEGORIES } from '../hooks/useToast' ;
15+ import { safeExecute , logError } from '../utils/errorHandling' ;
1516
1617// Maximum number of reconnection attempts
1718const MAX_RECONNECT_ATTEMPTS = 3 ;
@@ -151,40 +152,46 @@ const PhantomWalletProvider = ({ children }) => {
151152 return baseDelay + ( baseDelay * jitter ) ;
152153 } , [ ] ) ;
153154
154- // Detect Phantom wallet
155+ // Detect Phantom wallet with error protection
155156 const detectPhantomWallet = useCallback ( ( ) => {
156- if ( typeof window === 'undefined' ) return null ;
157-
158- // Check for Phantom's Solana provider
159- if ( window . phantom ?. solana ) {
160- return window . phantom . solana ;
161- }
162-
163- // Check for legacy solana provider
164- if ( window . solana ?. isPhantom ) {
165- return window . solana ;
166- }
167-
168- return null ;
157+ return safeExecute ( ( ) => {
158+ if ( typeof window === 'undefined' ) return null ;
159+
160+ // Check for Phantom's Solana provider
161+ if ( window . phantom ?. solana ) {
162+ return window . phantom . solana ;
163+ }
164+
165+ // Check for legacy solana provider
166+ if ( window . solana ?. isPhantom ) {
167+ return window . solana ;
168+ }
169+
170+ return null ;
171+ } , 'detectPhantomWallet' , null ) ;
169172 } , [ ] ) ;
170173
171- // Connect to Phantom wallet
174+ // Connect to Phantom wallet with enhanced error handling
172175 const connect = useCallback ( async ( ) => {
173176 try {
174177 setConnecting ( true ) ;
175178 setError ( null ) ;
176179 setConnectionState ( 'connecting' ) ;
177180
178- const phantomWallet = detectPhantomWallet ( ) ;
181+ const phantomWallet = await detectPhantomWallet ( ) ;
179182
180183 if ( ! phantomWallet ) {
181184 throw new Error ( 'Phantom wallet not found. Please install Phantom browser extension.' ) ;
182185 }
183186
184- // Connect to Phantom
185- const response = await phantomWallet . connect ( ) ;
187+ // Connect to Phantom with safe execution
188+ const response = await safeExecute (
189+ ( ) => phantomWallet . connect ( ) ,
190+ 'phantom-wallet-connect' ,
191+ null
192+ ) ;
186193
187- if ( response . publicKey ) {
194+ if ( response ? .publicKey ) {
188195 const pubKey = new PublicKey ( response . publicKey . toString ( ) ) ;
189196 const address = pubKey . toString ( ) ;
190197
@@ -207,7 +214,7 @@ const PhantomWalletProvider = ({ children }) => {
207214 throw new Error ( 'Failed to get public key from Phantom wallet' ) ;
208215 }
209216 } catch ( err ) {
210- console . error ( '[ PhantomWalletProvider] Connection failed:' , err ) ;
217+ logError ( err , ' PhantomWalletProvider connection' ) ;
211218 const errorMsg = err . message || 'Failed to connect to Phantom wallet' ;
212219 setError ( errorMsg ) ;
213220 setConnectionState ( 'error' ) ;
@@ -245,9 +252,9 @@ const PhantomWalletProvider = ({ children }) => {
245252 }
246253 } , [ detectPhantomWallet , toast ] ) ;
247254
248- // Disconnect from Phantom wallet
255+ // Disconnect from Phantom wallet with error protection
249256 const disconnect = useCallback ( async ( ) => {
250- try {
257+ return safeExecute ( async ( ) => {
251258 if ( wallet && wallet . disconnect ) {
252259 await wallet . disconnect ( ) ;
253260 }
@@ -268,12 +275,12 @@ const PhantomWalletProvider = ({ children }) => {
268275 } ) ;
269276
270277 console . log ( '[PhantomWalletProvider] Disconnected from Phantom wallet' ) ;
271- } catch ( err ) {
272- console . error ( '[ PhantomWalletProvider] Disconnect failed:' , err ) ;
278+ } , 'phantom-wallet-disconnect' ) . catch ( ( err ) => {
279+ logError ( err , ' PhantomWalletProvider disconnect' ) ;
273280 const errorMsg = err . message || 'Disconnect failed' ;
274281 setError ( errorMsg ) ;
275282 toast . criticalError ( `Disconnect failed: ${ errorMsg } ` ) ;
276- }
283+ } ) ;
277284 } , [ wallet , toast ] ) ;
278285
279286 // Sign transaction
@@ -483,11 +490,11 @@ const PhantomWalletProvider = ({ children }) => {
483490 console . log ( '[PhantomWalletProvider] Reconnection cancelled by user' ) ;
484491 } , [ ] ) ;
485492
486- // Check for existing connection on mount
493+ // Check for existing connection on mount with error protection
487494 useEffect ( ( ) => {
488495 const checkExistingConnection = async ( ) => {
489- try {
490- const phantomWallet = detectPhantomWallet ( ) ;
496+ return safeExecute ( async ( ) => {
497+ const phantomWallet = await detectPhantomWallet ( ) ;
491498 const wasConnected = localStorage . getItem ( 'phantomConnected' ) === 'true' ;
492499
493500 if ( phantomWallet && wasConnected ) {
@@ -509,41 +516,50 @@ const PhantomWalletProvider = ({ children }) => {
509516 localStorage . removeItem ( 'phantomAddress' ) ;
510517 }
511518 }
512- } catch ( error ) {
513- console . warn ( '[ PhantomWalletProvider] Failed to restore connection:' , error ) ;
519+ } , 'phantom-wallet-restore-connection' ) . catch ( ( error ) => {
520+ logError ( error , ' PhantomWalletProvider restore connection' ) ;
514521 localStorage . removeItem ( 'phantomConnected' ) ;
515522 localStorage . removeItem ( 'phantomAddress' ) ;
516- }
523+ } ) ;
517524 } ;
518525
519526 checkExistingConnection ( ) ;
520527 } , [ detectPhantomWallet ] ) ;
521528
522- // Listen for account changes
529+ // Listen for account changes with error protection
523530 useEffect ( ( ) => {
524531 if ( wallet && connected ) {
525532 const handleAccountChanged = ( publicKey ) => {
526- if ( publicKey ) {
527- const pubKey = new PublicKey ( publicKey . toString ( ) ) ;
528- const address = pubKey . toString ( ) ;
529-
530- setPublicKey ( pubKey ) ;
531- setWalletAddress ( address ) ;
532- localStorage . setItem ( 'phantomAddress' , address ) ;
533-
534- toast . info ( 'Phantom wallet account changed' , {
535- category : ERROR_CATEGORIES . SUCCESS
536- } ) ;
537- } else {
538- // Account disconnected
539- disconnect ( ) ;
540- }
533+ safeExecute ( ( ) => {
534+ if ( publicKey ) {
535+ const pubKey = new PublicKey ( publicKey . toString ( ) ) ;
536+ const address = pubKey . toString ( ) ;
537+
538+ setPublicKey ( pubKey ) ;
539+ setWalletAddress ( address ) ;
540+ localStorage . setItem ( 'phantomAddress' , address ) ;
541+
542+ toast . info ( 'Phantom wallet account changed' , {
543+ category : ERROR_CATEGORIES . SUCCESS
544+ } ) ;
545+ } else {
546+ // Account disconnected
547+ disconnect ( ) ;
548+ }
549+ } , 'phantom-wallet-account-change' ) . catch ( ( error ) => {
550+ logError ( error , 'PhantomWalletProvider account change handler' ) ;
551+ } ) ;
541552 } ;
542553
543- wallet . on ( 'accountChanged' , handleAccountChanged ) ;
554+ // Safe event listener attachment
555+ safeExecute ( ( ) => {
556+ wallet . on ( 'accountChanged' , handleAccountChanged ) ;
557+ } , 'phantom-wallet-event-listener' ) ;
544558
545559 return ( ) => {
546- wallet . off ( 'accountChanged' , handleAccountChanged ) ;
560+ safeExecute ( ( ) => {
561+ wallet . off ( 'accountChanged' , handleAccountChanged ) ;
562+ } , 'phantom-wallet-event-cleanup' ) ;
547563 } ;
548564 }
549565 } , [ wallet , connected , disconnect , toast ] ) ;
0 commit comments