@fintekkers/ledger-models 1.1.19 → 1.1.21

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.
Files changed (30) hide show
  1. package/catalog/measures.json +1 -1
  2. package/hierarchy.json +20 -2
  3. package/node/fintekkers/models/price/price_type_pb.d.ts +1 -0
  4. package/node/fintekkers/models/price/price_type_pb.js +2 -1
  5. package/node/fintekkers/models/security/product_type_pb.d.ts +1 -0
  6. package/node/fintekkers/models/security/product_type_pb.js +1 -0
  7. package/node/fintekkers/requests/valuation/product_inputs.test.js +6 -6
  8. package/node/fintekkers/requests/valuation/product_inputs.test.js.map +1 -1
  9. package/node/fintekkers/requests/valuation/product_inputs.test.ts +6 -6
  10. package/node/wrappers/models/errors.d.ts +12 -0
  11. package/node/wrappers/models/errors.js +17 -1
  12. package/node/wrappers/models/errors.js.map +1 -1
  13. package/node/wrappers/models/errors.ts +19 -0
  14. package/node/wrappers/models/price/lm412EnumValues.test.d.ts +1 -0
  15. package/node/wrappers/models/price/lm412EnumValues.test.js +66 -0
  16. package/node/wrappers/models/price/lm412EnumValues.test.js.map +1 -0
  17. package/node/wrappers/models/price/lm412EnumValues.test.ts +55 -0
  18. package/node/wrappers/models/security/product_hierarchy.test.d.ts +1 -0
  19. package/node/wrappers/models/security/product_hierarchy.test.js +16 -0
  20. package/node/wrappers/models/security/product_hierarchy.test.js.map +1 -0
  21. package/node/wrappers/models/security/product_hierarchy.test.ts +14 -0
  22. package/node/wrappers/models/security/product_inputs.d.ts +46 -0
  23. package/node/wrappers/models/security/product_inputs.js +189 -0
  24. package/node/wrappers/models/security/product_inputs.js.map +1 -0
  25. package/node/wrappers/models/security/product_inputs.test.d.ts +1 -0
  26. package/node/wrappers/models/security/product_inputs.test.js +144 -0
  27. package/node/wrappers/models/security/product_inputs.test.js.map +1 -0
  28. package/node/wrappers/models/security/product_inputs.test.ts +141 -0
  29. package/node/wrappers/models/security/product_inputs.ts +193 -0
  30. package/package.json +1 -1
