-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteract.js
34 lines (27 loc) · 1.19 KB
/
interact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const { ethers } = require("ethers");
// Connect to an Ethereum provider (e.g., MetaMask or Infura)
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
// The smart contract ABI (Application Binary Interface)
const abi = [
"function setGreeting(string memory _greeting) public",
"function getGreeting() public view returns (string memory)"
];
// The deployed contract address (you'll replace this with the actual address after deploying)
const contractAddress = "0xYourDeployedContractAddressHere";
// Create a contract instance
const helloWorldContract = new ethers.Contract(contractAddress, abi, signer);
// Function to read the greeting
async function getGreeting() {
const greeting = await helloWorldContract.getGreeting();
console.log("Current Greeting:", greeting);
}
// Function to set a new greeting
async function setGreeting(newGreeting) {
const tx = await helloWorldContract.setGreeting(newGreeting);
await tx.wait(); // Wait for the transaction to be mined
console.log("Greeting updated to:", newGreeting);
}
// Usage
getGreeting(); // Get the initial greeting
setGreeting("Hello, Ethereum!"); // Set a new greeting