-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeoretical.js
More file actions
337 lines (288 loc) · 10.2 KB
/
Copy paththeoretical.js
File metadata and controls
337 lines (288 loc) · 10.2 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Constants
const USD_TOTAL_DEBT = 100000000000000; // $100 trillion
const BTC_HARD_CAP = 21000000; // 21 million
const ERG_HARD_CAP = 97000000; // 97 million
// Data store
let cryptoData = {
btc: {
price: 0,
supply: 0,
timestamp: null
},
erg: {
price: 0,
supply: 0,
timestamp: null
},
doge: {
price: 0,
supply: 0,
timestamp: null
}
};
// Number formatting utilities
function formatPrice(num) {
if (num === 0 || num === null || num === undefined) return '-';
if (num >= 1000000) {
return '$' + (num / 1000000).toFixed(2) + 'M';
} else if (num >= 1000) {
return '$' + num.toLocaleString('en-US', {
minimumFractionDigits: 0,
maximumFractionDigits: 0
});
} else if (num >= 1) {
return '$' + num.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
} else {
return '$' + num.toFixed(4);
}
}
function formatMultiplier(num) {
if (num === 0 || num === null || num === undefined) return '-';
if (num >= 1000000) {
return (num / 1000000).toFixed(2) + 'M×';
} else if (num >= 1000) {
return (num / 1000).toFixed(1) + 'K×';
} else {
return num.toFixed(1) + '×';
}
}
function formatSupply(num) {
if (num === 0 || num === null || num === undefined) return '-';
if (num >= 1e12) {
return (num / 1e12).toFixed(1) + 'T';
} else if (num >= 1e9) {
return (num / 1e9).toFixed(1) + 'B';
} else if (num >= 1e6) {
return (num / 1e6).toFixed(1) + 'M';
} else {
return num.toLocaleString('en-US', { maximumFractionDigits: 0 });
}
}
function getTimestamp() {
const now = new Date();
return now.toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
}
// API calls
async function fetchCryptoData() {
try {
const response = await fetch('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin,ergo,dogecoin&order=market_cap_desc&per_page=3&page=1&sparkline=false&locale=en');
if (!response.ok) {
throw new Error('Failed to fetch crypto data');
}
const data = await response.json();
const btcData = data.find(coin => coin.id === 'bitcoin');
const ergData = data.find(coin => coin.id === 'ergo');
const dogeData = data.find(coin => coin.id === 'dogecoin');
return {
btc: {
supply: btcData?.circulating_supply || 0,
price: btcData?.current_price || 0
},
erg: {
supply: ergData?.circulating_supply || 0,
price: ergData?.current_price || 0
},
doge: {
supply: dogeData?.circulating_supply || 0,
price: dogeData?.current_price || 0
}
};
} catch (error) {
console.error('Error fetching crypto data:', error);
throw error;
}
}
// Calculate theoretical values
function calculateTheoretical(currentPrice, hardCap) {
// Theoretical max = Total USD debt / Hard cap
const theoreticalMax = USD_TOTAL_DEBT / hardCap;
// Upside = Theoretical max / Current price
const upside = theoreticalMax / currentPrice;
return {
theoreticalMax,
upside
};
}
// Calculate cross-crypto theoretical values
function calculateCrossTheoretical(targetHardCap, targetCurrentPrice, sourceMarketCap) {
// If target crypto absorbed source crypto's market cap
// New theoretical price = Source market cap / Target hard cap
const theoreticalPrice = sourceMarketCap / targetHardCap;
// Upside = Theoretical price / Current price
const upside = theoreticalPrice / targetCurrentPrice;
return {
theoreticalPrice,
upside
};
}
// Main data fetch function
async function fetchData() {
showLoading(true);
hideError();
try {
const data = await fetchCryptoData();
const timestamp = getTimestamp();
// Update data store
cryptoData.btc = {
price: data.btc.price,
supply: data.btc.supply,
timestamp: timestamp
};
cryptoData.erg = {
price: data.erg.price,
supply: data.erg.supply,
timestamp: timestamp
};
cryptoData.doge = {
price: data.doge.price,
supply: data.doge.supply,
timestamp: timestamp
};
render();
showLoading(false);
} catch (error) {
console.error('Error in fetchData:', error);
showError();
showLoading(false);
}
}
// Render function
function render() {
// Calculate market caps
const btcMarketCap = cryptoData.btc.price * cryptoData.btc.supply;
const ergMarketCap = cryptoData.erg.price * cryptoData.erg.supply;
const dogeMarketCap = cryptoData.doge.price * cryptoData.doge.supply;
// Bitcoin
const btcTheoretical = calculateTheoretical(cryptoData.btc.price, BTC_HARD_CAP);
const btcAbsorbErg = calculateCrossTheoretical(BTC_HARD_CAP, cryptoData.btc.price, ergMarketCap);
const btcAbsorbDoge = calculateCrossTheoretical(BTC_HARD_CAP, cryptoData.btc.price, dogeMarketCap);
updateCard('btc', {
current: cryptoData.btc.price,
theoretical: btcTheoretical.theoreticalMax,
upside: btcTheoretical.upside,
timestamp: cryptoData.btc.timestamp,
crossAbsorb: {
erg: btcAbsorbErg,
doge: btcAbsorbDoge
}
});
// Ergo
const ergTheoretical = calculateTheoretical(cryptoData.erg.price, ERG_HARD_CAP);
const ergAbsorbBtc = calculateCrossTheoretical(ERG_HARD_CAP, cryptoData.erg.price, btcMarketCap);
const ergAbsorbDoge = calculateCrossTheoretical(ERG_HARD_CAP, cryptoData.erg.price, dogeMarketCap);
updateCard('erg', {
current: cryptoData.erg.price,
theoretical: ergTheoretical.theoreticalMax,
upside: ergTheoretical.upside,
timestamp: cryptoData.erg.timestamp,
crossAbsorb: {
btc: ergAbsorbBtc,
doge: ergAbsorbDoge
}
});
// Dogecoin (uses circulating supply since no hard cap)
const dogeTheoretical = calculateTheoretical(cryptoData.doge.price, cryptoData.doge.supply);
const dogeAbsorbBtc = calculateCrossTheoretical(cryptoData.doge.supply, cryptoData.doge.price, btcMarketCap);
const dogeAbsorbErg = calculateCrossTheoretical(cryptoData.doge.supply, cryptoData.doge.price, ergMarketCap);
updateCard('doge', {
current: cryptoData.doge.price,
theoretical: dogeTheoretical.theoreticalMax,
upside: dogeTheoretical.upside,
timestamp: cryptoData.doge.timestamp,
supply: cryptoData.doge.supply,
crossAbsorb: {
btc: dogeAbsorbBtc,
erg: dogeAbsorbErg
}
});
}
function updateCard(asset, data) {
// Update current price
const currentElement = document.getElementById(`${asset}-current`);
if (currentElement) {
currentElement.textContent = formatPrice(data.current);
}
// Update theoretical max
const theoreticalElement = document.getElementById(`${asset}-theoretical`);
if (theoreticalElement) {
theoreticalElement.textContent = formatPrice(data.theoretical);
}
// Update upside
const upsideElement = document.getElementById(`${asset}-upside`);
if (upsideElement) {
upsideElement.textContent = formatMultiplier(data.upside);
}
// Update timestamp
const timestampElement = document.getElementById(`${asset}-timestamp`);
if (timestampElement) {
timestampElement.textContent = data.timestamp;
}
// Update supply for DOGE
if (asset === 'doge' && data.supply) {
const supplyElement = document.getElementById(`${asset}-supply`);
if (supplyElement) {
supplyElement.textContent = formatSupply(data.supply);
}
}
// Update cross-crypto absorb values
if (data.crossAbsorb) {
for (let targetAsset in data.crossAbsorb) {
const absorbElement = document.getElementById(`${asset}-absorb-${targetAsset}`);
if (absorbElement) {
const crossData = data.crossAbsorb[targetAsset];
absorbElement.textContent = formatPrice(crossData.theoreticalPrice) + ' (' + formatMultiplier(crossData.upside) + ')';
}
}
}
}
// UI helpers
function showLoading(show) {
const loadingElement = document.getElementById('loading');
if (loadingElement) {
if (show) {
loadingElement.classList.add('active');
} else {
loadingElement.classList.remove('active');
}
}
}
function showError() {
const errorElement = document.getElementById('error');
if (errorElement) {
errorElement.classList.add('active');
// Hide after 5 seconds
setTimeout(() => {
hideError();
}, 5000);
}
}
function hideError() {
const errorElement = document.getElementById('error');
if (errorElement) {
errorElement.classList.remove('active');
}
}
// Initialize app
function init() {
console.log('Initializing Theoretical Value Calculator...');
console.log('USD Total Debt: $' + (USD_TOTAL_DEBT / 1e12) + 'T');
// Initial fetch
fetchData();
// Set up auto-refresh every 60 seconds
setInterval(fetchData, 60000);
console.log('App initialized. Data will refresh every 60 seconds.');
}
// Start the app when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}