-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.ts
54 lines (44 loc) · 1.55 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
import { createSolanaRpcGraphQL } from '@solana/rpc-graphql';
import { createSolanaRpc } from '@solana/web3.js';
import bodyParser from 'body-parser';
import express from 'express';
const PORT = process.env.SERVER_PORT || 3001;
const RPC_ENDPOINT = process.env.RPC_ENDPOINT || 'http://127.0.0.1:8899';
const SERVER_ERROR = 'SERVER ERROR';
const app = express();
app.use(bodyParser.json());
const rpc = createSolanaRpc(RPC_ENDPOINT);
const rpcGraphQL = createSolanaRpcGraphQL(rpc);
app.post('/api/graphql', async (req, res) => {
const { source, variableValues } = req.body;
const response = await rpcGraphQL.query(source, variableValues);
if (response.errors) {
response.errors.forEach(e => console.error(e));
res.status(500).send(SERVER_ERROR);
}
if (response.data) {
res.send(
JSON.stringify(response.data, (_, value) => {
if (typeof value === 'bigint') {
return { __bigint: value.toString() };
}
return value;
}),
);
}
return;
});
app.post('/api/getSignaturesForAddress', async (req, res) => {
try {
const { address } = req.body;
const results = await rpc.getSignaturesForAddress(address).send();
const signatures = results.map(result => result.signature);
res.status(200).send(JSON.stringify(signatures));
} catch (e) {
console.error(e);
res.status(500).send(SERVER_ERROR);
}
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});