-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
153 lines (128 loc) · 5.09 KB
/
server.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
//Initiallising node modules
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var fs = require('fs');
// Body Parser Middleware
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(bodyParser.urlencoded({ 'extended': 'true' })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
//CORS Middleware
app.use(function (req, res, next) {
//Enabling CORS
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
next();
});
//////////////////////////////////////////// DAPP ///////////////////////////////////////
// Read the config.
var serverConfig = require('./serverConfig')
// web3.js library.
Web3 = require("web3");
// Url of Ethereum private network.
if (!serverConfig.ChainIpAddr) {
/*
'\x1b[31m' - Red Color
'\x1b[32m' - Green Color
'\x1b[0m' - Reset color
*/
console.log('\x1b[31m', '!!! ERROR !!! - No Ethereum Chain IP address defined.', '\x1b[0m');
}
else {
var privateNodeUrl = "http://" + serverConfig.ChainIpAddr + ":" + serverConfig.ChainPortNo;
web3 = new Web3(new Web3.providers.HttpProvider(privateNodeUrl));
var server = app.listen(process.env.PORT || serverConfig.WebPortNo, function () {
var port = server.address().port;
console.info('\x1b[32m', "App running on - localhost:" + port, '\x1b[0m');
});
}
// ----------------------------------------------------- API ----------------------------------------------------- //
app.get("/api/getChainOverview", function (req, res) {
// console.log('inside api service method : getlatestBlock()');
var chainOverview = {};
var gasPrice, hashRate;
web3.eth.getGasPrice((err, price) => {
chainOverview['gasPrice'] = price;
web3.eth.getHashrate((err, rate) => {
chainOverview['hashRate'] = rate;
chainOverview['lastBlock'] = web3.eth.blockNumber;
// console.log('chain overview---')
// console.log(chainOverview);
res.send(chainOverview);
});
});
});
app.get("/api/getAllAccountDetails", function (req, res) {
// console.log('inside api service method : getAllAccountDetails()');
// Account List
var accounts = web3.eth.accounts;
var accountDetails = accounts.map(function (item) {
return {
address: item,
balance: web3.fromWei(web3.eth.getBalance(item))
}
});
res.send(accountDetails);
});
app.get("/api/getAllBlocks", function (req, res) {
// console.log('inside api service method : getAllBlocks()');
var n = web3.eth.blockNumber;
// web3.eth.getBlockNumber(function(data){n=data});
// console.log("Block number : "+ n);
var blocks = [];
for (var i = 0; i < n; i++) {
var block = web3.eth.getBlock(i, true);
if (block.transactions.length) {
blocks.push(block);
}
}
var strBlocks = JSON.stringify(blocks);
// console.log("Writing blocks to txt file.");
fs.writeFile('./Database/Blocks.txt', strBlocks, function (err) {
if (err) {
console.log("ERROR writing blocks to txt file...!!!");
res.send(err);
}
});
// console.log("Returning blocks--------");
res.send(blocks);
});
app.get("/api/getAllBlocksFromFile", function (req, res) {
// console.log('inside api service method : getAllBlocksFromFile()');
fs.readFile('./Database/Blocks.txt', 'utf8', function (err, contents) {
if (err) {
console.log("ERROR reading txt file!!!");
res.send(err);
}
else {
// console.log('--------- String Content from TXT file ---------');
// console.log(contents);
var jsonObj = JSON.parse(contents);
// console.log('--------- JSON Content from TXT file ---------');
// console.log(jsonObj);
// send data to front end
// console.log("Returning blocks from TXT File --------");
res.send(jsonObj);
}
});
});
app.get("/api/getTransactionsFromBlock/:id", function (req, res) {
// console.log('inside api service method : getTransactionsFromBlock() : param : ' + req.params.id);
var id = req.params.id;
var txs = [];
var tx;
var n = web3.eth.getBlockTransactionCount(id)
for (var i = 0; i < n; i++) {
tx = web3.eth.getTransactionFromBlock(id, i);
tx["value"] = web3.fromWei(tx["value"]);
txs.push(tx);
}
res.send(txs);
});
app.get("/api/getBlock/:addr", function (req, res) {
// console.log('inside api service method : getBlock() : param : ' + req.params.addr);
var block = web3.eth.getBlock(req.params.addr);
res.send(block);
});