-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
163 lines (156 loc) · 4.09 KB
/
main.js
File metadata and controls
163 lines (156 loc) · 4.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
let WALLET_CONNECTED = "";
let contractAddress = ""; // in the video it is 0xdD3375552d7eDd5Bf695bF1143515697733d6FCa
let contractAbi = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "string",
"name": "_desc",
"type": "string"
}
],
"name": "addTask",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getAllTasks",
"outputs": [
{
"components": [
{
"internalType": "string",
"name": "desc",
"type": "string"
},
{
"internalType": "enum TaskToDo.TaskStatus",
"name": "status",
"type": "uint8"
}
],
"internalType": "struct TaskToDo.Task[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
}
],
"name": "getTask",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
},
{
"internalType": "enum TaskToDo.TaskStatus",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getTaskCount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
}
],
"name": "markAsFinished",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "tasks",
"outputs": [
{
"internalType": "string",
"name": "desc",
"type": "string"
},
{
"internalType": "enum TaskToDo.TaskStatus",
"name": "status",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
}
];
const connectMetamask = async() => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
WALLET_CONNECTED = await signer.getAddress();
var element = document.getElementById("metamasknotification");
element.innerHTML = "Metamask is connected " + WALLET_CONNECTED;
}
const getAllTasks = async() => {
if(WALLET_CONNECTED != 0) {
var p3 = document.getElementById("p3");
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
const contractInstance = new ethers.Contract(contractAddress, contractAbi, signer);
p3.innerHTML = "Please wait, getting all the tasks from the smart contract";
var tasks = await contractInstance.getAllTasks();
var table = document.getElementById("myTable");
for (let i = 0; i < tasks.length; i++) {
var row = table.insertRow();
var idCell = row.insertCell();
var descCell = row.insertCell();
var statusCell = row.insertCell();
const status = tasks[i].status == 0 ? "Pending" : "Finished";
idCell.innerHTML = i;
descCell.innerHTML = tasks[i].desc;
statusCell.innerHTML = status;
}
p3.innerHTML = "The tasks are updated"
}
else {
var p3 = document.getElementById("p3");
p3.innerHTML = "Please connect metamask first";
}
}