指南
扫描代币并等待结果
发起扫描,有节制地轮询,拿到完成的报告。
最常见的流程:你从某处拿到一个合约地址,在采取行动之前需要知道它的 Holder Score。先发起扫描,然后以逐渐增加的间隔轮询报告,直到扫描完成。
POST /scans会立即返回;扫描在后台运行。- 两次轮询之间逐步拉长间隔,收到 429 时遵守
retryAfter。 - 在
DONE或ERROR时停止,或在达到你自己设定的超时后停止。
代码#
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);