Intelligence, Advanced Charting & Trading Platform
feat(feed): CoinGecko points and Binance/yfinance OHLC provider adapters with explicit rights
davoodmehraban89 committed
Oct 12, 2026 at 00:16 UTC
fcbe26eacb2fdf9e31492a7979ecb5accc0b8599
1 file changed
+88
packages/market-feed/src/adapters.mjs
new
+88
@@ -0,0 +1,88 @@
1
+import {canonicalReferencePoint,canonicalSourceCandle,DATA_KIND} from "./contracts.mjs";
2
+const requireApproved=(rights)=>{if(rights!=="approved_public"&&rights!=="research_only")throw Error("Provider rights unverified");};
3
+const positiveDate=(x,label)=>{if(!Number.isSafeInteger(x)||x<=0)throw Error(label+" invalid epoch");return new Date(x).toISOString()};
4
+const safeMapping=(symbol,unit,source,rights,licenseEvidence,receivedAt)=> {
5
+ if(!symbol||!unit||!source||!receivedAt||!licenseEvidence)throw Error("Explicit provider mapping/provenance required");
6
+ requireApproved(rights);return {symbol,unit,source,rights,licenseEvidence,receivedAt};
7
+};
8
+/**
9
+ * CoinGecko market_chart is a set of market-price points, NOT OHLC.
10
+ * The API's prices[] and total_volumes[] are independent timeseries. Never
11
+ * invent opening/closing prices, trade volume, or exchange-specific bars.
12
+ */
13
+export function parseCoinGeckoMarketChart(payload,{symbol,unit,receivedAt,rights="research_only",licenseEvidence,coinId}){
14
+ const base=safeMapping(symbol,unit,"coingecko",rights,licenseEvidence,receivedAt);
15
+ if(!/^[a-z0-9-]{2,80}$/.test(coinId||""))throw Error("Invalid CoinGecko ID");
16
+ if(!Array.isArray(payload?.prices))throw Error("CoinGecko prices[] missing");
17
+ const seen=new Set();
18
+ return payload.prices.map((p,i)=>{
19
+ if(!Array.isArray(p)||p.length!==2)throw Error("Malformed CoinGecko point "+i);
20
+ const when=positiveDate(p[0],"CoinGecko point");
21
+ if(seen.has(when))throw Error("Duplicate CoinGecko timestamp");
22
+ seen.add(when);
23
+ return canonicalReferencePoint({...base,dataKind:DATA_KIND.REFERENCE,price:p[1],sourceTimestamp:when,
24
+ venue:"coingecko_aggregated",quoteSide:"indicative"});
25
+ }).sort((a,b)=>a.sourceTimestamp.localeCompare(b.sourceTimestamp));
26
+}
27
+/** Request planning only. Actual Demo API entitlement/rate restrictions are checked by the caller. */
28
+export function planCoinGeckoHourlyRanges({startMs,endMs,maxDaysPerRequest=100}){
29
+ if(!Number.isSafeInteger(startMs)||!Number.isSafeInteger(endMs)||startMs>=endMs)throw Error("Invalid time range");
30
+ if(!Number.isInteger(maxDaysPerRequest)||maxDaysPerRequest<1||maxDaysPerRequest>100)throw Error("Hourly request must not exceed 100 days");
31
+ const range=[];let start=startMs;const span=maxDaysPerRequest*86400000;
32
+ while(start<endMs){
33
+ const end=Math.min(start+span,endMs);
34
+ range.push({from:Math.floor(start/1000),to:Math.floor(end/1000)});
35
+ start=end;
36
+ if(range.length>64)throw Error("Too many request windows");
37
+ }
38
+ return range;
39
+}
40
+/**
41
+ * Binance SPOT klines are genuine venue OHLC. closeTime is inclusive in
42
+ * the wire format. Preserve its +1ms exclusive interval without fabricating.
43
+ * This adapter performs no requests and cannot bypass access restrictions.
44
+ */
45
+export function parseBinanceSpotKlines(klines,{symbol,unit,receivedAt,rights="research_only",licenseEvidence,interval,venue="binance_spot"}){
46
+ const base=safeMapping(symbol,unit,"binance",rights,licenseEvidence,receivedAt);
47
+ const durations=Object.freeze({"1h":3600000,"1d":86400000,"1m":60000,"5m":300000,"15m":900000,"4h":14400000});
48
+ if(!Object.hasOwn(durations,interval))throw Error("Unsupported Binance kline interval");
49
+ if(!Array.isArray(klines))throw Error("Binance klines array missing");
50
+ const seen=new Set();
51
+ return klines.map((k,i)=>{
52
+ if(!Array.isArray(k)||k.length<7)throw Error("Malformed Binance kline "+i);
53
+ const [openEpoch,o,h,l,c,v,closeEpoch]=k;
54
+ const begin=positiveDate(openEpoch,"openTime"),end=positiveDate(closeEpoch+1,"closeTime");
55
+ if(closeEpoch+1-openEpoch!==durations[interval])throw Error("Binance interval not aligned");
56
+ if(seen.has(begin))throw Error("Duplicate Binance open timestamp");
57
+ seen.add(begin);
58
+ return canonicalSourceCandle({...base,dataKind:DATA_KIND.CANDLE,openTime:begin,closeTime:end,sourceTimestamp:end,
59
+ timeframe:interval,open:o,high:h,low:l,close:c,volume:v,timezone:"UTC",venue,quoteSide:"trade",quality:"DEGRADED"});
60
+ }).sort((a,b)=>a.openTime.localeCompare(b.openTime));
61
+}
62
+/**
63
+ * yfinance is an explicitly RESEARCH-ONLY adapter scaffold. Commercial
64
+ * publishing remains blocked even if its API returns correctly shaped OHLC.
65
+ * It intentionally does not fetch Yahoo in deployed ChartNova.
66
+ */
67
+export function parseYfinanceResearchBars(bars,{symbol,unit,receivedAt,licenseEvidence,timeframe="1d",timezone="America/New_York"}){
68
+ const base=safeMapping(symbol,unit,"yfinance","research_only",licenseEvidence,receivedAt);
69
+ if(!Array.isArray(bars))throw Error("yfinance research data missing");
70
+ const seen=new Set();
71
+ return bars.map((r,i)=>{
72
+ if(!r||typeof r!=="object")throw Error("Invalid research bar "+i);
73
+ if(seen.has(r.openTime))throw Error("Duplicate research timestamp");seen.add(r.openTime);
74
+ return canonicalSourceCandle({...base,dataKind:DATA_KIND.CANDLE,timeframe,
75
+ openTime:r.openTime,closeTime:r.closeTime,sourceTimestamp:r.closeTime,
76
+ open:r.open,high:r.high,low:r.low,close:r.close,volume:r.volume??null,
77
+ timezone,venue:"yahoo_research",quality:"DEGRADED",quoteSide:"trade"});
78
+ }).sort((a,b)=>a.openTime.localeCompare(b.openTime));
79
+}
80
+export const PROVIDER_REGISTRY=Object.freeze({
81
+ coingecko:Object.freeze({transport:"REST",dataKind:"reference_point",history:"market_chart/range",launch:"license_review_required"}),
82
+ binance:Object.freeze({transport:"REST+WebSocket",dataKind:"source_ohlc",history:"api/v3/klines",launch:"geofencing_and_license_review"}),
83
+ yfinance:Object.freeze({transport:"Python research bridge",dataKind:"source_ohlc",launch:"research_only"}),
84
+ polygon:Object.freeze({transport:"WebSocket",dataKind:"provider_dependent",launch:"plan_and_entitlement_required"}),
85
+ navasan:Object.freeze({transport:"REST",dataKind:"provider_dependent",launch:"license_and_unit_audit_required"}),
86
+ bonbast:Object.freeze({transport:"not_verified",dataKind:"not_verified",launch:"blocked_pending_contract"}),
87
+ ecb:Object.freeze({transport:"REST",dataKind:"reference_point",launch:"attribution_required"}),
88
+});