-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (66 loc) · 2.46 KB
/
script.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Define the Ethereum smart contract address for the MoodContract
const MoodContractAddress = "0x21127f7a4c0A6dC97215C4e52F90Fec883aC59D5";
// Define the ABI (Application Binary Interface) for the MoodContract, specifying its functions and their details
const MoodContractABI = [
{
"inputs": [
{
"internalType": "string",
"name": "_mood",
"type": "string"
}
],
"name": "setMood",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getMood",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
];
// Initialize variables to store the instance of the MoodContract and the Ethereum account signer
let MoodContract = undefined;
let signer = undefined;
// Create a Web3Provider using the Ethereum provider from the browser, specifying a custom network name "sepolia"
const provider = new ethers.providers.Web3Provider(window.ethereum, "sepolia");
// Request Ethereum accounts from the user
provider.send("eth_requestAccounts", []).then(() => {
// Retrieve the list of Ethereum accounts after successful account request
provider.listAccounts().then((accounts) => {
// Get the signer object for the first Ethereum account
signer = provider.getSigner(accounts[0]);
// Instantiate the MoodContract with the contract address, ABI, and signer
MoodContract = new ethers.Contract(
MoodContractAddress,
MoodContractABI,
signer
);
});
});
// Asynchronous function to fetch and display the current mood
async function getMood() {
// Call the getMood function from the MoodContract
const mood = await MoodContract.getMood();
// Display the fetched mood on the webpage
document.getElementById("showMood").innerText = `Your Mood: ${mood}`;
// Log the mood to the console for debugging or further analysis
console.log(mood);
}
// Asynchronous function to set a new mood
async function setMood() {
// Get the mood value from the input field on the webpage
const mood = document.getElementById("mood").value;
// Call the setMood function of the MoodContract to update the stored mood value
await MoodContract.setMood(mood);
}