-
Notifications
You must be signed in to change notification settings - Fork 12
/
txStatus.ts
65 lines (55 loc) · 1.87 KB
/
txStatus.ts
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
import config from "config";
import axios from "axios";
import { Pool } from "pg";
import { Request, Response } from "express";
const signedTxQueueEndpoint = config.get("server.signedTxQueueEndpoint");
const txStatusQuery = `
SELECT encode(tx.hash, 'hex') tx_id,
(highest_block.block_no - block.block_no + 1) depth
FROM tx
INNER JOIN block on tx.block_id = block.id
CROSS JOIN (SELECT MAX(block_no) block_no FROM block) as highest_block
WHERE encode(tx.hash, 'hex') = any(($1)::varchar array);
`;
export const handleTxStatus =
(pool: Pool) => async (req: Request, res: Response) => {
if (!req.body || !req.body.txHashes) {
throw new Error("error, no tx txHashes informed.");
}
if (!Array.isArray(req.body.txHashes)) {
throw new Error("'txHashes' should be an array.");
}
const txHashes: string[] = req.body.txHashes;
if (txHashes.length > 100) {
throw new Error("Max limit of 100 tx txHashes exceeded.");
}
if (txHashes.length === 0) {
throw new Error("error, at least 1 tx id should be informed.");
}
const result = await pool.query(txStatusQuery, [txHashes]);
const response: any = {};
const depth: { [key: string]: number } = {};
for (const item of result.rows) {
depth[item.tx_id] = item.depth;
}
response.depth = depth;
if (config.get("usingQueueEndpoint") === "true") {
try {
const result = await axios({
method: "post",
url: `${signedTxQueueEndpoint}api/getTxsStatus`,
data: {
txHashes: txHashes,
},
});
const submissionStatus: { [key: string]: string } = {};
for (const status of result.data) {
submissionStatus[status.id] = status.status;
}
response.submissionStatus = submissionStatus;
} finally {
// ignore errors
}
}
res.send(response);
};