Created
July 21, 2025 20:53
-
-
Save Signor1/3f26d8553fb30b30f9146ab2a820af68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState } from "react"; | |
| import abi from "./abi.json"; | |
| import { ethers } from "ethers"; | |
| const contractAddress = "0x9D1eb059977D71E1A21BdebD1F700d4A39744A70"; | |
| function App() { | |
| const [text, setText] = 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) { | |
| 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); | |
| } else { | |
| console.error("MetaMask not found. Please install MetaMask to use this application."); | |
| } | |
| } catch (error) { | |
| console.error("Error setting message:", error); | |
| alert(error.message || error); | |
| } | |
| }; | |
| return ( | |
| <div style={{ padding: "2rem" }}> | |
| <h1>Set Message on Smart Contract</h1> | |
| <input | |
| type="text" | |
| placeholder="Set message" | |
| value={text} | |
| onChange={(e) => setText(e.target.value)} | |
| /> | |
| <button onClick={handleSet}>Set Message</button> | |
| </div> | |
| ); | |
| } | |
| export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity 0.8.26; | |
| contract Greeter { | |
| string public text; | |
| function setMessage(string memory _text) public { | |
| text = _text; | |
| } | |
| function getMessage() public view returns(string memory) { | |
| return text; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment