-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
158 lines (137 loc) · 5.57 KB
/
server.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
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
153
154
155
156
157
158
import express from 'express';
import cors from 'cors';
import { query, setUpDB } from './db';
import { Database } from '@tableland/sdk';
import path from 'path';
export type Request = express.Request;
export type Response = express.Response;
let db: Database;
const app = express()
const port = 3002
//app.use(cors());
let transactionsTable: string, blocksTable: string, accountsTable: string, crossChainTable: string;
const setup = async () => {
db = await setUpDB();
}
setup();
app.get('/api/crosschain', async (req: Request, res: Response) => {
const transactionId = req.query.transactionId as string;
try {
let statement = `SELECT * FROM ${crossChainTable} WHERE id = "${transactionId}"`;
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting crosschain: " + e);
res.status(500).json({ error: 'An error occurred while querying the crosschain table' });
}
});
app.get('/api/crosschains', async (req: Request, res: Response) => {
let block_id = req.query.block_id as string;
try {
let statement = `SELECT * FROM ${crossChainTable} WHERE block_id = "${block_id}"`;
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting crosschain: " + e);
res.status(500).json({ error: 'An error occurred while querying the crosschain table' });
}
});
app.get('/api/lastcrosschains', async (req: Request, res: Response) => {
try {
let statement = `SELECT * FROM ${crossChainTable} ORDER BY height DESC LIMIT 10`;
console.log(statement);
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting crosschain: " + e);
res.status(500).json({ error: 'An error occurred while querying the crosschain table' });
}
});
app.get('/api/transaction', async (req: Request, res: Response) => {
const transactionId = req.query.transactionId as string;
try {
let statement = `SELECT * FROM ${transactionsTable} WHERE id = "${transactionId}"`;
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting transactions: " + e);
res.status(500).json({ error: 'An error occurred while querying the transactions table' });
}
});
app.get('/api/transactions', async (req: Request, res: Response) => {
let block_id = req.query.block_id as string;
try {
let statement = `SELECT * FROM ${transactionsTable} WHERE block_id = "${block_id}"`;
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting transactions: " + e);
res.status(500).json({ error: 'An error occurred while querying the transactions table' });
}
});
app.get('/api/lasttransactions', async (req: Request, res: Response) => {
try {
let statement = `SELECT * FROM ${transactionsTable} ORDER BY block_height DESC LIMIT 10`;
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting transactions: " + e);
res.status(500).json({ error: 'An error occurred while querying the transactions table' });
}
});
app.get('/api/block', async (req: Request, res: Response) => {
const block_id = req.query.block_id as string;
try {
// FIX: your query is wrong.
let statement = `SELECT * FROM ${blocksTable} WHERE id = "${block_id}"`;
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting blocks: " + e);
res.status(500).json({ error: 'An error occurred while querying the blocks table' });
}
});
app.get('/api/lastblocks', async (req: Request, res: Response) => {
try {
let statement = `SELECT * FROM ${blocksTable} ORDER BY height DESC LIMIT 10`;
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting blocks: " + e);
res.status(500).json({ error: 'An error occurred while querying the blocks table' });
}
});
app.get('/api/account', async (req: Request, res: Response) => {
const accountId = req.query.accountId as string;
try {
let statement = `SELECT * FROM ${accountsTable} WHERE address = "${accountId}"`;
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting accounts: " + e);
res.status(500).json({ error: 'An error occurred while querying the accounts table' });
}
});
app.get('/api/ownedtransactions', async (req: Request, res: Response) => {
const accountId = req.query.accountId as string;
try {
let statement = `SELECT * FROM ${transactionsTable} WHERE "from" = "${accountId}" or "to" = "${accountId}"`;
const results = await query(db, statement);
res.json(results);
} catch (e) {
console.log("Error getting transactions: " + e);
res.status(500).json({ error: 'An error occurred while querying the transactions table' });
}
});
app.use(express.static(path.resolve(__dirname, '..', 'filecoin-indexer-frontend', 'build')));
app.get('/*', (req: Request, res: Response) => {
res.sendFile(path.resolve(__dirname, '..', 'filecoin-indexer-frontend', 'build', 'index.html'));
})
const args = process.argv.slice(2);
transactionsTable = args[0];
blocksTable = args[1];
accountsTable = args[2];
crossChainTable = args[3];
app.listen(port, () => {
console.log(`API listening on port ${port}`)
});