Intelligence, Advanced Charting & Trading Platform
feat(risk): deterministic free-quota schedule planner with reserve and no latency promise
davoodmehraban89 committed
Oct 21, 2026 at 08:52 UTC
f483475b8b071a26fed9085fe877f46d53e73251
1 file changed
+36
packages/market-domain/src/free-quota-budget.mjs
new
+36
@@ -0,0 +1,36 @@
1
+/**
2
+ * Non-billable, quota-aware polling planner. Returns advisory scheduling
3
+ * constraints only: it never changes Worker Cron or calls a vendor API.
4
+ */
5
+export const VERIFIED_DATED_FREE_BUDGETS=Object.freeze({
6
+ NAVASAN:{calls:120,period:'MONTH',source:'https://www.navasan.tech/en/api',checked:'2026-10-15'},
7
+ GHEYMAT_ONLINE:{calls:250,period:'DAY',source:'https://gheymat.online/price-api',checked:'2026-10-15'}
8
+});
9
+export function quotaBudget({calls,windowMinutes,callsPerPoll=1,reserveFraction=0.25}){
10
+ for(const x of [calls,windowMinutes,callsPerPoll]){
11
+ if(!Number.isSafeInteger(x)||x<=0)throw new RangeError('positive integers required');
12
+ }
13
+ if(!Number.isFinite(reserveFraction)||reserveFraction<0||reserveFraction>=1)
14
+ throw new RangeError('reserveFraction must be 0..1 exclusive');
15
+ const usable=Math.floor(calls*(1-reserveFraction));
16
+ const allowed=Math.floor(usable/callsPerPoll);
17
+ if(allowed<1)throw new RangeError('No quota available after reserve');
18
+ const mathematicallyMinimalMinutes=windowMinutes*callsPerPoll/calls;
19
+ const reservedMinimumMinutes=windowMinutes/allowed;
20
+ const suggestedMinutes=Math.ceil(reservedMinimumMinutes);
21
+ const scheduledCycles=Math.ceil(windowMinutes/suggestedMinutes);
22
+ const spend=scheduledCycles*callsPerPoll;
23
+ if(spend>usable)throw new Error('Planner exceeded reserved budget');
24
+ return {quotaLimit:calls,availableCalls:usable,callsPerPoll,windowMinutes,
25
+ mathematicallyMinimalMinutes,reservedMinimumMinutes,suggestedMinutes,
26
+ plannedCalls:spend,remainingCalls:calls-spend,
27
+ guaranteedLatencyMs:null,latencyVerified:false,advisoryOnly:true};
28
+}
29
+export function officialFreePlanBudget(provider,{daysInMonth=30,callsPerPoll=1,reserveFraction=.25}={}){
30
+ const plan=VERIFIED_DATED_FREE_BUDGETS[provider];
31
+ if(!plan)throw new RangeError('unknown provider');
32
+ if(plan.period==='MONTH'&&![28,29,30,31].includes(daysInMonth))
33
+ throw new RangeError('daysInMonth');
34
+ return quotaBudget({calls:plan.calls,windowMinutes:
35
+ plan.period==='DAY'?1440:daysInMonth*1440,callsPerPoll,reserveFraction});
36
+}