-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (51 loc) · 1.57 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
const axios = require("axios");
const cors = require("cors");
const express = require("express");
require("dotenv").config();
const app = express();
const API = process.env.API_ENDPOINT;
const PORT = process.env.PORT || 3000;
const DIVIDE_BY = `1e${process.env.COIN_DECIMALS || 1}`;
// Use cors middleware to enable CORS
app.use(cors());
async function fetchData(endpoint) {
try {
const response = await axios.get(`${API}${endpoint}`);
return response.data;
} catch (error) {
console.error(`Error fetching data from ${endpoint}:`, error.message);
throw new Error("Internal Server Error");
}
}
async function fetchTotalSupply() {
const data = await fetchData(
`/cosmos/bank/v1beta1/supply/${process.env.COIN_DENOM}`
);
return data.amount.amount / DIVIDE_BY;
}
async function fetchCirculatingSupply() {
const communityPool = (
await fetchData("/cosmos/distribution/v1beta1/community_pool")
).pool[0].amount;
const totalSupply = await fetchTotalSupply();
return totalSupply - communityPool / DIVIDE_BY;
}
app.get("/total-supply", async (_, res) => {
try {
const totalSupply = await fetchTotalSupply();
res.json(totalSupply);
} catch (error) {
res.status(500).json({ error: "Internal Server Error" });
}
});
app.get("/circulating-supply", async (_, res) => {
try {
const circulatingSupply = await fetchCirculatingSupply();
res.json(circulatingSupply);
} catch (error) {
res.status(500).json({ error: "Internal Server Error" });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});