-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
192 lines (185 loc) · 5.18 KB
/
app.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const contractAddress = '0xe7f1725e7734ce288f8367e1bb143e90bb3f0512';
const contractABI = [
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "patient",
"type": "address"
},
{
"indexed": false,
"internalType": "string",
"name": "diagnosis",
"type": "string"
},
{
"indexed": false,
"internalType": "string",
"name": "treatment",
"type": "string"
},
{
"indexed": false,
"internalType": "uint256",
"name": "timestamp",
"type": "uint256"
}
],
"name": "RecordAdded",
"type": "event"
},
{
"inputs": [
{
"internalType": "string",
"name": "_diagnosis",
"type": "string"
},
{
"internalType": "string",
"name": "_treatment",
"type": "string"
}
],
"name": "addRecord",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "provider",
"type": "address"
}
],
"name": "authorizeProvider",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "index",
"type": "uint256"
}
],
"name": "getRecord",
"outputs": [
{
"internalType": "string",
"name": "diagnosis",
"type": "string"
},
{
"internalType": "string",
"name": "treatment",
"type": "string"
},
{
"internalType": "uint256",
"name": "timestamp",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getRecordCount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
];
let web3;
let contract;
let isConnected = false;
window.addEventListener('load', async () => {
if (window.ethereum) {
web3 = new Web3(window.ethereum);
try {
await window.ethereum.enable();
isConnected = true;
initContract();
updateConnectButton();
} catch (error) {
console.error("User denied account access");
}
} else if (window.web3) {
web3 = new Web3(web3.currentProvider);
isConnected = true;
initContract();
updateConnectButton();
} else {
console.log('No web3 provider detected');
}
});
function initContract() {
contract = new web3.eth.Contract(contractABI, contractAddress);
}
async function connectWallet() {
if (!isConnected) {
try {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
if (accounts.length > 0) {
isConnected = true;
updateConnectButton();
console.log('Wallet connected');
}
} catch (error) {
console.error(error);
}
} else {
try {
await web3.currentProvider.disconnect();
isConnected = false;
updateConnectButton();
console.log('Wallet disconnected');
} catch (error) {
console.error(error);
}
}
}
function updateConnectButton() {
const connectWalletBtn = document.getElementById('connectWalletBtn');
if (isConnected) {
connectWalletBtn.textContent = 'Connected';
connectWalletBtn.disabled = true; // Disable button when already connected
} else {
connectWalletBtn.textContent = 'Connect Wallet';
connectWalletBtn.disabled = false; // Enable button when not connected
}
}
async function addRecord() {
const diagnosis = document.getElementById('diagnosis').value;
const treatment = document.getElementById('treatment').value;
const accounts = await web3.eth.getAccounts();
await contract.methods.addRecord(diagnosis, treatment).send({ from: accounts[0] });
console.log('Record added');
}
async function getRecords() {
const accounts = await web3.eth.getAccounts();
const count = await contract.methods.getRecordCount().call({ from: accounts[0] });
const recordsList = document.getElementById('recordsList');
recordsList.innerHTML = '';
for (let i = 0; i < count; i++) {
const record = await contract.methods.getRecord(i).call({ from: accounts[0] });
const listItem = document.createElement('li');
listItem.textContent = `Diagnosis: ${record[0]}, Treatment: ${record[1]}, Timestamp: ${record[2]}`;
recordsList.appendChild(listItem);
}
}