Intelligence, Advanced Charting & Trading Platform
Phase 1: implement Navasan provider adapter
* test: define Navasan adapter contract * feat: implement Navasan latest-price adapter * test: define Navasan OHLC adapter contract * feat: add Navasan historical OHLC adapter * test: correct Navasan fixture chronology
davoodmehraban89 committed
Oct 5, 2026 at 16:26 UTC
eb9e131af05af9263346fca8479fdea6b0d97b19
3 files changed
+245
packages/market-domain/src/adapters/navasan.mjs
new
+130
@@ -0,0 +1,130 @@
1
+import { canonicalObservation } from '../provider-contract.mjs';
2
+
3
+export const NAVASAN_SYMBOLS = Object.freeze({
4
+ usd_sell: Object.freeze({ canonicalSymbol: 'USD_IRT_FREE', rawUnit: 'IRR_PER_USD' }),
5
+ '18ayar': Object.freeze({ canonicalSymbol: 'GOLD18_IRT', rawUnit: 'IRR_PER_GRAM' }),
6
+});
7
+
8
+function validDate(value, name) {
9
+ if (!(value instanceof Date) || Number.isNaN(value.valueOf())) {
10
+ throw new TypeError(`${name} must be a valid Date`);
11
+ }
12
+}
13
+
14
+function parsePositiveNumeric(value, name) {
15
+ const numeric = typeof value === 'string' && value.trim() !== '' ? Number(value) : value;
16
+ if (!Number.isFinite(numeric) || numeric <= 0) {
17
+ throw new RangeError(`${name} must be numeric, positive, and finite`);
18
+ }
19
+ return numeric;
20
+}
21
+
22
+function unixSecondsToDate(value, name) {
23
+ const numeric = Number(value);
24
+ if (!Number.isFinite(numeric) || numeric <= 0) {
25
+ throw new RangeError(`${name} must be a positive Unix timestamp`);
26
+ }
27
+ const date = new Date(numeric * 1000);
28
+ if (Number.isNaN(date.valueOf())) throw new RangeError(`${name} is invalid`);
29
+ return date;
30
+}
31
+
32
+function toCanonicalPrice(rawPrice, rawUnit) {
33
+ if (rawUnit === 'IRR_PER_USD' || rawUnit === 'IRR_PER_GRAM') return rawPrice / 10;
34
+ return rawPrice;
35
+}
36
+
37
+export function parseNavasanLatest({ payload, receivedAt }) {
38
+ validDate(receivedAt, 'receivedAt');
39
+ if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
40
+ throw new TypeError('payload must be an object');
41
+ }
42
+
43
+ const observations = [];
44
+
45
+ for (const [providerSymbol, mapping] of Object.entries(NAVASAN_SYMBOLS)) {
46
+ const item = payload[providerSymbol];
47
+ if (item == null) continue;
48
+ if (typeof item !== 'object' || Array.isArray(item)) {
49
+ throw new TypeError(`Navasan ${providerSymbol} observation must be an object`);
50
+ }
51
+
52
+ const rawPrice = parsePositiveNumeric(item.value, `${providerSymbol}.value`);
53
+ const providerTimestamp = unixSecondsToDate(item.timestamp, `${providerSymbol}.timestamp`);
54
+
55
+ const canonical = canonicalObservation({
56
+ providerCode: 'navasan',
57
+ canonicalSymbol: mapping.canonicalSymbol,
58
+ providerTimestamp,
59
+ receivedAt,
60
+ price: rawPrice,
61
+ rawUnit: mapping.rawUnit,
62
+ });
63
+
64
+ observations.push({
65
+ providerCode: canonical.providerCode,
66
+ providerSymbol,
67
+ canonicalSymbol: canonical.canonicalSymbol,
68
+ providerTimestamp: canonical.providerTimestamp,
69
+ receivedAt: canonical.receivedAt,
70
+ price: canonical.price,
71
+ canonicalUnit: canonical.canonicalUnit,
72
+ rawUnit: mapping.rawUnit,
73
+ raw: {
74
+ value: item.value,
75
+ change: item.change ?? null,
76
+ timestamp: item.timestamp,
77
+ date: item.date ?? null,
78
+ },
79
+ });
80
+ }
81
+
82
+ return observations;
83
+}
84
+
85
+export function parseNavasanOhlc({ providerSymbol, payload }) {
86
+ const mapping = NAVASAN_SYMBOLS[providerSymbol];
87
+ if (!mapping) throw new RangeError(`unsupported Navasan symbol: ${providerSymbol}`);
88
+ if (!Array.isArray(payload)) throw new TypeError('payload must be an array');
89
+
90
+ return payload.map((item, index) => {
91
+ if (!item || typeof item !== 'object' || Array.isArray(item)) {
92
+ throw new TypeError(`payload[${index}] must be an object`);
93
+ }
94
+
95
+ const openRaw = parsePositiveNumeric(item.open, `payload[${index}].open`);
96
+ const highRaw = parsePositiveNumeric(item.high, `payload[${index}].high`);
97
+ const lowRaw = parsePositiveNumeric(item.low, `payload[${index}].low`);
98
+ const closeRaw = parsePositiveNumeric(item.close, `payload[${index}].close`);
99
+ const open = toCanonicalPrice(openRaw, mapping.rawUnit);
100
+ const high = toCanonicalPrice(highRaw, mapping.rawUnit);
101
+ const low = toCanonicalPrice(lowRaw, mapping.rawUnit);
102
+ const close = toCanonicalPrice(closeRaw, mapping.rawUnit);
103
+
104
+ if (low > open || low > close || high < open || high < close || low > high) {
105
+ throw new RangeError(`invalid OHLC ordering at payload[${index}]`);
106
+ }
107
+
108
+ return {
109
+ providerCode: 'navasan',
110
+ providerSymbol,
111
+ canonicalSymbol: mapping.canonicalSymbol,
112
+ timeframe: '1d',
113
+ openTime: unixSecondsToDate(item.timestamp, `payload[${index}].timestamp`).toISOString(),
114
+ open,
115
+ high,
116
+ low,
117
+ close,
118
+ canonicalUnit: mapping.canonicalSymbol === 'USD_IRT_FREE' ? 'IRT_PER_USD' : 'IRT_PER_GRAM',
119
+ rawUnit: mapping.rawUnit,
120
+ raw: {
121
+ timestamp: item.timestamp,
122
+ date: item.date ?? null,
123
+ open: item.open,
124
+ high: item.high,
125
+ low: item.low,
126
+ close: item.close,
127
+ },
128
+ };
129
+ });
130
+}
packages/market-domain/test/navasan-adapter.test.mjs
new
+67
@@ -0,0 +1,67 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import {
4
+ parseNavasanLatest,
5
+ NAVASAN_SYMBOLS,
6
+} from '../src/adapters/navasan.mjs';
7
+
8
+const providerTimestamp = Math.floor(new Date('2026-10-05T07:59:00Z').valueOf() / 1000);
9
+const receivedAt = new Date('2026-10-05T08:00:02Z');
10
+
11
+test('maps Navasan usd_sell to canonical USD_IRT_FREE and converts IRR to IRT', () => {
12
+ const payload = {
13
+ usd_sell: {
14
+ value: '1200000',
15
+ change: 1000,
16
+ timestamp: providerTimestamp,
17
+ date: '1405-07-13 11:29:00',
18
+ },
19
+ };
20
+
21
+ const result = parseNavasanLatest({ payload, receivedAt });
22
+
23
+ assert.equal(result.length, 1);
24
+ assert.equal(result[0].canonicalSymbol, 'USD_IRT_FREE');
25
+ assert.equal(result[0].price, 120000);
26
+ assert.equal(result[0].rawUnit, 'IRR_PER_USD');
27
+ assert.equal(result[0].providerCode, 'navasan');
28
+});
29
+
30
+test('maps Navasan 18ayar to canonical GOLD18_IRT and converts IRR to IRT', () => {
31
+ const payload = {
32
+ '18ayar': {
33
+ value: '90000000',
34
+ change: 0,
35
+ timestamp: providerTimestamp,
36
+ date: '1405-07-13 11:29:00',
37
+ },
38
+ };
39
+
40
+ const result = parseNavasanLatest({ payload, receivedAt });
41
+
42
+ assert.equal(result[0].canonicalSymbol, 'GOLD18_IRT');
43
+ assert.equal(result[0].price, 9000000);
44
+ assert.equal(result[0].rawUnit, 'IRR_PER_GRAM');
45
+});
46
+
47
+test('ignores unmapped Navasan instruments rather than guessing', () => {
48
+ const payload = {
49
+ sekkeh: { value: '1000000', timestamp: providerTimestamp, date: 'x' },
50
+ };
51
+ const result = parseNavasanLatest({ payload, receivedAt });
52
+ assert.deepEqual(result, []);
53
+});
54
+
55
+test('rejects malformed mapped observations', () => {
56
+ assert.throws(() => parseNavasanLatest({
57
+ payload: { usd_sell: { value: 'not-a-number', timestamp: providerTimestamp } },
58
+ receivedAt,
59
+ }), /numeric/i);
60
+});
61
+
62
+test('locks the explicit Navasan symbol map', () => {
63
+ assert.deepEqual(NAVASAN_SYMBOLS, {
64
+ usd_sell: { canonicalSymbol: 'USD_IRT_FREE', rawUnit: 'IRR_PER_USD' },
65
+ '18ayar': { canonicalSymbol: 'GOLD18_IRT', rawUnit: 'IRR_PER_GRAM' },
66
+ });
67
+});
packages/market-domain/test/navasan-ohlc.test.mjs
new
+48
@@ -0,0 +1,48 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import { parseNavasanOhlc } from '../src/adapters/navasan.mjs';
4
+
5
+test('parses Navasan daily OHLC and converts IRR values to canonical IRT', () => {
6
+ const payload = [{
7
+ timestamp: 1791131400,
8
+ date: '1405-07-13',
9
+ open: '1200000',
10
+ high: '1220000',
11
+ low: '1190000',
12
+ close: '1210000',
13
+ }];
14
+
15
+ const candles = parseNavasanOhlc({
16
+ providerSymbol: 'usd_sell',
17
+ payload,
18
+ });
19
+
20
+ assert.equal(candles.length, 1);
21
+ assert.equal(candles[0].canonicalSymbol, 'USD_IRT_FREE');
22
+ assert.equal(candles[0].timeframe, '1d');
23
+ assert.equal(candles[0].open, 120000);
24
+ assert.equal(candles[0].high, 122000);
25
+ assert.equal(candles[0].low, 119000);
26
+ assert.equal(candles[0].close, 121000);
27
+});
28
+
29
+test('rejects invalid OHLC ordering from Navasan', () => {
30
+ assert.throws(() => parseNavasanOhlc({
31
+ providerSymbol: '18ayar',
32
+ payload: [{
33
+ timestamp: 1791131400,
34
+ date: '1405-07-13',
35
+ open: '90000000',
36
+ high: '89000000',
37
+ low: '88000000',
38
+ close: '88500000',
39
+ }],
40
+ }), /OHLC/i);
41
+});
42
+
43
+test('rejects unmapped historical Navasan symbols', () => {
44
+ assert.throws(() => parseNavasanOhlc({
45
+ providerSymbol: 'sekkeh',
46
+ payload: [],
47
+ }), /unsupported/i);
48
+});