-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
139 lines (125 loc) · 3.09 KB
/
Copy pathindex.html
File metadata and controls
139 lines (125 loc) · 3.09 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello Web3 DApp</title>
<script src="https://cdn.jsdelivr.net/npm/ethers@5.7.2/dist/ethers.umd.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
padding: 40px;
}
button {
margin-top: 10px;
}
input {
padding: 5px;
margin-top: 5px;
width: 300px;
}
</style>
</head>
<body>
<h1>Hello Web3 DApp</h1>
<button id="connectButton">Connect Wallet</button>
<p><strong>Connected Wallet:</strong> <span id="walletAddress">Not connected</span></p>
<hr>
<h3>Current Message:</h3>
<p id="currentMessage">No message loaded yet</p>
<button id="getMessage">Get Message</button>
<h3>Update Message:</h3>
<input type="text" id="newMessageInput" placeholder="Enter new message">
<button id="setMessage">Update Message</button>
<script>
const contractAddress = "0xc929CEf83242b17aB7202eDBdaC7F7243Bff4d71";
const contractABI = [
{
"inputs": [{"internalType": "string", "name": "_message", "type": "string"}],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "getMessage",
"outputs": [{"internalType": "string", "name": "", "type": "string"}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "message",
"outputs": [{"internalType": "string", "name": "", "type": "string"}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{"internalType": "string", "name": "_newMessage", "type": "string"}],
"name": "setMessage",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
let provider;
let signer;
let contract;
// Connect wallet
async function connectWallet() {
if (window.ethereum) {
try {
await window.ethereum.request({ method: "eth_requestAccounts" });
provider = new ethers.providers.Web3Provider(window.ethereum);
signer = provider.getSigner();
const userAddress = await signer.getAddress();
document.getElementById("walletAddress").innerText = userAddress;
contract = new ethers.Contract(contractAddress, contractABI, signer);
alert("Wallet connected!");
} catch (err) {
console.error(err);
alert("Wallet connection failed.");
}
} else {
alert("MetaMask is not installed.");
}
}
// Get current message
async function fetchMessage() {
if (!contract) {
alert("Connect wallet first.");
return;
}
try {
const message = await contract.getMessage();
document.getElementById("currentMessage").innerText = message;
} catch (err) {
console.error(err);
alert("Could not fetch message.");
}
}
// Set new message
async function updateMessage() {
if (!contract) {
alert("Connect wallet first.");
return;
}
const newMsg = document.getElementById("newMessageInput").value;
if (!newMsg) {
alert("Enter a message.");
return;
}
try {
const tx = await contract.setMessage(newMsg);
await tx.wait();
alert("Message updated!");
fetchMessage(); // refresh the displayed message
} catch (err) {
console.error(err);
alert("Failed to update message.");
}
}
document.getElementById("connectButton").addEventListener("click", connectWallet);
document.getElementById("getMessage").addEventListener("click", fetchMessage);
document.getElementById("setMessage").addEventListener("click", updateMessage);
</script>
</body>
</html>