-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
105 lines (93 loc) · 3.03 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
import express, { Request, Response } from "express";
import {
fetchDiscordChannel,
initClient as initDiscordClient,
} from "lib/discord";
import initWorkers from "workers/initWorkers";
import { newConnection } from "lib/solana/connection";
import dotenv from "dotenv";
import notifyDiscordSale, { getStatus } from "lib/discord/notifyDiscordSale";
import { loadConfig } from "config";
import { Worker } from "workers/types";
import notifyNFTSalesWorker from "workers/notifyNFTSalesWorker";
import { parseNFTSale } from "./lib/marketplaces";
import { ParsedConfirmedTransaction } from "@solana/web3.js";
const port = process.env.PORT || 4000;
(async () => {
try {
const result = dotenv.config();
if (result.error) {
throw result.error;
}
const config = loadConfig();
const web3Conn = newConnection();
const discordClient = await initDiscordClient();
const server = express();
server.get("/", (req, res) => {
const { totalNotified, lastNotified } = getStatus();
res.send(`
${config.subscriptions.map(
(s) =>
`Watching the address ${s.mintAddress} at discord channel #${s.discordChannelId} for NFT sales.<br/>`
)}
Total notifications sent: ${totalNotified}<br/>
${
lastNotified
? `Last notified at: ${lastNotified.toISOString()}<br/>`
: ""
}
${`Current UTC time: ${new Date().toISOString()}`}
`);
});
server.get("/test-sale-tx", async (req, res) => {
const signature = (req.query["signature"] as string) || "";
if (!signature) {
res.send(`no signature in query param`);
return;
}
let tx: ParsedConfirmedTransaction | null = null;
try {
tx = await web3Conn.getParsedConfirmedTransaction(signature);
} catch (e) {
console.log(e);
res.send(`Get transaction failed, check logs for error.`);
return;
}
if (!tx) {
res.send(`No transaction found for ${signature}`);
return;
}
const nftSale = await parseNFTSale(web3Conn, tx);
if (!nftSale) {
res.send(
`No NFT Sale detected for tx: ${signature}\n${JSON.stringify(tx)}`
);
return;
}
const channelId = (req.query["channelId"] as string) || "";
if (channelId) {
const channel = await fetchDiscordChannel(discordClient, channelId);
if (channel) {
await notifyDiscordSale(discordClient, channel, nftSale);
}
}
res.send(`NFT Sales parsed: \n${JSON.stringify(nftSale)}`);
});
server.listen(port, (err?: any) => {
if (err) throw err;
console.log(
`> Ready on http://localhost:${port} - env ${process.env.NODE_ENV}`
);
});
const workers: Worker[] = config.subscriptions.map((s) => {
return notifyNFTSalesWorker(discordClient, web3Conn, {
discordChannelId: s.discordChannelId,
mintAddress: s.mintAddress,
});
});
initWorkers(workers);
} catch (e) {
console.error(e);
process.exit(1);
}
})();