@tapepack/core 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/prices.mjs +8 -6
package/package.json
CHANGED
package/src/prices.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// 原生币实时美元价(BNB / OKB / ETH),浏览器 / Node 共用。服务费的封顶 $2 与免费门槛 $0.10 按它折算。
|
|
2
|
-
//
|
|
2
|
+
// 来源优先级 CoinGecko → OKX → Binance(都允许浏览器跨域),前一个缺的币由后一个补;全部取不到时返回 null——
|
|
3
3
|
// 调用方据此不收服务费(chains.mjs 的快照价可能偏差数倍)。CoinGecko 免费接口限流严、国内常连不上,所以要有后备。
|
|
4
4
|
|
|
5
5
|
export const PRICE_IDS = { bnb: 'binancecoin', xlayer: 'okb', base: 'ethereum' };
|
|
@@ -23,16 +23,18 @@ const SOURCES = [
|
|
|
23
23
|
},
|
|
24
24
|
];
|
|
25
25
|
|
|
26
|
-
/**
|
|
27
|
-
|
|
26
|
+
/**
|
|
27
|
+
* 各来源并行请求、按优先级合并(经代理时单个接口常要 6–7 秒,依次请求容易全部超时)
|
|
28
|
+
* @returns {Promise<Record<string, number> | null>} 链 key → 美元价;全部取不到时 null
|
|
29
|
+
*/
|
|
30
|
+
export async function fetchLivePrices({ fetchImpl = (...a) => fetch(...a), timeoutMs = 15000 } = {}) {
|
|
28
31
|
const get = (url) => fetchImpl(url, { signal: AbortSignal.timeout ? AbortSignal.timeout(timeoutMs) : undefined }).then((r) => {
|
|
29
32
|
if (!r.ok) throw new Error('http ' + r.status);
|
|
30
33
|
return r.json();
|
|
31
34
|
});
|
|
35
|
+
const results = await Promise.all(SOURCES.map((src) => src(get).catch(() => ({}))));
|
|
32
36
|
const m = {};
|
|
33
|
-
for (const
|
|
34
|
-
if (Object.keys(SYMBOLS).every((k) => m[k])) break;
|
|
35
|
-
const got = await src(get).catch(() => ({}));
|
|
37
|
+
for (const got of results) {
|
|
36
38
|
for (const [k, v] of Object.entries(got)) { const n = Number(v); if (!m[k] && n > 0) m[k] = n; }
|
|
37
39
|
}
|
|
38
40
|
return Object.keys(m).length ? m : null;
|