@@ -2,6 +2,7 @@ import { useState, useEffect, useCallback, useRef } from 'react';
22import { useSDK } from '@metamask/sdk-react' ;
33import { useToast } from '@functionland/component-library' ;
44import { useSettingsStore } from '../stores/useSettingsStore' ;
5+ import { useUserProfileStore } from '../stores/useUserProfileStore' ;
56import { useWalletNetwork } from './useWalletNetwork' ;
67import { getContractService , ContractService , resetContractService } from '../contracts/contractService' ;
78import { SupportedChain } from '../contracts/types' ;
@@ -57,8 +58,12 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
5758 const { isOnCorrectNetwork } = useWalletNetwork ( ) ;
5859 const selectedChain = useSettingsStore ( ( state ) => state . selectedChain ) ;
5960 const setSelectedChain = useSettingsStore ( ( state ) => state . setSelectedChain ) ;
61+ const manualSignatureWalletAddress = useUserProfileStore ( ( state ) => state . manualSignatureWalletAddress ) ;
6062 const initializedChainRef = useRef < SupportedChain | null > ( null ) ;
6163 const showConnectedNotification = options ?. showConnectedNotification ?? false ;
64+
65+ // Use MetaMask account if available, otherwise fallback to manually signed wallet address
66+ const effectiveAccount = account || manualSignatureWalletAddress ;
6267
6368 const [ state , setState ] = useState < ContractIntegrationState > ( {
6469 isInitialized : false ,
@@ -75,10 +80,10 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
7580 const lastInitAttemptRef = useRef < number > ( 0 ) ;
7681
7782 const initializeContracts = useCallback ( async ( chain : SupportedChain , options : { allowNetworkSwitch ?: boolean } = { } ) => {
78- console . log ( 'initializeContracts called for chain:' , chain , 'provider:' , ! ! provider , 'account:' , account ) ;
83+ console . log ( 'initializeContracts called for chain:' , chain , 'provider:' , ! ! provider , 'account:' , account , 'effectiveAccount:' , effectiveAccount ) ;
7984
80- if ( ! provider || ! account ) {
81- console . log ( 'Missing provider or account, cannot initialize contracts' ) ;
85+ if ( ! provider || ! effectiveAccount ) {
86+ console . log ( 'Missing provider or effective account, cannot initialize contracts' ) ;
8287 setState ( prev => ( {
8388 ...prev ,
8489 isInitialized : false ,
@@ -339,15 +344,16 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
339344 useEffect ( ( ) => {
340345 console . log ( 'Contract integration useEffect triggered:' , {
341346 account : ! ! account ,
347+ effectiveAccount : ! ! effectiveAccount ,
342348 provider : ! ! provider ,
343349 selectedChain,
344350 isInitialized : state . isInitialized ,
345351 isInitializing : state . isInitializing
346352 } ) ;
347353
348- // Clear state when wallet disconnects
349- if ( ! account || ! provider ) {
350- console . log ( 'Clearing contract state - no account or provider' ) ;
354+ // Clear state when wallet disconnects (both MetaMask and manual signature)
355+ if ( ! effectiveAccount || ! provider ) {
356+ console . log ( 'Clearing contract state - no effective account or provider' ) ;
351357 setState ( {
352358 isInitialized : false ,
353359 isInitializing : false ,
@@ -383,6 +389,7 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
383389 }
384390
385391 // Check if we should show the switch button (same logic as WalletNotification)
392+ // Only check if MetaMask is connected (not for manual signature fallback)
386393 const shouldShowSwitchButton = account && provider && selectedChain && ! isOnCorrectNetwork ;
387394
388395 // If switch button should be shown, DO NOT perform any contract operations
@@ -407,7 +414,7 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
407414 }
408415
409416 // Only attempt initialization when wallet is connected AND on correct network
410- const needsInitialization = account && provider && selectedChain && isOnCorrectNetwork && ! state . isInitializing && ! state . isInitialized && initializedChainRef . current !== selectedChain ;
417+ const needsInitialization = effectiveAccount && provider && selectedChain && isOnCorrectNetwork && ! state . isInitializing && ! state . isInitialized && initializedChainRef . current !== selectedChain ;
411418
412419 if ( needsInitialization ) {
413420 console . log ( 'Wallet is connected and on correct network - attempting automatic contract initialization for chain:' , selectedChain ) ;
@@ -454,7 +461,7 @@ export const useContractIntegration = (options?: { showConnectedNotification?: b
454461 } ) ;
455462 } , 1500 ) ; // 1500ms debounce to allow network state sync
456463 }
457- } , [ account , provider , selectedChain , isOnCorrectNetwork , state . isInitialized , state . isInitializing ] ) ;
464+ } , [ account , effectiveAccount , provider , selectedChain , isOnCorrectNetwork , state . isInitialized , state . isInitializing ] ) ;
458465
459466 // Cleanup timeout on unmount
460467 useEffect ( ( ) => {
0 commit comments