Intelligence, Advanced Charting & Trading Platform
Phase 1: add auditable Data Proof evidence
* feat: harden Phase 1 persistence invariants * test: add executable Phase 1 persistence invariants * test: define auditable live data proof report contract * feat: add auditable data proof report builder * Phase 1: aggregate required Data Proof evidence * test: define Phase 1 aggregate Data Proof artifact * feat: aggregate required Phase 1 Data Proof evidence
davoodmehraban89 committed
Oct 7, 2026 at 00:19 UTC
480db0b0561248582edbc9e24e718be85eaf2ae8
4 files changed
+151
packages/market-domain/src/data-proof-artifact.mjs
new
+33
@@ -0,0 +1,33 @@
1
+const REQUIRED = Object.freeze(['GOLD18_IRT', 'USD_IRT_FREE', 'XAU_USD']);
2
+
3
+export function buildDataProofArtifact({ generatedAt, reports }) {
4
+ if (!(generatedAt instanceof Date) || Number.isNaN(generatedAt.valueOf())) throw new TypeError('generatedAt must be a valid Date');
5
+ if (!Array.isArray(reports)) throw new TypeError('reports must be an array');
6
+
7
+ const seen = new Set();
8
+ for (const report of reports) {
9
+ if (!report || typeof report !== 'object') throw new TypeError('each report must be an object');
10
+ if (!report.providerCode || !report.canonicalSymbol) throw new TypeError('report providerCode and canonicalSymbol are required');
11
+ if (!['PASS', 'FAIL'].includes(report.status)) throw new RangeError('report status must be PASS or FAIL');
12
+ if (seen.has(report.canonicalSymbol)) throw new RangeError(`duplicate report for ${report.canonicalSymbol}`);
13
+ seen.add(report.canonicalSymbol);
14
+ const checked = new Date(report.checkedAt);
15
+ if (Number.isNaN(checked.valueOf())) throw new TypeError('report checkedAt must be valid');
16
+ if (checked > generatedAt) throw new RangeError('report checkedAt cannot be after generatedAt');
17
+ }
18
+
19
+ const missing = REQUIRED.filter(symbol => !seen.has(symbol));
20
+ if (missing.length) throw new RangeError(`required Phase 1 reports missing: ${missing.join(', ')}`);
21
+
22
+ const requiredReports = reports
23
+ .filter(report => REQUIRED.includes(report.canonicalSymbol))
24
+ .sort((a, b) => a.canonicalSymbol.localeCompare(b.canonicalSymbol));
25
+
26
+ return Object.freeze({
27
+ phase: 1,
28
+ generatedAt: generatedAt.toISOString(),
29
+ status: requiredReports.every(report => report.status === 'PASS') ? 'PASS' : 'FAIL',
30
+ requiredSymbols: [...REQUIRED],
31
+ reports: requiredReports.map(report => Object.freeze({ ...report })),
32
+ });
33
+}
packages/market-domain/src/data-proof-report.mjs
new
+34
@@ -0,0 +1,34 @@
1
+import { analyzeObservationSeries } from './data-proof.mjs';
2
+
3
+function nonNegative(value, name) {
4
+ if (!Number.isFinite(value) || value < 0) throw new RangeError(`${name} must be non-negative and finite`);
5
+}
6
+
7
+export function buildDataProofReport({ providerCode, canonicalSymbol, observations, checkedAt, thresholds }) {
8
+ if (!providerCode || !canonicalSymbol) throw new TypeError('providerCode and canonicalSymbol are required');
9
+ if (!Array.isArray(observations) || observations.length === 0) throw new RangeError('at least one observation is required');
10
+ if (!(checkedAt instanceof Date) || Number.isNaN(checkedAt.valueOf())) throw new TypeError('checkedAt must be a valid Date');
11
+ if (!thresholds || typeof thresholds !== 'object') throw new TypeError('thresholds are required');
12
+ for (const key of ['maxGapMs','maxFreshnessMs','maxDuplicateCount','maxOutOfOrderCount']) nonNegative(thresholds[key], key);
13
+
14
+ const series = analyzeObservationSeries(observations);
15
+ const latestMs = new Date(series.lastTimestamp).valueOf();
16
+ const freshnessMs = checkedAt.valueOf() - latestMs;
17
+ if (freshnessMs < 0) throw new RangeError('checkedAt cannot be before the latest observation');
18
+
19
+ const failures = [];
20
+ if (series.maxGapMs !== null && series.maxGapMs > thresholds.maxGapMs) failures.push('MAX_GAP_EXCEEDED');
21
+ if (freshnessMs > thresholds.maxFreshnessMs) failures.push('STALE_LATEST_OBSERVATION');
22
+ if (series.duplicateTimestampCount > thresholds.maxDuplicateCount) failures.push('DUPLICATE_TIMESTAMPS');
23
+ if (series.outOfOrderCount > thresholds.maxOutOfOrderCount) failures.push('OUT_OF_ORDER_OBSERVATIONS');
24
+
25
+ return {
26
+ providerCode,
27
+ canonicalSymbol,
28
+ checkedAt: checkedAt.toISOString(),
29
+ status: failures.length === 0 ? 'PASS' : 'FAIL',
30
+ failures,
31
+ thresholds: { ...thresholds },
32
+ metrics: { ...series, freshnessMs },
33
+ };
34
+}
packages/market-domain/test/data-proof-artifact.test.mjs
new
+38
@@ -0,0 +1,38 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import { buildDataProofArtifact } from '../src/data-proof-artifact.mjs';
4
+
5
+test('builds immutable evidence artifact from three required Phase 1 reports', () => {
6
+ const artifact = buildDataProofArtifact({
7
+ generatedAt: new Date('2026-10-05T12:10:00Z'),
8
+ reports: [
9
+ { providerCode: 'navasan', canonicalSymbol: 'USD_IRT_FREE', status: 'PASS', checkedAt: '2026-10-05T12:09:00Z' },
10
+ { providerCode: 'navasan', canonicalSymbol: 'GOLD18_IRT', status: 'PASS', checkedAt: '2026-10-05T12:09:00Z' },
11
+ { providerCode: 'alpha_vantage', canonicalSymbol: 'XAU_USD', status: 'PASS', checkedAt: '2026-10-05T12:09:00Z' },
12
+ ],
13
+ });
14
+ assert.equal(artifact.phase, 1);
15
+ assert.equal(artifact.status, 'PASS');
16
+ assert.deepEqual(artifact.requiredSymbols, ['GOLD18_IRT','USD_IRT_FREE','XAU_USD']);
17
+});
18
+
19
+test('fails aggregate proof when any required report fails', () => {
20
+ const artifact = buildDataProofArtifact({
21
+ generatedAt: new Date('2026-10-05T12:10:00Z'),
22
+ reports: [
23
+ { providerCode: 'navasan', canonicalSymbol: 'USD_IRT_FREE', status: 'PASS', checkedAt: '2026-10-05T12:09:00Z' },
24
+ { providerCode: 'navasan', canonicalSymbol: 'GOLD18_IRT', status: 'FAIL', checkedAt: '2026-10-05T12:09:00Z' },
25
+ { providerCode: 'alpha_vantage', canonicalSymbol: 'XAU_USD', status: 'PASS', checkedAt: '2026-10-05T12:09:00Z' },
26
+ ],
27
+ });
28
+ assert.equal(artifact.status, 'FAIL');
29
+});
30
+
31
+test('refuses to certify Phase 1 when a required symbol is missing or duplicated', () => {
32
+ const base = [
33
+ { providerCode: 'navasan', canonicalSymbol: 'USD_IRT_FREE', status: 'PASS', checkedAt: '2026-10-05T12:09:00Z' },
34
+ { providerCode: 'navasan', canonicalSymbol: 'GOLD18_IRT', status: 'PASS', checkedAt: '2026-10-05T12:09:00Z' },
35
+ ];
36
+ assert.throws(() => buildDataProofArtifact({ generatedAt: new Date('2026-10-05T12:10:00Z'), reports: base }), /required/i);
37
+ assert.throws(() => buildDataProofArtifact({ generatedAt: new Date('2026-10-05T12:10:00Z'), reports: [...base, base[0]] }), /duplicate/i);
38
+});
packages/market-domain/test/data-proof-report.test.mjs
new
+46
@@ -0,0 +1,46 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import { buildDataProofReport } from '../src/data-proof-report.mjs';
4
+
5
+test('passes a clean provider window with explicit thresholds', () => {
6
+ const report = buildDataProofReport({
7
+ providerCode: 'navasan',
8
+ canonicalSymbol: 'USD_IRT_FREE',
9
+ observations: [
10
+ { timestamp: '2026-10-05T12:00:00Z', price: 100000 },
11
+ { timestamp: '2026-10-05T12:01:00Z', price: 100100 },
12
+ { timestamp: '2026-10-05T12:02:00Z', price: 100050 },
13
+ ],
14
+ checkedAt: new Date('2026-10-05T12:02:10Z'),
15
+ thresholds: { maxGapMs: 90_000, maxFreshnessMs: 60_000, maxDuplicateCount: 0, maxOutOfOrderCount: 0 },
16
+ });
17
+ assert.equal(report.status, 'PASS');
18
+ assert.deepEqual(report.failures, []);
19
+ assert.equal(report.metrics.freshnessMs, 10_000);
20
+});
21
+
22
+test('fails stale, gapped and duplicate data with auditable reasons', () => {
23
+ const report = buildDataProofReport({
24
+ providerCode: 'navasan',
25
+ canonicalSymbol: 'GOLD18_IRT',
26
+ observations: [
27
+ { timestamp: '2026-10-05T12:00:00Z', price: 100 },
28
+ { timestamp: '2026-10-05T12:00:00Z', price: 100 },
29
+ { timestamp: '2026-10-05T12:05:00Z', price: 101 },
30
+ ],
31
+ checkedAt: new Date('2026-10-05T12:10:00Z'),
32
+ thresholds: { maxGapMs: 120_000, maxFreshnessMs: 60_000, maxDuplicateCount: 0, maxOutOfOrderCount: 0 },
33
+ });
34
+ assert.equal(report.status, 'FAIL');
35
+ assert.ok(report.failures.includes('MAX_GAP_EXCEEDED'));
36
+ assert.ok(report.failures.includes('STALE_LATEST_OBSERVATION'));
37
+ assert.ok(report.failures.includes('DUPLICATE_TIMESTAMPS'));
38
+});
39
+
40
+test('refuses to certify an empty observation window', () => {
41
+ assert.throws(() => buildDataProofReport({
42
+ providerCode: 'alpha_vantage', canonicalSymbol: 'XAU_USD', observations: [],
43
+ checkedAt: new Date('2026-10-05T12:00:00Z'),
44
+ thresholds: { maxGapMs: 60_000, maxFreshnessMs: 60_000, maxDuplicateCount: 0, maxOutOfOrderCount: 0 },
45
+ }), /observation/i);
46
+});