-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (69 loc) · 1.65 KB
/
index.js
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
import { serve } from "bun";
const UPSTREAM_API = process.env.MEMPOOL_API_URL;
async function fetchFees() {
try {
const response = await fetch(UPSTREAM_API);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return {
economyFee: data.economyFee || '-',
hourFee: data.hourFee || '-',
halfHourFee: data.halfHourFee || '-',
fastestFee: data.fastestFee || '-'
};
} catch (error) {
console.error("Error fetching fees:", error);
return {
economyFee: '-',
hourFee: '-',
halfHourFee: '-',
fastestFee: '-'
};
}
}
function formatResponse(fees) {
return {
type: "four-stats",
refresh: "5s",
link: "",
items: [
{
title: "No priority",
text: fees.economyFee,
subtext: "sat/vB"
},
{
title: "Low priority",
text: fees.hourFee,
subtext: "sat/vB"
},
{
title: "Medium priority",
text: fees.halfHourFee,
subtext: "sat/vB"
},
{
title: "High priority",
text: fees.fastestFee,
subtext: "sat/vB"
}
]
};
}
console.log("Running Mempool Widget API server...");
serve({
port: 3000,
async fetch(req) {
const url = new URL(req.url);
if (url.pathname === "/widgets/fees") {
const fees = await fetchFees();
const formattedResponse = formatResponse(fees);
return new Response(JSON.stringify(formattedResponse), {
headers: { "Content-Type": "application/json" }
});
}
return new Response("Not Found", { status: 404 });
}
});