-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (51 loc) · 1.78 KB
/
index.js
File metadata and controls
68 lines (51 loc) · 1.78 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
require('dotenv').config();
const express = require('express');
const app = express();
const fileUpload = require('express-fileupload');
app.use(
fileUpload({
extended:true
})
)
app.use(express.static(__dirname));
app.use(express.json());
const path = require("path");
const ethers = require('ethers');
var port = 3000;
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
})
app.get("/index.html", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
})
const API_URL = process.env.API_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
const {abi} = require('./artifacts/contracts/TaskToDo.sol/TaskToDo.json');
const provider = new ethers.providers.JsonRpcProvider(API_URL);
const signer = new ethers.Wallet(PRIVATE_KEY, provider);
const contractInstance = new ethers.Contract(CONTRACT_ADDRESS, abi, signer);
app.post("/addTask", async (req, res) => {
var task = req.body.task;
console.log(task)
async function storeDataInBlockchain(task) {
console.log("Adding the task in the blockchain network...");
const tx = await contractInstance.addTask(task);
await tx.wait();
}
await storeDataInBlockchain(task);
res.send("The task has been registered in the smart contract");
});
app.post("/changeStatus", async (req, res) => {
var id = req.body.id;
async function storeDataInBlockchain(id) {
console.log("Changing the task status...");
const tx = await contractInstance.markAsFinished(id);
await tx.wait();
}
await storeDataInBlockchain(id);
res.send("The task status has been changed in the smart contract");
});
app.listen(port, function () {
console.log("App is listening on port 3000")
});