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
import { hexToUtf8 } from "https://esm.town/v/ryanwaits/hexToUtf8";
export const satsNameApi = async (req: Request) => {
const { Hono } = await import("npm:hono");
const app = new Hono();
let events = [];
app.post("/api/events", async (c) => {
const body = await c.req.json();
const { apply } = body;
let events = [];
apply.forEach((item: any) => {
item.transactions.forEach((transaction: any) => {
if (transaction.metadata && transaction.metadata.ordinal_operations) {
transaction.metadata.ordinal_operations.forEach((operation: any) => {
if (operation.inscription_revealed) {
let op_data = operation.inscription_revealed;
if (op_data.content_type === "text/plain;charset=utf-8") {
const decodedContent = hexToUtf8(
op_data.content_bytes,
);
try {
let content = JSON.parse(decodedContent);
if (content["p"] === "sns") {
const [name, namespace] = content["name"].split(".");
const snsData = {
current_owner: op_data.inscriber_address,
inscription_id: op_data.inscription_id,
name,
namespace,
block_height: item.block_identifier.index,
};
events.push(snsData);
}
}
catch (e: any) {
if (!(e instanceof SyntaxError)) {
console.log(`Error parsing JSON: ${e.message}`);
}
}
}
}
else if (operation.inscription_transferred) {
let op_data = operation.inscription_transferred;
let event = events.find((event: any) =>
event.inscription_id === op_data.inscription_id
);
if (event) {
event.current_owner = op_data.updated_address;
}
}
});
}
});
});
return c.json({ events });
});
return app.fetch(req);
};