From 96bd814a5145ceaae04bb6e755ad9527fe41b6a7 Mon Sep 17 00:00:00 2001 From: Abdul Arif <55670928+abdularif0705@users.noreply.github.com> Date: Sun, 10 Mar 2024 11:39:38 -0400 Subject: [PATCH 1/2] Refactor handleReceive function to open a new window for Arduino handshake --- .../templates/transfers/ETH/ETHTransfers.tsx | 34 ++----------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/src/components/templates/transfers/ETH/ETHTransfers.tsx b/src/components/templates/transfers/ETH/ETHTransfers.tsx index 0476518..d538fd9 100644 --- a/src/components/templates/transfers/ETH/ETHTransfers.tsx +++ b/src/components/templates/transfers/ETH/ETHTransfers.tsx @@ -77,38 +77,8 @@ const ETHTransfers = () => { }; // Function to handle the receiving process (listening for Arduino confirmation) - const handleReceive = async () => { - try { - // Similar to startPayment but with action set to 'receive' - const response = await fetch("/api/arduino_handshake", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ action: "receive", recipient, amount }), - }); - - // Checks for Arduino approval for receiving - if (!response.ok) - throw new Error("Failed to receive approval from Arduino"); - - // Shows a success notification on receiving Arduino handshake approval - toast({ - title: "Approval Received", - description: "Arduino handshake approved. Ready to receive.", - status: "success", - duration: 9000, - isClosable: true, - }); - } catch (err) { - // Catches and logs any errors, shows an error notification - console.error(err); - toast({ - title: "Receiving Failed", - description: err.message, - status: "error", - duration: 9000, - isClosable: true, - }); - } + const handleReceive = () => { + window.open("http://localhost:5000/", "_blank"); }; // UI components for entering transaction details, sending, and receiving From ac1546411acb8d2165b49054b5db6ad6a724a80e Mon Sep 17 00:00:00 2001 From: Abdul Arif <55670928+abdularif0705@users.noreply.github.com> Date: Sun, 10 Mar 2024 11:40:00 -0400 Subject: [PATCH 2/2] Refactor ETHTransfers component to improve code readability and remove unnecessary API calls --- .../templates/transfers/ETH/ETHTransfers.tsx | 26 +++---------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/src/components/templates/transfers/ETH/ETHTransfers.tsx b/src/components/templates/transfers/ETH/ETHTransfers.tsx index d538fd9..e668308 100644 --- a/src/components/templates/transfers/ETH/ETHTransfers.tsx +++ b/src/components/templates/transfers/ETH/ETHTransfers.tsx @@ -26,36 +26,19 @@ const ETHTransfers = () => { // Function to initiate payment after Arduino handshake confirmation const startPayment = async () => { try { - // Checks if the Ethereum provider (MetaMask) is available if (!window.ethereum) throw new Error("No crypto wallet found. Please install it."); - // Requests access to the user's account await window.ethereum.send("eth_requestAccounts"); const provider = new ethers.providers.Web3Provider(window.ethereum); const signer = provider.getSigner(); - - // Validates the recipient address format - ethers.utils.getAddress(recipient); - - // Makes a POST request to the backend to check for Arduino handshake approval - const response = await fetch("/api/arduino_handshake", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ action: "send", recipient, amount }), - }); - - // Throws an error if the handshake was not approved - if (!response.ok) throw new Error("Transaction not approved by Arduino"); - - // If approved, proceeds to send ETH to the recipient + ethers.utils.getAddress(recipient); // Validates the address const tx = await signer.sendTransaction({ to: recipient, value: ethers.utils.parseEther(amount), }); - - // Updates the transaction history and shows a success notification - setTxs([...txs, tx]); + console.log("tx", tx); + setTxs([tx]); toast({ title: "Transaction Submitted", description: `Transaction hash: ${tx.hash}`, @@ -64,7 +47,6 @@ const ETHTransfers = () => { isClosable: true, }); } catch (err) { - // Catches and logs any errors, shows an error notification console.error(err); toast({ title: "Transaction Failed", @@ -78,7 +60,7 @@ const ETHTransfers = () => { // Function to handle the receiving process (listening for Arduino confirmation) const handleReceive = () => { - window.open("http://localhost:5000/", "_blank"); + window.open("http://localhost:http://127.0.0.1:5000/", "_blank"); }; // UI components for entering transaction details, sending, and receiving