Set event for tx success

This commit is contained in:
Isha 2024-11-08 10:01:31 +05:30
parent f82ff49d77
commit 08711b50d6

View File

@ -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<string | null>(null);
const txEventRef = useRef<MessageEvent | null>(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');