diff --git a/App.jsx b/App.jsx new file mode 100644 index 0000000..eb1cc93 --- /dev/null +++ b/App.jsx @@ -0,0 +1,78 @@ +import { useState, useEffect } from "react"; +import abi from "../abi.json"; +import { ethers } from "ethers"; + +const contractAddress = "0x9D1eb059977D71E1A21BdebD1F700d4A39744A70"; + +function App() { + const [text, setText] = useState(""); + const [retrievedMessage, setRetrievedMessage] = useState(""); + + async function requestAccount() { + await window.ethereum.request({ method: "eth_requestAccounts" }); + } + + const handleSet = async () => { + try { + if (!text) { + alert("Please enter a message before setting."); + return; + } + + if (window.ethereum && window.ethereum.isMetaMask) { + await requestAccount(); + const provider = new ethers.BrowserProvider(window.ethereum); + const signer = await provider.getSigner(); + const contract = new ethers.Contract(contractAddress, abi, signer); + + const tx = await contract.setMessage(text); + const txReceipt = await tx.wait(); + console.log("Transaction successful:", txReceipt); + setText(""); // Clear input after successful transaction + await getMessage(); // Refresh the displayed message + } else { + throw new Error("Please use MetaMask to interact with this application."); + } + } catch (error) { + console.error("Error Debbie setting message:", error); + alert(error.message || "Failed to set message on the smart contract."); + } + }; + + const getMessage = async () => { + try { + if (window.ethereum && window.ethereum.isMetaMask) { + const provider = new ethers.BrowserProvider(window.ethereum); + const contract = new ethers.Contract(contractAddress, abi, provider); + const message = await contract.getMessage(); + setRetrievedMessage(message); + } else { + throw new Error("Please use MetaMask to interact with this application."); + } + } catch (error) { + console.error("Error Debbie fetching message:", error); + alert(error.message || "Failed to fetch message from the smart contract."); + } + }; + + useEffect(() => { + getMessage(); + }, []); + + return ( +
Current Message: {retrievedMessage || "No message retrieved yet"}
+