-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethers.js
70 lines (62 loc) · 1.88 KB
/
ethers.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
// Load the ethers library from the external source
const script = document.createElement('script');
script.src = 'https://cdn.ethers.io/lib/ethers-5.7.2.umd.min.js';
script.type = 'application/javascript';
script.onload = () => {
// Your code that relies on ethers library
const provider = new ethers.providers.Web3Provider(window.ethereum, "sepolia");
provider.send("eth_requestAccounts", []).then(() => {
provider.listAccounts().then((accounts) => {
signer = provider.getSigner(accounts[0]);
MoodContract = new ethers.Contract(
MoodContractAddress,
MoodContractABI,
signer
);
});
});
};
document.head.appendChild(script);
// Your code
// Replace the following two values
const MoodContractAddress = '0x764AcD20fcf155eC0fDc4A37E160A20DA79DC14c';
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"
}
];
// Currently these two are undefined, we will use Ethers to assign them values
let MoodContract = undefined;
let signer = undefined;
async function getMood() {
const mood = await MoodContract.getMood();
document.getElementById("showMood").innerText = `Your Mood: ${mood}`;
console.log(mood);
}
async function setMood() {
const mood = document.getElementById("mood").value;
await MoodContract.setMood(mood);
}