-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (66 loc) · 2.38 KB
/
index.js
File metadata and controls
73 lines (66 loc) · 2.38 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
const express = require('express');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const node = require('./eth_client.js');
const sc = require('./scripts.js');
const save = require('./data.js');
const query = require('./query.js');
app.get('/api/v1/transactions', async (req, res) => {
try {
const addresses = req.query.addresses;
if (!addresses) {
return res.status(400).json({ error: 'No addresses provided' });
}
const wallets = addresses.split(",")
if (wallets.length === 0) {
return res.status(400).json({ error: 'No addresses provided' });
}
const transactions = await query.getTransactionsByAddresses(wallets);
res.json(transactions);
} catch (error) {
console.error('Error retrieving transactions:', error);
res.status(500).json({ error: 'An error occurred' });
}
});
app.get('/api/v1/transaction/:tx_hash', async (req, res) => {
try {
const txHash = req.params.tx_hash;
if (!txHash) {
return res.status(400).json({ error: 'No txHash provided' });
}
const transactions = await query.getTransactionByHash(txHash);
res.json(transactions);
} catch (error) {
console.error('Error retrieving transactions:', error);
res.status(500).json({ error: 'An error occurred' });
}});
app.get('/health', async (req, res) => {
res.status(200).json();
});
async function main() {
const chainId = await node.getChainId();
let fromBlock = process.env.FROM_BLOCK;
let toBlock = process.env.TO_BLOCK;
let indexFrom = await query.getLastIndexedBlock(chainId);
if (!indexFrom) {
await save.startIndexingFromBlock(fromBlock, chainId);
indexFrom = fromBlock;
}
while (indexFrom < toBlock) {
const blockWithTxs = await node.getBlockWithRetries(indexFrom, 5);
if (blockWithTxs == null) {
continue
}
const blockId = await save.storeBlock(blockWithTxs, chainId);
const receipts = await node.getTransactionReceiptsWithRetries(blockWithTxs);
let res = await save.storeTransaction(blockWithTxs, blockId, chainId);
let ress = await save.storeTransactionReceipts(receipts, chainId);
await save.updateBlockIndexed(indexFrom, chainId)
indexFrom++;
}
}
main();
app.listen(8080, () => {
console.log('Server is running on port 8080');
});