Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/smazurs/WaveWallet
Browse files Browse the repository at this point in the history
  • Loading branch information
abdularif0705 committed Mar 10, 2024
2 parents ac15464 + b918068 commit 34c532d
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/components/templates/transfers/ETH/ETHTransfers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,15 @@ import {
useToast,
VStack,
Text,
HStack,
} from "@chakra-ui/react";
import { ethers } from "ethers";

// Component for handling ETH transfers with Arduino handshake verification
const ETHTransfers = () => {
// State variables for recipient address, amount to be sent, and transactions history
const [recipient, setRecipient] = useState("");
const [amount, setAmount] = useState("");
const [txs, setTxs] = useState([]);

// Chakra UI toast for showing notifications
const toast = useToast();

// Function to initiate payment after Arduino handshake confirmation
const startPayment = async () => {
try {
if (!window.ethereum)
Expand All @@ -33,12 +27,15 @@ const ETHTransfers = () => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
ethers.utils.getAddress(recipient); // Validates the address
ethers.utils.getAddress(recipient); // Validates the address
const tx = await signer.sendTransaction({
to: recipient,
value: ethers.utils.parseEther(amount),
});
console.log("tx", tx);
setTxs([tx]);
console.log("tx", tx);
setTxs([tx]);
toast({
title: "Transaction Submitted",
description: `Transaction hash: ${tx.hash}`,
Expand Down Expand Up @@ -88,14 +85,9 @@ const ETHTransfers = () => {
placeholder="Enter amount"
/>
</FormControl>
<HStack spacing={4}>
<Button colorScheme="blue" onClick={startPayment}>
Send
</Button>
<Button colorScheme="green" onClick={handleReceive}>
Receive
</Button>
</HStack>
<Button colorScheme="blue" onClick={startPayment}>
Pay now
</Button>
</VStack>
{txs.length > 0 && (
<Box mt="4">
Expand Down
160 changes: 160 additions & 0 deletions src/components/templates/transfers/ETH/arduino_handshake.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// import React, { useState } from "react";
// import {
// Box,
// Button,
// FormControl,
// FormLabel,
// Input,
// Heading,
// useToast,
// VStack,
// Text,
// HStack,
// } from "@chakra-ui/react";
// import { ethers } from "ethers";

// // Component for handling ETH transfers with Arduino handshake verification
// const ETHTransfers = () => {
// // State variables for recipient address, amount to be sent, and transactions history
// const [recipient, setRecipient] = useState("");
// const [amount, setAmount] = useState("");
// const [txs, setTxs] = useState([]);

// // Chakra UI toast for showing notifications
// const toast = useToast();

// // 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
// const tx = await signer.sendTransaction({
// to: recipient,
// value: ethers.utils.parseEther(amount),
// });

// // Updates the transaction history and shows a success notification
// setTxs([...txs, tx]);
// toast({
// title: "Transaction Submitted",
// description: `Transaction hash: ${tx.hash}`,
// status: "info",
// duration: 9000,
// isClosable: true,
// });
// } catch (err) {
// // Catches and logs any errors, shows an error notification
// console.error(err);
// toast({
// title: "Transaction Failed",
// description: err.message,
// status: "error",
// duration: 9000,
// isClosable: true,
// });
// }
// };

// // 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,
// });
// }
// };

// // UI components for entering transaction details, sending, and receiving
// return (
// <Box padding="4" maxW="md">
// <Heading mb="6">Send ETH</Heading>
// <VStack spacing={5} as="form" onSubmit={(e) => e.preventDefault()}>
// <FormControl>
// <FormLabel htmlFor="recipient">Recipient Address</FormLabel>
// <Input
// id="recipient"
// type="text"
// value={recipient}
// onChange={(e) => setRecipient(e.target.value)}
// placeholder="Enter recipient address"
// />
// </FormControl>
// <FormControl>
// <FormLabel htmlFor="amount">Amount in ETH</FormLabel>
// <Input
// id="amount"
// type="text"
// value={amount}
// onChange={(e) => setAmount(e.target.value)}
// placeholder="Enter amount"
// />
// </FormControl>
// <HStack spacing={4}>
// <Button colorScheme="blue" onClick={startPayment}>
// Send
// </Button>
// <Button colorScheme="green" onClick={handleReceive}>
// Receive
// </Button>
// </HStack>
// </VStack>
// {txs.length > 0 && (
// <Box mt="4">
// <Heading size="md">Transaction History</Heading>
// {txs.map((tx, index) => (
// <Text key={index}>Tx Hash: {tx.hash}</Text>
// ))}
// </Box>
// )}
// </Box>
// );
// };

// export default ETHTransfers;

0 comments on commit 34c532d

Please sign in to comment.