@@ -0,0 +1,193 @@
1
+ /**
2
+ * Which ProductInput arm values a security, and which market data that arm
3
+ * needs. Backed by the valuation_arms section of
4
+ * ledger-models-protos/hierarchy.json; there is no mapping table in code.
5
+ * Same rule in Java / Rust / Python / JS-TS. See docs/adr/valuation_arms.md.
6
+ *
7
+ * Resolution walks from the security's product_type up its parent chain. At
8
+ * each node a coupon_types entry matching bond_details.coupon_type wins, then
9
+ * the node's plain entry, then an unpriced listing. The first node with any
10
+ * match decides. No match, or an unpriced match, throws
11
+ * UnmappedProductTypeError: there is no default arm. A broken section throws
12
+ * a plain Error at load, like product_hierarchy.
13
+ */
14
+
15
+ import hierarchy from '../../../../hierarchy.json';
16
+ import { CouponTypeProto } from '../../../fintekkers/models/security/coupon_type_pb';
17
+ import { ProductTypeProto } from '../../../fintekkers/models/security/product_type_pb';
18
+ import { SecurityProto } from '../../../fintekkers/models/security/security_pb';
19
+ import { ProductInput } from '../../../fintekkers/requests/valuation/product_inputs_pb';
20
+ import { UnmappedProductTypeError } from '../errors';
21
+ import { allProductTypes, descendantsOf, parentOf } from './product_hierarchy';
22
+
23
+ /** The closed requires vocabulary: market data an arm needs beyond the price. */
24
+ export type MarketDataKind = 'CPI_U' | 'FRN_FIXINGS' | 'FRN_CURVE';
25
+
26
+ const MARKET_DATA_KINDS: ReadonlyArray<string> = ['CPI_U', 'FRN_FIXINGS', 'FRN_CURVE'];
27
+
28
+ /** Map key for a node's entry with no coupon_types. */
29
+ const ANY_COUPON = '';
30
+
31
+ interface Arm {
32
+ arm: string;
33
+ requires: ReadonlyArray<MarketDataKind>;
34
+ }
35
+
36
+ function hasOwn(o: object, key: string): boolean {
37
+ return Object.prototype.hasOwnProperty.call(o, key);
38
+ }
39
+
40
+ function enumName(e: object, value: number): string {
41
+ for (const [k, v] of Object.entries(e)) {
42
+ if (v === value) return k;
43
+ }
44
+ return String(value);
45
+ }
46
+
47
+ /** A parsed, checked valuation_arms section. */
48
+ export class ValuationArms {
49
+ /** @param entries node -> coupon type name (or '' for the plain entry) -> arm */
50
+ constructor(
51
+ private readonly entries: Map<string, Map<string, Arm>>,
52
+ private readonly unpriced: ReadonlySet<string>,
53
+ ) {}
54
+
55
+ /** The deepest matching node's arm, or null when unmapped or unpriced. */
56
+ resolve(productType: string, couponType: string): Arm | null {
57
+ for (let node: string | null = productType; node !== null; node = parentOf(node)) {
58
+ const byCoupon = this.entries.get(node);
59
+ const arm = byCoupon?.get(couponType) ?? byCoupon?.get(ANY_COUPON);
60
+ if (arm) return arm;
61
+ if (this.unpriced.has(node)) return null;
62
+ }
63
+ return null;
64
+ }
65
+
66
+ require(productType: string, couponType: string): Arm {
67
+ const arm = this.resolve(productType, couponType);
68
+ if (!arm) throw new UnmappedProductTypeError(productType, couponType);
69
+ return arm;
70
+ }
71
+
72
+ /** Leaf product types (active and planned) with neither an entry nor an unpriced listing. */
73
+ uncoveredLeaves(): string[] {
74
+ return allProductTypes().filter(n => descendantsOf(n).length === 0 && !this.covers(n));
75
+ }
76
+
77
+ private covers(productType: string): boolean {
78
+ for (let node: string | null = productType; node !== null; node = parentOf(node)) {
79
+ if (this.entries.has(node) || this.unpriced.has(node)) return true;
80
+ }
81
+ return false;
82
+ }
83
+ }
84
+
85
+ function bad(detail: string): Error {
86
+ return new Error(`hierarchy.json valuation_arms: ${detail}`);
87
+ }
88
+
89
+ function stringOf(body: Record<string, unknown>, key: string): string {
90
+ const v = body[key];
91
+ if (typeof v !== 'string') throw bad(`'${key}' must be a string in ${JSON.stringify(body)}`);
92
+ return v;
93
+ }
94
+
95
+ function stringsOf(body: Record<string, unknown>, key: string): string[] {
96
+ const v = body[key];
97
+ if (!Array.isArray(v) || !v.every(x => typeof x === 'string')) {
98
+ throw bad(`'${key}' must be a list of strings in ${JSON.stringify(body)}`);
99
+ }
100
+ return v as string[];
101
+ }
102
+
103
+ /**
104
+ * Parses and checks a valuation_arms section against the shipped
105
+ * product_types tree, the ProductInput oneof and the CouponTypeProto /
106
+ * MarketDataKind vocabularies. Throws on any failure. Exported for tests.
107
+ */
108
+ export function parseValuationArms(section: unknown): ValuationArms {
109
+ if (typeof section !== 'object' || section === null || Array.isArray(section)) {
110
+ throw bad('section must be an object');
111
+ }
112
+ const s = section as Record<string, unknown>;
113
+ const productTypes = new Set(allProductTypes());
114
+ const arms = new Set(Object.keys(ProductInput.InputCase)
115
+ .filter(k => k !== 'INPUT_NOT_SET')
116
+ .map(k => k.toLowerCase()));
117
+
118
+ if (!Array.isArray(s.entries)) throw bad("'entries' must be a list");
119
+ const entries = new Map<string, Map<string, Arm>>();
120
+ for (const raw of s.entries as unknown[]) {
121
+ if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
122
+ throw bad(`entry must be an object, got ${JSON.stringify(raw)}`);
123
+ }
124
+ const body = raw as Record<string, unknown>;
125
+ const node = stringOf(body, 'node');
126
+ if (!productTypes.has(node)) throw bad(`node '${node}' is not in product_types`);
127
+ const arm = stringOf(body, 'arm');
128
+ if (!arms.has(arm)) throw bad(`arm '${arm}' is not a ProductInput.input field`);
129
+ const requires = stringsOf(body, 'requires').map(r => {
130
+ if (!MARKET_DATA_KINDS.includes(r)) {
131
+ throw bad(`requires value '${r}' is not one of CPI_U, FRN_FIXINGS, FRN_CURVE`);
132
+ }
133
+ return r as MarketDataKind;
134
+ });
135
+ let coupons = [ANY_COUPON];
136
+ if (hasOwn(body, 'coupon_types')) {
137
+ coupons = stringsOf(body, 'coupon_types');
138
+ for (const c of coupons) {
139
+ if (!hasOwn(CouponTypeProto, c)) throw bad(`coupon_types value '${c}' is not a CouponTypeProto name`);
140
+ }
141
+ }
142
+ let byCoupon = entries.get(node);
143
+ if (!byCoupon) {
144
+ byCoupon = new Map();
145
+ entries.set(node, byCoupon);
146
+ }
147
+ const value: Arm = Object.freeze({ arm, requires: Object.freeze(requires) });
148
+ for (const c of coupons) {
149
+ if (byCoupon.has(c)) throw bad(`duplicate entry for node ${node}${c ? `, coupon_type ${c}` : ''}`);
150
+ byCoupon.set(c, value);
151
+ }
152
+ }
153
+
154
+ const unpriced = new Set<string>();
155
+ for (const node of stringsOf(s, 'unpriced')) {
156
+ if (!productTypes.has(node)) throw bad(`node '${node}' is not in product_types`);
157
+ if (entries.has(node)) throw bad(`node ${node} is both mapped and unpriced`);
158
+ unpriced.add(node);
159
+ }
160
+ return new ValuationArms(entries, unpriced);
161
+ }
162
+
163
+ let shippedArms: ValuationArms | undefined;
164
+
165
+ /** The shipped valuation_arms section, parsed and checked on first use. Exported for tests. */
166
+ export function shippedValuationArms(): ValuationArms {
167
+ if (!shippedArms) {
168
+ const section = (hierarchy as Record<string, unknown>).valuation_arms;
169
+ if (section === undefined) throw bad('section missing from hierarchy.json');
170
+ shippedArms = parseValuationArms(section);
171
+ }
172
+ return shippedArms;
173
+ }
174
+
175
+ function productTypeOf(security: SecurityProto): string {
176
+ return enumName(ProductTypeProto, security.getProductType());
177
+ }
178
+
179
+ /** Read from the proto field only; unset bond_details reads as UNKNOWN_COUPON_TYPE. */
180
+ function couponTypeOf(security: SecurityProto): string {
181
+ const details = security.getBondDetails();
182
+ return details ? enumName(CouponTypeProto, details.getCouponType()) : 'UNKNOWN_COUPON_TYPE';
183
+ }
184
+
185
+ /** Name of the ProductInput.input oneof field that values `security`, e.g. 'tips'. */
186
+ export function armFor(security: SecurityProto): string {
187
+ return shippedValuationArms().require(productTypeOf(security), couponTypeOf(security)).arm;
188
+ }
189
+
190
+ /** Market data the security's arm needs, in file order. A fresh array; empty when none. */
191
+ export function requiredMarketData(security: SecurityProto): MarketDataKind[] {
192
+ return [...shippedValuationArms().require(productTypeOf(security), couponTypeOf(security)).requires];
193
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fintekkers/ledger-models",
3
3
  "todo": "Replace the version with build script version number",
4
- "version": "1.1.19",
4
+ "version": "1.1.21",
5
5
  "description": "ledger model protos ",
6
6
  "authors": [
7
7
  "David Doherty",