-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
65 lines (65 loc) · 2.13 KB
/
index.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
const Express = require('express');
const cors = require('cors');
const net = require('net');
const Web3 = require('web3');
const dotenv = require('dotenv');
dotenv.config({});
const ipc = process.env.IPC || '/mnt/geth/geth.ipc';
const web3 = new Web3(new Web3.providers.IpcProvider(ipc, net));
const app = Express();
app.use(Express.json({ limit: '50mb' }));
app.use(cors());
app.post('/nonces', async (request, response) => {
const accounts = request.body.accounts;
const tasks = [];
accounts.forEach((account) => {
tasks.push(
new Promise(async (resolve, reject) => {
try {
const count = await web3.eth.getTransactionCount(account);
resolve({ account, count });
} catch (error) {
resolve({ account, count: 0 });
}
})
)
});
response.send(await Promise.all(tasks));
});
app.post('/contract-codes', async (request, response) => {
const contracts = request.body.contracts;
const tasks = [];
contracts.forEach((contract) => {
tasks.push(
new Promise(async (resolve, reject) => {
try {
const code = await web3.eth.getCode(contract);
resolve({ contract, code });
} catch (error) {
resolve({ contract, code: "0x" });
}
})
)
});
response.send(await Promise.all(tasks));
});
app.post('/transaction-receipts', async (request, response) => {
const hashes = request.body.hashes;
const tasks = [];
hashes.forEach((hash) => {
tasks.push(
new Promise(async (resolve, reject) => {
try {
const receipt = await web3.eth.getTransactionReceipt(hash);
resolve({ hash, receipt });
} catch (error) {
resolve({ hash, receipt: null });
}
})
)
});
response.send(await Promise.all(tasks));
});
const port = process.env.PORT || 80;
app.listen(port);
console.log("listening on port " + port);