-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
103 lines (92 loc) · 2.93 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Inbo Contract</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.2.7-rc.0/web3.min.js"></script>
<script src="https://smtpjs.com/v3/smtp.js" ></script>
</head>
<body>
<button onclick="connectMetamask()">CONNECT TO METAMASK</button> <br>
<p id="accountArea">Account address will apear here</p>
<button onclick="connectContract()">CLICK TO CONNECT WITH CONTRACT</button> <br>
<p id="contractArea">Here you can see contract connection status</p>
<button onclick="readContract()">EXISTING MESSAGE</button> <br>
<p id="dataArea">Existing message appears here</p>
<h3>Enter new message in below text box</h3>
<div>
<input id="newmsg" placeholder="Enter New Message" ></input>
</div>
<form onsubmit="updateContract(); reset(); return false;">
<button type = "submit" >CLICK TO UPDATE WITH NEW MESSAGE</button> <br>
<p id = "dataArea1"></p>
</form>
<script>
//1- connect metamask to site
let account;
const connectMetamask = async () => {
if(window.ethereum !== "undefined") {
const accounts = await ethereum.request({method: "eth_requestAccounts"});
account = accounts[0];
document.getElementById("accountArea").innerHTML = account;
}
}
//2- Connect to Smart contract
const connectContract = async () => {
const ABI = [
{
"inputs": [
{
"internalType": "string",
"name": "newMessage",
"type": "string"
}
],
"name": "setMessage",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "initialMessage",
"type": "string"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "message",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
];
const Address = "0xa2900AFf5121d9393e195ADAF4aD1042cD4C7a13";
window.web3 = await new Web3(window.ethereum);
window.contract = await new window.web3.eth.Contract( ABI, Address);
document.getElementById("contractArea").innerHTML = "connected to smart contract";
}
//3-read data from smart contract
const readContract = async () => {
const data = await window.contract.methods.message().call();
document.getElementById("dataArea").textContent = data;
}
//4-update data from smart contract
const updateContract = async () => {
let newmsg = document.getElementById('newmsg').value;
const data1 = await window.contract.methods.setMessage(newmsg).send({from: account});
window.location.reload();
}
</script>
</body>
</html>