-
Notifications
You must be signed in to change notification settings - Fork 2
/
token_api.js
124 lines (102 loc) · 3.47 KB
/
token_api.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const THALES_L1_ADDRESS = "0x8947da500eb47f82df21143d0c01a29862a8c3c5";
const BURN_ADDRESS = "0x000000000000000000000000000000000000dEaD";
const MAX_SUPPLY = 100000000;
const circulatingSupplyList = require("./assets/circulating-supply.json");
const erc20Contract = require("./abi/erc20Contract.js");
const { redisClient, connectDefaultRedisClient } = require("./redis/client");
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
app.use(cors());
app.use(express.json());
app.listen(process.env.PORT || 3003, () => {
console.log("Server running on port " + (process.env.PORT || 3003));
});
const Web3 = require("web3");
const Web3Client = new Web3(new Web3.providers.HttpProvider(process.env.INFURA_URL));
const fetch = require("node-fetch");
let tokenMap = new Map();
app.get("/token/price", (_, res) => res.send(tokenMap.get("price") + ""));
app.get("/token/circulatingsupply", (_, res) => res.send(tokenMap.get("circulatingsupply") + ""));
app.get("/token/marketcap", (_, res) => res.send(tokenMap.get("marketcap") + ""));
app.get("/token/totalsupply", (_, res) => res.send(tokenMap.get("totalsupply") + ""));
app.get("/", (_, res) => {
res.sendStatus(200);
});
(async () => {
await connectDefaultRedisClient();
if (process.env.REDIS_URL) {
const tokenMapRaw = await redisClient.get("tokenMap");
console.log("tokenMapRaw:" + tokenMapRaw);
if (tokenMapRaw) {
tokenMap = new Map(JSON.parse(tokenMapRaw));
console.log("tokenMap:" + tokenMap);
}
}
})();
async function processToken() {
try {
const price = await getPrice();
tokenMap.set("price", price);
} catch {
tokenMap.set("price", undefined);
}
try {
const circulatingsupply = getCirculatingSupply();
tokenMap.set("circulatingsupply", circulatingsupply);
} catch {
tokenMap.set("circulatingsupply", undefined);
}
try {
const totalsupply = await getTotalSupply();
tokenMap.set("totalsupply", totalsupply);
} catch {
tokenMap.set("totalsupply", undefined);
}
const price = tokenMap.get("price");
const circulatingsupply = tokenMap.get("circulatingsupply");
if (price && circulatingsupply) {
const marketCap = price * circulatingsupply;
tokenMap.set("marketcap", marketCap);
} else {
tokenMap.set("marketcap", undefined);
}
if (process.env.REDIS_URL) {
redisClient.set("tokenMap", JSON.stringify([...tokenMap]));
}
}
setTimeout(processToken, 1000 * 3);
setInterval(processToken, 1000 * 30);
async function getPrice() {
try {
const gcThalesResponse = await fetch("https://api.coingecko.com/api/v3/simple/price?ids=thales&vs_currencies=usd");
const gcThalesResponseJson = await gcThalesResponse.json();
const price = gcThalesResponseJson.thales.usd;
return price;
} catch (e) {
console.log(e);
}
}
function getCirculatingSupply() {
try {
const startDate = new Date("2021-09-14");
const todaysDate = new Date();
const dif = Math.round(todaysDate - startDate);
const weeks = Math.round(dif / 604800000);
const period = weeks + 1;
return circulatingSupplyList[period];
} catch (e) {
console.log(e);
}
}
async function getTotalSupply() {
try {
const contract = new Web3Client.eth.Contract(erc20Contract, THALES_L1_ADDRESS);
const result = await contract.methods.balanceOf(BURN_ADDRESS).call();
const burnedAmount = Web3Client.utils.fromWei(result);
return MAX_SUPPLY - burnedAmount;
} catch (e) {
console.log(e);
}
}