-
Notifications
You must be signed in to change notification settings - Fork 18
/
timestamps.js
72 lines (59 loc) · 1.79 KB
/
timestamps.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
const db = require('./db');
async function getTimestamp(hash) {
return await db.knex('timestamps').where({ hash }).select();
}
async function getTimestamps(hashes) {
const returnHashes = {};
try {
const dbHashes = await db.knex('timestamps').whereIn('hash', hashes).select();
hashes.forEach(hash => {
const dbResult = dbHashes.find(dbHash => dbHash.hash === hash);
returnHashes[hash] = dbResult ? dbResult.timestamp : null;
});
return returnHashes;
} catch (err) {
console.log(`Error retrieving timestamps for `, hashes);
console.log(err);
return [];
}
}
/**
* Morph the normal Nano node responses to include timestamps
*/
async function mapAccountHistory(nodeResult) {
if (!nodeResult || !nodeResult.history) return nodeResult;
const hashes = nodeResult.history.map(tx => tx.hash);
const txHashes = await getTimestamps(hashes);
nodeResult.history = nodeResult.history.map(tx => {
tx.timestamp = txHashes[tx.hash];
return tx;
});
return nodeResult;
}
async function mapBlocksInfo(blockHashes, nodeResult) {
if (!nodeResult || !nodeResult.blocks) return nodeResult;
const txHashes = await getTimestamps(blockHashes);
for (let block in nodeResult.blocks) {
nodeResult.blocks[block].timestamp = txHashes[block] || null;
}
return nodeResult;
}
async function mapPending(nodeResult) {
if (!nodeResult || !nodeResult.blocks) return nodeResult;
const pendingHashes = [];
for (let block in nodeResult.blocks) {
pendingHashes.push(block);
}
const txHashes = await getTimestamps(pendingHashes);
for (let block in nodeResult.blocks) {
nodeResult.blocks[block].timestamp = txHashes[block] || null;
}
return nodeResult;
}
module.exports = {
getTimestamp,
getTimestamps,
mapAccountHistory,
mapBlocksInfo,
mapPending
};