-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
216 lines (195 loc) · 6.29 KB
/
index.js
File metadata and controls
216 lines (195 loc) · 6.29 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const express = require('express');
const csv = require('csv-parser');
const fs = require('fs');
const moment = require('moment');
const { Web3 } = require('web3');
const app = express();
const port = 3000;
// CSV column name mappings
//Node Rewards,Operating Funds,Community Funds,User Rewards,Initial Liquidity & MM Fund,Fundraising,Core Team & Consultants,Total,Circulating Percent
const COLUMNS = {
DATE: 'Date',
MONTH: 'Month',
NODE_REWARDS: 'Node Rewards',
OPERATING_FUNDS: 'Operating Funds',
COMMUNITY_FUNDS: 'Community Funds',
USER_REWARDS: 'User Rewards',
INITIAL_LIQUIDITY_AND_MM_FUND: 'Initial Liquidity & MM Fund',
FUNDRAISING: 'Fundraising',
CORE_TEAM_AND_CONSULTANTS: 'Core Team & Consultants',
TOTAL: 'Total',
CIRCULATING_PERCENT: 'Circulating Percent',
};
let tokenomicsData = [];
fs.createReadStream('tokenomics.csv')
.pipe(csv())
.on('data', (row) => {
// Process number formats
const processedRow = {};
Object.entries(row).forEach(([key, value]) => {
if (key === COLUMNS.DATE || key === COLUMNS.MONTH) {
processedRow[key] = value;
} else {
processedRow[key] = Number(value.replace(/,/g, ''));
}
});
tokenomicsData.push(processedRow);
});
function formatTimeRange(timestamp, type, tokenomicsData) {
const currentDate = moment(timestamp).format('YYYY/MM/DD');
const firstDate = moment(
tokenomicsData[0][COLUMNS.DATE],
'YYYY/MM/DD'
).format('YYYY/MM/DD');
const lastDate = moment(
tokenomicsData[tokenomicsData.length - 1][COLUMNS.DATE],
'YYYY/MM/DD'
).format('YYYY/MM/DD');
switch (type) {
case 'before_first':
return `Before ${firstDate} (Current: ${currentDate})`;
case 'after_last':
return `After ${lastDate} (Current: ${currentDate})`;
case 'between':
let prevDate, nextDate;
for (let i = 0; i < tokenomicsData.length - 1; i++) {
const date1 = moment(
tokenomicsData[i][COLUMNS.DATE],
'YYYY/MM/DD'
).format('YYYY/MM/DD');
const date2 = moment(
tokenomicsData[i + 1][COLUMNS.DATE],
'YYYY/MM/DD'
).format('YYYY/MM/DD');
if (currentDate >= date1 && currentDate < date2) {
prevDate = date1;
nextDate = date2;
break;
}
}
return `${prevDate} - ${nextDate} (Current: ${currentDate})`;
}
}
async function formatSupplyResponse(
timestamp,
data,
timeRangeType,
tokenomicsData
) {
let totalSupply = data[COLUMNS.TOTAL];
let circulatingSupply = totalSupply;
return {
timestamp,
timeRange: formatTimeRange(timestamp, timeRangeType, tokenomicsData),
totalSupply: tokenomicsData[tokenomicsData.length - 1][COLUMNS.TOTAL],
circulatingSupply: circulatingSupply,
circulatingPercent:
circulatingSupply /
tokenomicsData[tokenomicsData.length - 1][COLUMNS.TOTAL],
};
}
async function calculateSupply(timestamp) {
const currentDate = moment(timestamp);
if (!currentDate.isValid()) {
throw new Error('Invalid timestamp format. Please use ISO 8601 format.');
}
// If timestamp is earlier than the first record
const firstDate = moment(tokenomicsData[0][COLUMNS.DATE], 'YYYY/MM/DD');
if (currentDate.isBefore(firstDate)) {
return formatSupplyResponse(
timestamp,
{
[COLUMNS.DATE]: data[COLUMNS.DATE],
[COLUMNS.MONTH]: data[COLUMNS.MONTH],
[COLUMNS.NODE_REWARDS]: data[COLUMNS.NODE_REWARDS],
[COLUMNS.OPERATING_FUNDS]: data[COLUMNS.OPERATING_FUNDS],
[COLUMNS.COMMUNITY_FUNDS]: data[COLUMNS.COMMUNITY_FUNDS],
[COLUMNS.USER_REWARDS]: data[COLUMNS.USER_REWARDS],
[COLUMNS.INITIAL_LIQUIDITY_AND_MM_FUND]:
data[COLUMNS.INITIAL_LIQUIDITY_AND_MM_FUND],
[COLUMNS.FUNDRAISING]: data[COLUMNS.FUNDRAISING],
[COLUMNS.CORE_TEAM_AND_CONSULTANTS]:
data[COLUMNS.CORE_TEAM_AND_CONSULTANTS],
[COLUMNS.TOTAL]: data[COLUMNS.TOTAL],
[COLUMNS.CIRCULATING_PERCENT]: data[COLUMNS.CIRCULATING_PERCENT],
},
'before_first',
tokenomicsData
);
}
// If timestamp is after or equal to the last record
const lastDate = moment(
tokenomicsData[tokenomicsData.length - 1][COLUMNS.DATE],
'YYYY/MM/DD'
);
if (currentDate.isSameOrAfter(lastDate)) {
return formatSupplyResponse(
timestamp,
tokenomicsData[tokenomicsData.length - 1],
'after_last',
tokenomicsData
);
}
// Find the relevant date range
const relevantData = tokenomicsData.find((row, index) => {
const currentRowDate = moment(row[COLUMNS.DATE], 'YYYY/MM/DD');
const nextRowDate =
index < tokenomicsData.length - 1
? moment(tokenomicsData[index + 1][COLUMNS.DATE], 'YYYY/MM/DD')
: null;
return (
currentDate.isSameOrAfter(currentRowDate) &&
(nextRowDate === null || currentDate.isBefore(nextRowDate))
);
});
return formatSupplyResponse(
timestamp,
relevantData,
'between',
tokenomicsData
);
}
app.use((req, res, next) => {
const { timestamp } = req.query;
if (timestamp) {
const date = moment(timestamp);
if (!date.isValid()) {
return res.status(400).json({
error: 'Invalid timestamp value',
});
}
}
next();
});
app.get('/', async (req, res) => {
const timestamp = req.query.timestamp || new Date().getTime();
const supply = await calculateSupply(timestamp);
res.json(supply);
});
app.get('/circulatingSupply', async (req, res) => {
const timestamp = req.query.timestamp || new Date().getTime();
const supply = await calculateSupply(timestamp);
res.json(supply.circulatingSupply);
});
app.get('/circulatingSupply/json', async (req, res) => {
const timestamp = req.query.timestamp || new Date().getTime();
const supply = await calculateSupply(timestamp);
res.json({
result: supply.circulatingSupply,
});
});
app.get('/totalSupply', async (req, res) => {
const timestamp = req.query.timestamp || new Date().getTime();
const supply = await calculateSupply(timestamp);
res.json(supply.totalSupply);
});
app.get('/totalSupply/json', async (req, res) => {
const timestamp = req.query.timestamp || new Date().getTime();
const supply = await calculateSupply(timestamp);
res.json({
result: supply.totalSupply,
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});