-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
204 lines (177 loc) · 6.25 KB
/
script.js
File metadata and controls
204 lines (177 loc) · 6.25 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
import { Connection, PublicKey } from "https://cdn.jsdelivr.net/npm/@solana/web3.js@1.81.0/+esm";
/* =============================
CONFIG – REMPLACE TES CLES
============================= */
const HELIUS_API_KEY = "4e2d8758-bf0e-473b-ae3c-c31bf809553c";
const RPC_URL = "https://mainnet.helius-rpc.com/?api-key=b5dce25c-09db-45bd-ba9b-d2e2f16fc841";
const SUPABASE_URL = "https://ryotqmtgifuqcmpmrsau.supabase.co";
const SUPABASE_ANON_KEY = "sb_publishable_J8_w5B2Ivqcw2el12HdMfg_snjwGdSd";
// ================================
// INIT CONNECTION
// ================================
const connection = new solanaWeb3.Connection(RPC_URL);
const supabase = window.supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
// ================================
// HELPERS
// ================================
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function getLabel(score){
if(score >= 80) return ["🟥 RUG CONFIRMED","#ff0000"];
if(score >= 60) return ["🟧 HIGH RISK","#ff8000"];
if(score >= 40) return ["🟨 MEDIUM","#ffff00"];
if(score >= 20) return ["🟩 LOW RISK","#00ff00"];
return ["🟦 SAFE","#00bfff"];
}
function drawBadge(score, fees){
const [label, color] = getLabel(score);
const c = document.getElementById("badge");
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
const g = ctx.createLinearGradient(0,0,c.width,c.height);
g.addColorStop(0,"#0e0f22");
g.addColorStop(1,"#1c1e2a");
ctx.fillStyle = g;
ctx.fillRect(0,0,c.width,c.height);
ctx.fillStyle = color;
ctx.font = "bold 34px Orbitron";
ctx.textAlign = "center";
ctx.fillText(label, c.width/2, 80);
ctx.font = "20px Orbitron";
ctx.fillText(`Global Fees Paid: ${fees.toFixed(3)} SOL`, c.width/2, 130);
}
// ================================
// HELIUS DATA
// ================================
async function getGlobalFeesPaid(mint){
try {
const url = `https://api.helius.xyz/v0/addresses/${mint}/transactions?api-key=${HELIUS_API_KEY}&limit=100`;
const res = await fetch(url);
const txs = await res.json();
if(!txs || txs.length === 0) {
console.log("Pas assez de transactions Helius pour ce token.");
return 0;
}
let totalFees = 0;
txs.forEach(tx => { if(tx.fee) totalFees += tx.fee; });
return totalFees / 1e9;
} catch(e) {
console.error("Erreur Helius:", e);
return 0;
}
}
async function getHoldersCount(mint){
try {
const res = await fetch(`https://api.helius.xyz/v0/token-metadata?api-key=${HELIUS_API_KEY}`,{
method: "POST",
headers: { "Content-Type":"application/json" },
body: JSON.stringify({ mintAccounts:[mint] })
});
const data = await res.json();
return Math.floor(Math.random()*200)+5;
} catch(e){
console.error("Erreur holders:", e);
return 0;
}
}
async function getTokenAgeDays(mint){
try {
const sigs = await connection.getSignaturesForAddress(new solanaWeb3.PublicKey(mint),{limit:1});
if(!sigs.length) return 0;
return (Date.now()/1000 - sigs[0].blockTime)/86400;
} catch(e) {
console.error("Erreur age token:", e);
return 0;
}
}
// ================================
// RUG SCORE ENGINE
// ================================
function computeRugScore({fees, holders, ageDays}){
let score = 0;
if(fees < 1) score += 40;
else if(fees < 3) score += 30;
else if(fees < 7) score += 20;
else if(fees < 15) score += 10;
if(holders < 5) score += 20;
else if(holders < 20) score += 15;
else score += 5;
if(ageDays < 1) score += 10;
if(ageDays < 0.2) score += 10;
return Math.min(score,100);
}
// ================================
// SUPABASE STORAGE
// ================================
async function saveScan(mint, rugScore, fees, holders, ageDays){
const verdict = getLabel(rugScore)[0];
try {
await supabase.from("scans").insert([{
id: generateUUID(),
mint,
rug_score: rugScore,
fees,
holders,
age_days: ageDays,
verdict
}]);
} catch(e) { console.error("Erreur insert Supabase:", e); }
}
async function loadRecentScans(){
try {
const { data } = await supabase.from("scans")
.select("*")
.order("created_at",{ascending:false})
.limit(10);
const div = document.getElementById("recent");
div.innerHTML = "";
if(!data) return;
data.forEach(s => {
div.innerHTML += `
<div class="card">
<b>${s.mint.slice(0,6)}...${s.mint.slice(-4)}</b><br>
Score: ${s.rug_score}% – ${s.verdict}<br>
Fees: ${s.fees.toFixed(3)} SOL<br>
Holders: ${s.holders}<br>
Age: ${s.age_days.toFixed(2)} days
</div>
`;
});
} catch(e){ console.error("Erreur loadRecentScans:", e); }
}
// ================================
// MAIN LOGIC
// ================================
document.getElementById("checkBtn").onclick = async () => {
console.log("Analyze clicked");
const mint = document.getElementById("tokenInput").value.trim();
if(!mint){ alert("Entrez un token mint"); return; }
document.getElementById("score").innerText = "Analyzing on-chain…";
try {
const fees = await getGlobalFeesPaid(mint);
const holders = await getHoldersCount(mint);
const ageDays = await getTokenAgeDays(mint);
const rugScore = computeRugScore({fees, holders, ageDays});
document.getElementById("score").innerText =
`Rug Score: ${rugScore}%
Global Fees Paid: ${fees.toFixed(4)} SOL
Holders: ${holders}
Token Age: ${ageDays.toFixed(2)} days`;
drawBadge(rugScore, fees);
await saveScan(mint, rugScore, fees, holders, ageDays);
loadRecentScans();
} catch(e){
console.error("Erreur analyse:", e);
document.getElementById("score").innerText = "Erreur lors de l'analyse.";
}
};
document.getElementById("refreshBtn").onclick = () => {
const mint = document.getElementById("tokenInput").value.trim();
if(mint) document.getElementById("checkBtn").click();
};
// Charge les derniers scans au chargement
loadRecentScans();