From 3a087cbf8dbbb2448f270ca402a34ce05880b179 Mon Sep 17 00:00:00 2001 From: Isha Date: Thu, 7 Nov 2024 19:54:11 +0530 Subject: [PATCH] Add methods for sending tx and receiving tx hash --- .../components/projects/create/Configure.tsx | 104 ++++++++++++------ .../src/components/projects/create/IFrame.tsx | 6 +- 2 files changed, 73 insertions(+), 37 deletions(-) diff --git a/packages/frontend/src/components/projects/create/Configure.tsx b/packages/frontend/src/components/projects/create/Configure.tsx index 36661f53..67f7bc53 100644 --- a/packages/frontend/src/components/projects/create/Configure.tsx +++ b/packages/frontend/src/components/projects/create/Configure.tsx @@ -24,6 +24,7 @@ import EnvironmentVariablesForm from 'pages/org-slug/projects/id/settings/Enviro import { EnvironmentVariablesFormValues } from 'types/types'; import IFrame from './IFrame'; import { useWalletConnectClient } from 'context/WalletConnectContext'; +import { VITE_LACONICD_CHAIN_ID } from 'utils/constants'; type ConfigureDeploymentFormValues = { option: string; @@ -48,6 +49,8 @@ const Configure = () => { const [isPaymentLoading, setIsPaymentLoading] = useState(false); const [isPaymentDone, setIsPaymentDone] = useState(false); const [isFrameVisible, setIsFrameVisible] = useState(false); + const [txHashr, setTxHash] = useState(null); + const [isTransactionPending, setIsTransactionPending] = useState(false); const [searchParams] = useSearchParams(); const templateId = searchParams.get('templateId'); @@ -211,17 +214,19 @@ const Configure = () => { const amountToBePaid = amount.replace(/\D/g, '').toString(); - const txHashResponse = await cosmosSendTokensHandler( + await cosmosSendTokensHandler( selectedAccount, amountToBePaid, ); - if (!txHashResponse) { + await new Promise((resolve) => setTimeout(resolve, 3000)); + + if (!txHashr) { console.error('Tx not successful'); return; } - txHash = txHashResponse; + txHash = txHashr; const isTxHashValid = await verifyTx( senderAddress, @@ -309,12 +314,11 @@ const Configure = () => { const cosmosSendTokensHandler = useCallback( async (selectedAccount: string, amount: string) => { - if (!signClient || !session || !selectedAccount) { + if (!selectedAccount) { return; } - const chainId = selectedAccount.split(':')[1]; - const senderAddress = selectedAccount.split(':')[2]; + const senderAddress = selectedAccount; const snowballAddress = await client.getAddress(); try { @@ -328,35 +332,10 @@ const Configure = () => { onDismiss: dismiss, }); - const result: { signature: string } = await signClient.request({ - topic: session.topic, - chainId: `cosmos:${chainId}`, - request: { - method: 'cosmos_sendTokens', - params: [ - { - from: senderAddress, - to: snowballAddress, - value: amount, - }, - ], - }, - }); + await requestTx(senderAddress, snowballAddress, amount) - if (!result) { - throw new Error('Error completing transaction'); - } - - toast({ - id: 'payment-successful', - title: 'Payment successful', - variant: 'success', - onDismiss: dismiss, - }); - - setIsPaymentDone(true); - - return result.signature; + await new Promise((resolve) => setTimeout(resolve, 10000)); + return; } catch (error: any) { console.error('Error sending tokens', error); @@ -368,6 +347,7 @@ const Configure = () => { }); setIsPaymentDone(false); + // should return the txhash but we arw checking for it in the gettxstatus useeffect.. how do i return it after it is found? } finally { setIsPaymentLoading(false); } @@ -375,6 +355,62 @@ const Configure = () => { [session, signClient, toast], ); + const requestTx = async (sender: string, recipient: string, amount: string,) => { + const iframe = document.getElementById('walletIframe') as HTMLIFrameElement; + + if (!iframe.contentWindow) { + console.error('Iframe not found or not loaded'); + return; + } + + iframe.contentWindow.postMessage( + { + type: 'REQUEST_TX', + chainId: VITE_LACONICD_CHAIN_ID, + fromAddress: sender, + toAddress: recipient, + amount, + }, + 'http://localhost:3001' + ); + }; + + useEffect(() => { + // Define the event listener to listen for transaction status + const getTxStatus = (event: MessageEvent) => { + // Ensure you're handling the message only from the correct origin + if (event.origin !== 'http://localhost:3001') return; + + if (event.data.type === 'TRANSACTION_SUCCESS') { + console.log('Transaction Success:', event.data.transactionHash); + setTxHash(event.data.transactionHash); + console.log(event.data.transactionHash) + setIsTransactionPending(false); + + console.log({txHashr}) + toast({ + id: 'payment-successful', + title: 'Payment successful', + variant: 'success', + onDismiss: dismiss, + }); + + setIsPaymentDone(true); + } else if (event.data.type === 'ERROR') { + console.error('Error from wallet:', event.data.message); + setIsTransactionPending(false); // Mark the transaction as failed + } + }; + + // Add the event listener to listen for 'message' events + window.addEventListener('message', getTxStatus); + + // Cleanup: Remove the event listener when the component unmounts + return () => { + window.removeEventListener('message', getTxStatus); + }; + }, []); + useEffect(() => { fetchDeployers(); }, []); diff --git a/packages/frontend/src/components/projects/create/IFrame.tsx b/packages/frontend/src/components/projects/create/IFrame.tsx index f854e7e3..a6764ea2 100644 --- a/packages/frontend/src/components/projects/create/IFrame.tsx +++ b/packages/frontend/src/components/projects/create/IFrame.tsx @@ -88,7 +88,7 @@ const IFrame = ({ )} - + - + > */} ); };