-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_search.js
179 lines (159 loc) · 7.68 KB
/
token_search.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
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
const searchInput = document.getElementById("tokenSearch");
const resultSection = document.getElementById("tokenResults");
const tokenList = document.getElementById("tokenList");
// Récupérer la liste des tokens au chargement de la page
async function fetchTokenList() {
try {
let response = await fetch("https://api.coingecko.com/api/v3/coins/list");
if (!response.ok) throw new Error("Erreur lors de la récupération de la liste des tokens");
let data = await response.json();
console.log("Liste des tokens chargée :", data); // Vérification
populateTokenList(data);
} catch (error) {
console.error("Erreur :", error);
}
}
// Remplir la liste des tokens dans le datalist
function populateTokenList(tokens) {
tokens.forEach(token => {
const option = document.createElement("option");
option.value = token.name; // Affiche le nom complet du token
option.setAttribute("data-id", token.id); // Stocke l'ID du token
tokenList.appendChild(option);
});
}
// Fonction pour calculer le pourcentage par rapport à l'ATH
function calculatePercentageFromATH(currentPrice, ath) {
if (ath === 0) return 0; // Pour éviter une division par zéro
return ((currentPrice / ath) * 100).toFixed(2);
}
// Fonction pour calculer les niveaux de Fibonacci
function calculateFibonacciLevels(ath, atl) {
const levels = [0.236, 0.382, 0.5, 0.618, 0.786];
return levels.map(level => ({
level: level * 100,
price: ath - (ath - atl) * level
}));
}
// Fonction pour afficher les niveaux de support/resistance
function displaySupportResistanceLevels(currentPrice, fibonacciLevels) {
let message = "";
fibonacciLevels.forEach(level => {
if (Math.abs(currentPrice - level.price) < 2) { // 2 est une marge d'erreur
message += `Support/resistance à <strong style="color:#60d394;">${level.price.toFixed(2)}</strong> (${level.level}%)<br>`;
}
});
return message || "Aucun niveau de support/resistance proche.";
}
// Rechercher les données du token
async function fetchTokenData(query) {
console.log("Recherche du token :", query); // Vérification
if (!query.trim()) {
resultSection.innerHTML = `<p style='color:#ee6055;'>⚠ Veuillez entrer un nom de token</p>`;
return;
}
resultSection.innerHTML = `<p>Chargement...</p>`;
try {
let response = await fetch(`https://api.coingecko.com/api/v3/coins/${query}`);
console.log("Réponse API :", response); // Vérification
if (!response.ok) throw new Error("Token non trouvé");
let data = await response.json();
console.log("Données du token :", data); // Vérification
displayTokenData(data);
} catch (error) {
console.error("Erreur :", error); // Vérification
resultSection.innerHTML = `<p style='color:#ee6055;'>⚠ Token introuvable ou probléme de connexion possible. Please Refresh 🔄️</p>`;
}
}
// Afficher les données du token
function displayTokenData(data) {
const { name, symbol, image, market_data, market_cap_rank, platforms, categories } = data;
const price = market_data.current_price.usd.toFixed(2);
const ath = market_data.ath.usd.toFixed(2);
const atl = market_data.atl.usd.toFixed(2);
const blockchains = platforms ? Object.keys(platforms).join(", ") : "Non listé";
const utility = categories ? categories.join(", ") : "Non spécifié";
// Calcul du pourcentage par rapport à l'ATH
const percentFromATH = calculatePercentageFromATH(market_data.current_price.usd, market_data.ath.usd);
// Calcul des niveaux de Fibonacci
const fibonacciLevels = calculateFibonacciLevels(market_data.ath.usd, market_data.atl.usd);
const supportResistanceMessage = displaySupportResistanceLevels(market_data.current_price.usd, fibonacciLevels);
resultSection.innerHTML = `
<p style="display: flex; justify-content: space-between; align-items: center;">
<strong><span>${name} (${symbol.toUpperCase()})</span></strong>
<img src="${image.small}" alt="${name} logo" style="width: 50px; height: 50px; margin-left: 10px;"></p>
<div>
<p>Price <strong style='color:grey;'>${price}</strong> $</p>
<p>Rank <strong style='color:grey;'>${market_cap_rank}</strong>⭐</p>
<p>ATH 📈<strong style='color:#60d394;'>${ath}</strong>$</p>
<p>ATL 📉<strong style='color:#ee6055;'>${atl}</strong>$</p><br>
<p style='color:#58a6ff;'><strong>${name}</strong> is at <strong>${percentFromATH}%</strong> of its ATH</p>
<br>
<p><strong style='color:#3c325b;'>Niveaux de Fibonacci :</strong><br>${supportResistanceMessage}</p><br>
<p><strong style='color:#3c325b;'>Blockchain</strong> 🔗 ${blockchains}</p><br>
<p><strong style='color:#3c325b;'>Utilité</strong> 🛠 ${utility}</p>
</div>
</div>
<!--<canvas id="price-chart"></canvas>-->
`;
fetchHistoricalData(data.id);
}
// Récupérer les données historiques pour le graphique
async function fetchHistoricalData(tokenId) {
console.log("Récupération des données historiques pour :", tokenId); // Vérification
try {
let response = await fetch(`https://api.coingecko.com/api/v3/coins/${tokenId}/market_chart?vs_currency=usd&days=30`);
console.log("Réponse API historique :", response); // Vérification
if (!response.ok) throw new Error("Erreur lors de la récupération des données historiques");
let data = await response.json();
console.log("Données historiques :", data); // Vérification
drawChart(data.prices);
} catch (error) {
console.error("Erreur :", error); // Vérification
}
}
// Dessiner le graphique
function drawChart(priceData) {
const ctx = document.getElementById("price-chart").getContext("2d");
const labels = priceData.map(entry => new Date(entry[0]).toLocaleDateString());
const prices = priceData.map(entry => entry[1]);
new Chart(ctx, {
data: {
labels: labels,
datasets: [{
label: "Prix USD",
data: prices,
borderColor: "#ab9ff2",
fill: false
}]
},
options: { responsive: true }
});
}
// Événements
document.getElementById("tokenSearch").addEventListener("keypress", (event) => {
console.log("Touche pressée :", event.key); // Vérification
if (event.key === "Enter") {
const selectedOption = Array.from(tokenList.options).find(option => option.value === event.target.value);
if (selectedOption) {
console.log("Token sélectionné :", selectedOption.getAttribute("data-id")); // Vérification
fetchTokenData(selectedOption.getAttribute("data-id"));
} else {
console.log("Recherche directe :", event.target.value.toLowerCase()); // Vérification
fetchTokenData(event.target.value.toLowerCase());
}
}
});
document.getElementById("searchButton").addEventListener("click", () => {
console.log("Bouton de recherche cliqué"); // Vérification
const selectedOption = Array.from(tokenList.options).find(option => option.value === searchInput.value);
if (selectedOption) {
console.log("Token sélectionné :", selectedOption.getAttribute("data-id")); // Vérification
fetchTokenData(selectedOption.getAttribute("data-id"));
} else {
console.log("Recherche directe :", searchInput.value.toLowerCase()); // Vérification
fetchTokenData(searchInput.value.toLowerCase());
}
});
// Charger la liste des tokens au démarrage
fetchTokenList();