Hướng dẫn
Quét token và chờ kết quả
Bắt đầu quét, kiểm tra định kỳ với tần suất hợp lý và nhận báo cáo hoàn chỉnh.
Luồng phổ biến nhất: bạn có một địa chỉ hợp đồng từ đâu đó và cần biết Holder Score của nó trước khi hành động. Hãy bắt đầu quét, rồi kiểm tra báo cáo định kỳ với độ trễ tăng dần cho đến khi hoàn tất.
POST /scanstrả về ngay lập tức; quá trình quét chạy ở chế độ nền.- Tăng dần thời gian chờ giữa các lần kiểm tra và tuân thủ
retryAfterkhi gặp 429. - Dừng lại khi gặp
DONEhoặcERROR, hoặc khi hết thời gian chờ do bạn tự đặt.
Mã#
const BASE = "https://app.xca.fun/api/v1";
const headers = {
Authorization: `Bearer ${process.env.XCA_API_KEY}`,
"Content-Type": "application/json",
};
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function scanAndWait(address, { timeoutMs = 180_000 } = {}) {
const start = await fetch(`${BASE}/scans`, {
method: "POST",
headers,
body: JSON.stringify({ chain: "solana", address }),
});
if (!start.ok) throw new Error((await start.json()).error.message);
const deadline = Date.now() + timeoutMs;
let delay = 2000;
while (Date.now() < deadline) {
const res = await fetch(`${BASE}/tokens/solana/${address}`, { headers });
if (res.status === 429) {
const { error } = await res.json();
await sleep((error.retryAfter ?? 5) * 1000);
continue;
}
const report = await res.json();
if (report.status === "DONE") return report;
if (report.status === "ERROR") throw new Error(report.errorMessage ?? "Scan failed");
console.log(`${report.step ?? "queued"} ${report.progress}%`);
await sleep(delay);
delay = Math.min(delay * 1.5, 8000);
}
throw new Error("Timed out waiting for the scan");
}
const report = await scanAndWait("DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263");
console.log(report.symbol, report.holderScore, report.riskLevel);