diff --git a/src/screens/WalletEmbed.tsx b/src/screens/WalletEmbed.tsx index ee9f150..8873e51 100644 --- a/src/screens/WalletEmbed.tsx +++ b/src/screens/WalletEmbed.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState, useCallback } from 'react'; +import React, { useEffect, useState, useCallback, useRef } from 'react'; import { ScrollView, View } from 'react-native'; import { @@ -32,6 +32,7 @@ export const WalletEmbed = () => { const [gasLimit, setGasLimit] = useState(''); const [isTxLoading, setIsTxLoading] = useState(false); const [txError, setTxError] = useState(null); + const txEventRef = useRef(null); const { networksData } = useNetworks(); @@ -117,6 +118,8 @@ export const WalletEmbed = () => { const handleTxRequested = async (event: MessageEvent) => { if (event.data.type !== 'REQUEST_TX') return; + txEventRef.current = event; + const { chainId, fromAddress, toAddress, amount } = event.data; const network = networksData.find( net => net.chainId === chainId @@ -200,7 +203,7 @@ export const WalletEmbed = () => { useEffect(() => { window.addEventListener('message', handleTxRequested); return () => window.removeEventListener('message', handleTxRequested); - }, [networksData]); + }, [networksData, handleTxRequested]); const acceptRequestHandler = async () => { try { @@ -240,7 +243,12 @@ export const WalletEmbed = () => { fee ); - sendMessage(window, 'TRANSACTION_SUCCESS', txResult.transactionHash, '*'); + const event = txEventRef.current; + if (event?.source) { + sendMessage(event.source as Window, 'TRANSACTION_SUCCESS', txResult.transactionHash, '*'); + } else { + console.error('No event source available to send message'); + } } catch (error) { console.error('Transaction error:', error); setTxError('Transaction failed');