@kasufinance/kasu-sdk 2.5.0 → 2.7.0

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 (128) hide show
  1. package/README.md +144 -7
  2. package/dist/bundle.cjs.js +1667 -21
  3. package/dist/bundle.esm.js +1630 -22
  4. package/dist/domain/au-minimum.d.ts +135 -0
  5. package/dist/domain/au-minimum.js +154 -0
  6. package/dist/domain/au-minimum.js.map +1 -0
  7. package/dist/domain/index.d.ts +18 -3
  8. package/dist/domain/index.js +13 -3
  9. package/dist/domain/index.js.map +1 -1
  10. package/dist/domain/loan-contract.d.ts +174 -0
  11. package/dist/domain/loan-contract.js +160 -0
  12. package/dist/domain/loan-contract.js.map +1 -0
  13. package/dist/domain/requests.d.ts +219 -0
  14. package/dist/domain/requests.js +218 -0
  15. package/dist/domain/requests.js.map +1 -0
  16. package/dist/domain/settlement.d.ts +97 -0
  17. package/dist/domain/settlement.js +117 -0
  18. package/dist/domain/settlement.js.map +1 -0
  19. package/dist/domain/wallet-errors.d.ts +92 -0
  20. package/dist/domain/wallet-errors.js +155 -0
  21. package/dist/domain/wallet-errors.js.map +1 -0
  22. package/dist/facade/chain-configs.js +7 -1
  23. package/dist/facade/chain-configs.js.map +1 -1
  24. package/dist/facade/deposits.js +2 -1
  25. package/dist/facade/deposits.js.map +1 -1
  26. package/dist/facade/flows.d.ts +95 -0
  27. package/dist/facade/flows.js +116 -0
  28. package/dist/facade/flows.js.map +1 -0
  29. package/dist/facade/index.d.ts +3 -0
  30. package/dist/facade/index.js +3 -0
  31. package/dist/facade/index.js.map +1 -1
  32. package/dist/facade/kasu.d.ts +6 -2
  33. package/dist/facade/kasu.js +28 -3
  34. package/dist/facade/kasu.js.map +1 -1
  35. package/dist/facade/read-only.d.ts +12 -0
  36. package/dist/facade/read-only.js +13 -0
  37. package/dist/facade/read-only.js.map +1 -0
  38. package/dist/facade/user-portfolio.d.ts +18 -0
  39. package/dist/facade/user-portfolio.js +23 -0
  40. package/dist/facade/user-portfolio.js.map +1 -1
  41. package/dist/flows/deposit-flow.d.ts +301 -0
  42. package/dist/flows/deposit-flow.js +358 -0
  43. package/dist/flows/deposit-flow.js.map +1 -0
  44. package/dist/flows/flow.d.ts +72 -0
  45. package/dist/flows/flow.js +110 -0
  46. package/dist/flows/flow.js.map +1 -0
  47. package/dist/flows/index.d.ts +19 -0
  48. package/dist/flows/index.js +17 -0
  49. package/dist/flows/index.js.map +1 -0
  50. package/dist/flows/observable.d.ts +59 -0
  51. package/dist/flows/observable.js +81 -0
  52. package/dist/flows/observable.js.map +1 -0
  53. package/dist/flows/withdraw-flow.d.ts +99 -0
  54. package/dist/flows/withdraw-flow.js +83 -0
  55. package/dist/flows/withdraw-flow.js.map +1 -0
  56. package/dist/index.d.ts +2 -0
  57. package/dist/index.js +7 -0
  58. package/dist/index.js.map +1 -1
  59. package/dist/services/DataService/data-service.js +3 -11
  60. package/dist/services/DataService/data-service.js.map +1 -1
  61. package/dist/services/DataService/directus-client.d.ts +26 -0
  62. package/dist/services/DataService/directus-client.js +38 -0
  63. package/dist/services/DataService/directus-client.js.map +1 -0
  64. package/dist/services/UserLending/user-lending.js +11 -7
  65. package/dist/services/UserLending/user-lending.js.map +1 -1
  66. package/package.json +8 -2
  67. package/src/domain/au-minimum.ts +192 -0
  68. package/src/domain/index.ts +73 -3
  69. package/src/domain/loan-contract.ts +275 -0
  70. package/src/domain/requests.ts +467 -0
  71. package/src/domain/settlement.ts +161 -0
  72. package/src/domain/wallet-errors.ts +173 -0
  73. package/src/facade/chain-configs.ts +7 -1
  74. package/src/facade/deposits.ts +2 -3
  75. package/src/facade/flows.ts +172 -0
  76. package/src/facade/index.ts +8 -0
  77. package/src/facade/kasu.ts +43 -3
  78. package/src/facade/read-only.ts +13 -0
  79. package/src/facade/user-portfolio.ts +24 -0
  80. package/src/flows/deposit-flow.ts +775 -0
  81. package/src/flows/flow.ts +108 -0
  82. package/src/flows/index.ts +45 -0
  83. package/src/flows/observable.ts +97 -0
  84. package/src/flows/withdraw-flow.ts +210 -0
  85. package/src/index.ts +9 -0
  86. package/src/services/DataService/data-service.ts +7 -25
  87. package/src/services/DataService/directus-client.ts +54 -0
  88. package/src/services/UserLending/user-lending.ts +17 -21
  89. package/dist/domain/deposit-bounds.test.d.ts +0 -1
  90. package/dist/domain/deposit-bounds.test.js +0 -135
  91. package/dist/domain/deposit-bounds.test.js.map +0 -1
  92. package/dist/domain/partners.test.d.ts +0 -1
  93. package/dist/domain/partners.test.js +0 -53
  94. package/dist/domain/partners.test.js.map +0 -1
  95. package/dist/domain/pools.test.d.ts +0 -1
  96. package/dist/domain/pools.test.js +0 -184
  97. package/dist/domain/pools.test.js.map +0 -1
  98. package/dist/domain/rates.test.d.ts +0 -1
  99. package/dist/domain/rates.test.js +0 -181
  100. package/dist/domain/rates.test.js.map +0 -1
  101. package/dist/domain/tranche-display-name.test.d.ts +0 -1
  102. package/dist/domain/tranche-display-name.test.js +0 -58
  103. package/dist/domain/tranche-display-name.test.js.map +0 -1
  104. package/dist/domain/tranches.test.d.ts +0 -1
  105. package/dist/domain/tranches.test.js +0 -206
  106. package/dist/domain/tranches.test.js.map +0 -1
  107. package/dist/facade/config.test.d.ts +0 -1
  108. package/dist/facade/config.test.js +0 -216
  109. package/dist/facade/config.test.js.map +0 -1
  110. package/dist/facade/facade.test.d.ts +0 -1
  111. package/dist/facade/facade.test.js +0 -201
  112. package/dist/facade/facade.test.js.map +0 -1
  113. package/dist/services/Locking/calculate-apy.test.d.ts +0 -1
  114. package/dist/services/Locking/calculate-apy.test.js +0 -41
  115. package/dist/services/Locking/calculate-apy.test.js.map +0 -1
  116. package/dist/tests/sample.test.d.ts +0 -1
  117. package/dist/tests/sample.test.js +0 -59
  118. package/dist/tests/sample.test.js.map +0 -1
  119. package/src/domain/deposit-bounds.test.ts +0 -200
  120. package/src/domain/partners.test.ts +0 -83
  121. package/src/domain/pools.test.ts +0 -260
  122. package/src/domain/rates.test.ts +0 -254
  123. package/src/domain/tranche-display-name.test.ts +0 -96
  124. package/src/domain/tranches.test.ts +0 -300
  125. package/src/facade/config.test.ts +0 -265
  126. package/src/facade/facade.test.ts +0 -250
  127. package/src/services/Locking/calculate-apy.test.ts +0 -52
  128. package/src/tests/sample.test.ts +0 -63
@@ -1,260 +0,0 @@
1
- import { PoolOverview, TrancheData } from '../services/DataService/types';
2
-
3
- import {
4
- maxNetRateCeiling,
5
- pickHighestYieldTranche,
6
- poolMaxApy,
7
- selectVisiblePools,
8
- } from './pools';
9
- import { netEffectiveApy } from './rates';
10
-
11
- /**
12
- * Ported from kasu-ui `select-visible-pools.test.ts` and the
13
- * `pickHighestYieldTranche` / `formatMaxNetRate` cases of
14
- * `pick-best-tranche.test.ts`. The `formatMaxNetRate` assertions are on the
15
- * ceiling NUMBER here; their rendered strings stay in kasu-ui.
16
- */
17
-
18
- function tranche(
19
- name: string,
20
- apy: string,
21
- poolCapacity: string,
22
- maxApy?: string,
23
- ): TrancheData {
24
- return { id: name, name, apy, poolCapacity, maxApy } as unknown as TrancheData;
25
- }
26
-
27
- function pool(over: Partial<PoolOverview>): PoolOverview {
28
- return {
29
- id: 'p',
30
- poolName: 'p',
31
- isActive: true,
32
- isOversubscribed: false,
33
- tranches: [tranche('Senior', '0.09', '250000', '0.10')],
34
- ...over,
35
- } as unknown as PoolOverview;
36
- }
37
-
38
- const withCapacity = (): TrancheData[] => [
39
- tranche('Senior', '0.09', '250000', '0.10'),
40
- ];
41
- const noCapacity = (): TrancheData[] => [tranche('Senior', '0.09', '0', '0.10')];
42
-
43
- /** The live performance fee on all deployments. */
44
- const FEE = 10;
45
-
46
- describe('poolMaxApy', () => {
47
- it('takes the highest usable maxApy across tranches', () => {
48
- expect(
49
- poolMaxApy(
50
- pool({
51
- tranches: [
52
- tranche('Senior', '0.09', '1', '0.10'),
53
- tranche('Junior', '0.12', '1', '0.18'),
54
- ],
55
- }),
56
- ),
57
- ).toBe(0.18);
58
- });
59
-
60
- it('is 0 when nothing is usable', () => {
61
- expect(poolMaxApy(pool({ tranches: [] }))).toBe(0);
62
- expect(
63
- poolMaxApy(pool({ tranches: [tranche('Senior', '0.09', '1', 'x')] })),
64
- ).toBe(0);
65
- });
66
- });
67
-
68
- describe('selectVisiblePools', () => {
69
- it('drops inactive pools', () => {
70
- const pools = [
71
- pool({ id: 'active', isActive: true }),
72
- pool({ id: 'inactive', isActive: false }),
73
- ];
74
- expect(selectVisiblePools(pools).map((p) => p.id)).toEqual(['active']);
75
- });
76
-
77
- it('drops oversubscribed pools', () => {
78
- const pools = [
79
- pool({ id: 'open', isOversubscribed: false }),
80
- pool({ id: 'full', isOversubscribed: true }),
81
- ];
82
- expect(selectVisiblePools(pools).map((p) => p.id)).toEqual(['open']);
83
- });
84
-
85
- it('sorts pools with capacity ahead of pools without', () => {
86
- const pools = [
87
- pool({ id: 'empty', tranches: noCapacity() }),
88
- pool({ id: 'has-cap', tranches: withCapacity() }),
89
- ];
90
- expect(selectVisiblePools(pools).map((p) => p.id)).toEqual([
91
- 'has-cap',
92
- 'empty',
93
- ]);
94
- });
95
-
96
- it('sorts by highest max APY within the same capacity bucket', () => {
97
- const pools = [
98
- pool({
99
- id: 'low',
100
- tranches: [tranche('Senior', '0.08', '250000', '0.08')],
101
- }),
102
- pool({
103
- id: 'high',
104
- tranches: [tranche('Senior', '0.15', '250000', '0.15')],
105
- }),
106
- ];
107
- expect(selectVisiblePools(pools).map((p) => p.id)).toEqual([
108
- 'high',
109
- 'low',
110
- ]);
111
- });
112
-
113
- it('does not mutate the input array', () => {
114
- const pools = [
115
- pool({ id: 'a', tranches: noCapacity() }),
116
- pool({ id: 'b', tranches: withCapacity() }),
117
- ];
118
- const before = pools.map((p) => p.id);
119
- selectVisiblePools(pools);
120
- expect(pools.map((p) => p.id)).toEqual(before);
121
- });
122
- });
123
-
124
- describe('pickHighestYieldTranche', () => {
125
- const named = (poolName: string, tranches: TrancheData[]): PoolOverview =>
126
- pool({ id: poolName, poolName, tranches });
127
-
128
- it('returns null for empty / nullish input', () => {
129
- expect(pickHighestYieldTranche(undefined)).toBeNull();
130
- expect(pickHighestYieldTranche(null)).toBeNull();
131
- expect(pickHighestYieldTranche([])).toBeNull();
132
- });
133
-
134
- it('picks the highest base APY across all pools with capacity', () => {
135
- const best = pickHighestYieldTranche([
136
- named('A', [tranche('Junior', '0.12', '5000')]),
137
- named('B', [
138
- tranche('Senior', '0.18', '5000'),
139
- tranche('Mezz', '0.15', '5000'),
140
- ]),
141
- ]);
142
- expect(best?.pool.poolName).toBe('B');
143
- expect(best?.tranche.name).toBe('Senior');
144
- expect(best?.apy).toBeCloseTo(0.18);
145
- });
146
-
147
- it('skips tranches without capacity (< 1 unit)', () => {
148
- const best = pickHighestYieldTranche([
149
- named('Full', [tranche('Junior', '0.30', '0')]),
150
- named('Live', [tranche('Senior', '0.10', '2000')]),
151
- ]);
152
- expect(best?.pool.poolName).toBe('Live');
153
- expect(best?.apy).toBeCloseTo(0.1);
154
- });
155
-
156
- it('breaks APY ties toward more remaining capacity', () => {
157
- const best = pickHighestYieldTranche([
158
- named('Small', [tranche('Junior', '0.15', '1000')]),
159
- named('Big', [tranche('Junior', '0.15', '9000')]),
160
- ]);
161
- expect(best?.pool.poolName).toBe('Big');
162
- });
163
-
164
- it('ignores zero / non-finite APY values', () => {
165
- const best = pickHighestYieldTranche([
166
- named('Zero', [tranche('Junior', '0', '5000')]),
167
- named('NaN', [tranche('Junior', 'not-a-number', '5000')]),
168
- named('Real', [tranche('Junior', '0.09', '5000')]),
169
- ]);
170
- expect(best?.pool.poolName).toBe('Real');
171
- });
172
-
173
- it('reads the BASE rate, not the FTD-widened maxApy', () => {
174
- // A ceiling claim and a named tranche's rate are different statements.
175
- const best = pickHighestYieldTranche([
176
- named('A', [tranche('Junior', '0.12', '5000', '0.30')]),
177
- named('B', [tranche('Senior', '0.18', '5000', '0.18')]),
178
- ]);
179
- expect(best?.pool.poolName).toBe('B');
180
- });
181
- });
182
-
183
- describe('maxNetRateCeiling', () => {
184
- const named = (poolName: string, tranches: TrancheData[]): PoolOverview =>
185
- pool({ id: poolName, poolName, tranches });
186
-
187
- it('returns the highest ceiling across all pools, net of the fee', () => {
188
- const rate = maxNetRateCeiling(
189
- [
190
- named('A', [tranche('Junior', '0.12', '5000', '0.18')]),
191
- named('B', [tranche('Senior', '0.10', '5000', '0.22')]),
192
- ],
193
- FEE,
194
- );
195
- // 0.22 gross → 19.60% net, the same arithmetic as everywhere else.
196
- expect(rate).toBe(netEffectiveApy(0.22, FEE));
197
- expect(rate).toBeCloseTo(0.196, 4);
198
- });
199
-
200
- it('prefers the FTD-inclusive maxApy over the base rate', () => {
201
- // The strategies card's range tops out at maxApy; an "up to" claim
202
- // quoting the base rate would contradict it on the same screen.
203
- expect(
204
- maxNetRateCeiling(
205
- [named('A', [tranche('Junior', '0.10', '5000', '0.22')])],
206
- FEE,
207
- ),
208
- ).toBe(netEffectiveApy(0.22, FEE));
209
- });
210
-
211
- it('falls back to the base rate when a tranche carries no maxApy', () => {
212
- expect(
213
- maxNetRateCeiling(
214
- [named('A', [tranche('Junior', '0.22', '5000')])],
215
- FEE,
216
- ),
217
- ).toBe(netEffectiveApy(0.22, FEE));
218
- });
219
-
220
- it('skips tranches without capacity — an unreachable rate is not a claim', () => {
221
- const rate = maxNetRateCeiling(
222
- [
223
- named('Full', [tranche('Junior', '0.30', '0', '0.30')]),
224
- named('Live', [tranche('Senior', '0.10', '5000', '0.10')]),
225
- ],
226
- FEE,
227
- );
228
- expect(rate).toBe(netEffectiveApy(0.1, FEE));
229
- expect(rate).toBeCloseTo(0.0896, 4);
230
- });
231
-
232
- it('returns null rather than a gross figure when the fee is out of domain', () => {
233
- const pools = [named('A', [tranche('Junior', '0.22', '5000', '0.22')])];
234
- expect(maxNetRateCeiling(pools, 1000)).toBeNull();
235
- expect(maxNetRateCeiling(pools, -1)).toBeNull();
236
- expect(maxNetRateCeiling(pools, NaN)).toBeNull();
237
- // A 100% fee leaves nothing to claim.
238
- expect(maxNetRateCeiling(pools, 100)).toBeNull();
239
- });
240
-
241
- it('returns null for empty, nullish or rate-less input', () => {
242
- expect(maxNetRateCeiling(undefined, FEE)).toBeNull();
243
- expect(maxNetRateCeiling(null, FEE)).toBeNull();
244
- expect(maxNetRateCeiling([], FEE)).toBeNull();
245
- expect(
246
- maxNetRateCeiling(
247
- [named('Zero', [tranche('Junior', '0', '5000', '0')])],
248
- FEE,
249
- ),
250
- ).toBeNull();
251
- });
252
-
253
- it('agrees with the top of the tranche range it must sit alongside', () => {
254
- // Both read maxApy through netEffectiveApy; they must not disagree.
255
- const tranches = [tranche('Senior', '0.14', '5000', '0.22')];
256
- expect(maxNetRateCeiling([named('A', tranches)], FEE)).toBe(
257
- netEffectiveApy(0.22, FEE),
258
- );
259
- });
260
- });
@@ -1,254 +0,0 @@
1
- import * as fc from 'fast-check';
2
-
3
- import {
4
- apyToEpochRate,
5
- epochRateToApy,
6
- EPOCHS_IN_YEAR,
7
- netEffectiveApy,
8
- } from './rates';
9
-
10
- /**
11
- * Ported from kasu-ui `src/features/lending/lib/interest-rate.test.ts`. The
12
- * `formatEffectiveRate` cases stay there — they assert copy and locale, which
13
- * this layer does not own.
14
- */
15
-
16
- /** The live performance fee on all deployments (subgraph units: 10 = ten percent). */
17
- const LIVE_FEE_PERCENT = 10;
18
-
19
- /**
20
- * Spec §9 — every distinct per-epoch rate configured on Base, read on-chain
21
- * 2026-09-01, with the NET Effective Interest Rate each must display at the
22
- * live 10% fee. This table IS the review sanity check: a correct
23
- * implementation reproduces the whole column, including the 30% crossover row
24
- * (26.30% gross nominal → 26.64% net APY) which moves UP while every other row
25
- * moves down, because the two errors in the old display — no fee deducted, and
26
- * a spurious de-compounding — pull in opposite directions and cross over near
27
- * 28%.
28
- */
29
- const GROUND_TRUTH: readonly { grossApy: number; netPercent: number }[] = [
30
- { grossApy: 0.1, netPercent: 8.96 },
31
- { grossApy: 0.105, netPercent: 9.4 },
32
- { grossApy: 0.12, netPercent: 10.74 },
33
- { grossApy: 0.14, netPercent: 12.52 },
34
- { grossApy: 0.15, netPercent: 13.41 },
35
- { grossApy: 0.16, netPercent: 14.29 },
36
- { grossApy: 0.2, netPercent: 17.84 },
37
- { grossApy: 0.22, netPercent: 19.6 },
38
- { grossApy: 0.3, netPercent: 26.64 },
39
- ];
40
-
41
- describe('netEffectiveApy — spec §9 ground truth', () => {
42
- it.each(GROUND_TRUTH)(
43
- 'gross $grossApy at a 10% fee is $netPercent% net',
44
- ({ grossApy, netPercent }) => {
45
- expect(netEffectiveApy(grossApy, LIVE_FEE_PERCENT) * 100).toBeCloseTo(
46
- netPercent,
47
- 2,
48
- );
49
- },
50
- );
51
-
52
- it('reproduces the 30% crossover — the net APY is ABOVE the old gross nominal figure', () => {
53
- // Old display: 52.17857 · ((1.30)^(1/52.17857) − 1) = 26.30%. New: 26.64%.
54
- const oldGrossNominal = EPOCHS_IN_YEAR * (1.3 ** (1 / EPOCHS_IN_YEAR) - 1);
55
- expect(oldGrossNominal * 100).toBeCloseTo(26.3, 1);
56
- expect(netEffectiveApy(0.3, LIVE_FEE_PERCENT)).toBeGreaterThan(
57
- oldGrossNominal,
58
- );
59
- });
60
- });
61
-
62
- describe('netEffectiveApy — units (spec §2.3)', () => {
63
- it('reads feePercent as a PERCENTAGE: 22% gross at fee 10 is ≈19.602%', () => {
64
- expect(netEffectiveApy(0.22, 10)).toBeCloseTo(0.19602, 5);
65
- });
66
-
67
- it('does not silently accept the 0..1 fraction — 0.1 is a 0.1% fee, not 10%', () => {
68
- const wrongUnit = netEffectiveApy(0.22, 0.1);
69
- const correct = netEffectiveApy(0.22, 10);
70
- // It is a finite, in-range number (we cannot reject it — 0.1% is a
71
- // legal fee), so the guard cannot catch this. What protects us is that
72
- // it is MATERIALLY different: ~2.37pp too high, not a plausible
73
- // near-miss that could sit on screen unnoticed.
74
- expect(wrongUnit).toBeCloseTo(0.21976, 5);
75
- expect(wrongUnit - correct).toBeGreaterThan(0.02);
76
- });
77
-
78
- it('rejects the RAW subgraph integer (1000 = 10%, pre-integerToPercentage2)', () => {
79
- expect(netEffectiveApy(0.22, 1000)).toBeNaN();
80
- });
81
-
82
- it('rejects a fee outside 0..100', () => {
83
- expect(netEffectiveApy(0.22, 101)).toBeNaN();
84
- expect(netEffectiveApy(0.22, -1)).toBeNaN();
85
- expect(netEffectiveApy(0.22, NaN)).toBeNaN();
86
- expect(netEffectiveApy(0.22, Infinity)).toBeNaN();
87
- });
88
-
89
- it('accepts the domain edges', () => {
90
- expect(netEffectiveApy(0.22, 0)).toBe(0.22);
91
- // A 100% fee leaves the lender nothing — 0, not NaN: the fee is
92
- // knowable and the answer is "you earn zero", which consumers render
93
- // as "—".
94
- expect(netEffectiveApy(0.22, 100)).toBe(0);
95
- });
96
-
97
- it('returns NaN for a non-finite or negative gross APY (callers render "—")', () => {
98
- expect(netEffectiveApy(NaN, 10)).toBeNaN();
99
- expect(netEffectiveApy(Infinity, 10)).toBeNaN();
100
- expect(netEffectiveApy(-Infinity, 10)).toBeNaN();
101
- expect(netEffectiveApy(-0.01, 10)).toBeNaN();
102
- });
103
- });
104
-
105
- // The FULL in-domain range, down to and including zero and the denormals. The
106
- // invariants below are the reason `netEffectiveApy` is written with
107
- // `log1p`/`expm1`: the direct `(1 + x) ** n - 1` form breaks the `net <= gross`
108
- // invariant for tiny rates, and the property runner finds that in a few hundred
109
- // cases. Narrowing this arb would hide it.
110
- const grossApyArb = fc.double({
111
- min: 0,
112
- max: 5,
113
- noNaN: true,
114
- noDefaultInfinity: true,
115
- });
116
- const feePercentArb = fc.double({
117
- min: 0,
118
- max: 100,
119
- noNaN: true,
120
- noDefaultInfinity: true,
121
- });
122
- /**
123
- * A fee as the subgraph can actually express one: `integerToPercentage2`
124
- * divides a uint by 100, so every reachable value is a 2dp multiple of 0.01 in
125
- * 0..100. Used only for the STRICT-decrease property, which cannot hold for a
126
- * fee of 1e-300 percent — `1 − feePercent/100` rounds to exactly 1 and the net
127
- * rate is the gross one. That is arithmetic, not a defect, and no such fee
128
- * exists.
129
- */
130
- const realisticFeePercentArb = fc
131
- .integer({ min: 1, max: 10_000 })
132
- .map((n) => n / 100);
133
-
134
- describe('netEffectiveApy — invariants', () => {
135
- it('never returns more than the gross APY it came from', () => {
136
- fc.assert(
137
- fc.property(grossApyArb, feePercentArb, (grossApy, feePercent) => {
138
- expect(netEffectiveApy(grossApy, feePercent)).toBeLessThanOrEqual(
139
- grossApy,
140
- );
141
- }),
142
- );
143
- });
144
-
145
- it('equals the gross APY EXACTLY when the fee is zero', () => {
146
- fc.assert(
147
- // Exact equality, not "close to": if the fee is ever set to zero
148
- // on-chain, the rate must be the SDK's own APY, with no float drift
149
- // from the de-compound/re-compound round trip.
150
- fc.property(grossApyArb, (grossApy) => {
151
- expect(netEffectiveApy(grossApy, 0)).toBe(grossApy);
152
- }),
153
- );
154
- });
155
-
156
- it('is STRICTLY below the gross APY for any real (2dp) non-zero fee', () => {
157
- fc.assert(
158
- fc.property(
159
- grossApyArb,
160
- realisticFeePercentArb,
161
- (grossApy, feePercent) => {
162
- if (grossApy === 0) return;
163
- expect(netEffectiveApy(grossApy, feePercent)).toBeLessThan(
164
- grossApy,
165
- );
166
- },
167
- ),
168
- );
169
- });
170
-
171
- it('decreases monotonically as the fee rises', () => {
172
- fc.assert(
173
- fc.property(
174
- grossApyArb,
175
- feePercentArb,
176
- feePercentArb,
177
- (grossApy, a, b) => {
178
- const [lo, hi] = a <= b ? [a, b] : [b, a];
179
- expect(netEffectiveApy(grossApy, hi)).toBeLessThanOrEqual(
180
- netEffectiveApy(grossApy, lo),
181
- );
182
- },
183
- ),
184
- );
185
- });
186
-
187
- it('increases monotonically as the gross APY rises', () => {
188
- fc.assert(
189
- fc.property(
190
- grossApyArb,
191
- grossApyArb,
192
- feePercentArb,
193
- (a, b, feePercent) => {
194
- const [lo, hi] = a <= b ? [a, b] : [b, a];
195
- expect(netEffectiveApy(hi, feePercent)).toBeGreaterThanOrEqual(
196
- netEffectiveApy(lo, feePercent),
197
- );
198
- },
199
- ),
200
- );
201
- });
202
-
203
- it('is never negative for any in-domain input', () => {
204
- fc.assert(
205
- fc.property(grossApyArb, feePercentArb, (grossApy, feePercent) => {
206
- expect(
207
- netEffectiveApy(grossApy, feePercent),
208
- ).toBeGreaterThanOrEqual(0);
209
- }),
210
- );
211
- });
212
- });
213
-
214
- describe('epochRateToApy / apyToEpochRate', () => {
215
- it('compounds a per-epoch rate exactly as the data service always has', () => {
216
- // The literal expression `calculateApyForTranche` carried inline.
217
- for (const r of [0, 0.001, 0.0025, 0.003, 0.005, 0.01]) {
218
- expect(epochRateToApy(r)).toBe((1 + r) ** EPOCHS_IN_YEAR - 1);
219
- }
220
- });
221
-
222
- it('round-trips within 1e-12 for realistic per-epoch rates', () => {
223
- fc.assert(
224
- fc.property(
225
- // 0.0001..0.02 per epoch ≈ 0.5%..190% APY — wider than any
226
- // rate the platform has configured.
227
- fc.double({
228
- min: 0.0001,
229
- max: 0.02,
230
- noNaN: true,
231
- noDefaultInfinity: true,
232
- }),
233
- (r) => {
234
- expect(apyToEpochRate(epochRateToApy(r))).toBeCloseTo(r, 12);
235
- },
236
- ),
237
- );
238
- });
239
-
240
- it('inverts the APYs the platform actually quotes', () => {
241
- for (const apy of [0.1, 0.12, 0.14, 0.16, 0.22, 0.3]) {
242
- expect(epochRateToApy(apyToEpochRate(apy))).toBeCloseTo(apy, 12);
243
- }
244
- });
245
-
246
- it('is the same de-compounding netEffectiveApy uses at a zero-ish fee', () => {
247
- // A fee of exactly 0 short-circuits; the smallest fee that does not
248
- // must still agree with the round trip to well under a basis point.
249
- const gross = 0.22;
250
- const net = netEffectiveApy(gross, 0.01);
251
- expect(net).toBeLessThan(gross);
252
- expect(gross - net).toBeLessThan(0.0005);
253
- });
254
- });
@@ -1,96 +0,0 @@
1
- import { getTrancheDisplayName, UPPER_MEZZANINE } from './tranche-display-name';
2
-
3
- // Ported from kasu-ui `src/features/lending/lib/tranche-display-name.test.ts`.
4
-
5
- describe('getTrancheDisplayName', () => {
6
- it('renames Senior to Upper Mezzanine on Apxium pools', () => {
7
- expect(
8
- getTrancheDisplayName('Senior', {
9
- poolName: 'Taxation Funding (Tax Pay)',
10
- }),
11
- ).toBe(UPPER_MEZZANINE);
12
- expect(
13
- getTrancheDisplayName('Senior', {
14
- poolName: 'Professional Fee Funding',
15
- }),
16
- ).toBe(UPPER_MEZZANINE);
17
- expect(
18
- getTrancheDisplayName('Senior', { poolName: 'Whole Ledger Funding' }),
19
- ).toBe(UPPER_MEZZANINE);
20
- });
21
-
22
- it('keeps Senior on non-Apxium (InvoiceMate) pools', () => {
23
- expect(
24
- getTrancheDisplayName('Senior', {
25
- poolName: 'Payment Finance (PayFi) - Clearing Houses',
26
- }),
27
- ).toBe('Senior');
28
- });
29
-
30
- it('never touches other tranche names, on any pool', () => {
31
- expect(
32
- getTrancheDisplayName('Mezzanine', { poolName: 'Taxation Funding' }),
33
- ).toBe('Mezzanine');
34
- expect(
35
- getTrancheDisplayName('Junior', { poolName: 'Taxation Funding' }),
36
- ).toBe('Junior');
37
- expect(
38
- getTrancheDisplayName('Mezzanine', {
39
- poolName: 'Payment Finance (PayFi)',
40
- }),
41
- ).toBe('Mezzanine');
42
- });
43
-
44
- it('matches the Senior name case-insensitively but preserves unknown names verbatim', () => {
45
- expect(
46
- getTrancheDisplayName('SENIOR', { poolName: 'Taxation Funding' }),
47
- ).toBe(UPPER_MEZZANINE);
48
- expect(
49
- getTrancheDisplayName('Senior Plus', { poolName: 'Taxation Funding' }),
50
- ).toBe('Senior Plus');
51
- });
52
-
53
- it('treats missing pool names as Apxium (matches getCreditOriginator default)', () => {
54
- // getCreditOriginator defaults unknown pools to Apxium; the display
55
- // name must follow the same rule or the two surfaces would disagree.
56
- expect(getTrancheDisplayName('Senior', { poolName: '' })).toBe(
57
- UPPER_MEZZANINE,
58
- );
59
- expect(getTrancheDisplayName('Senior', {})).toBe(UPPER_MEZZANINE);
60
- });
61
- });
62
-
63
- describe('getTrancheDisplayName — the widened PoolNameSignal', () => {
64
- it('accepts the facade Strategy shape (`name`)', () => {
65
- expect(getTrancheDisplayName('Senior', { name: 'Taxation Funding' })).toBe(
66
- UPPER_MEZZANINE,
67
- );
68
- expect(
69
- getTrancheDisplayName('Senior', {
70
- name: 'Payment Finance (PayFi)',
71
- }),
72
- ).toBe('Senior');
73
- });
74
-
75
- it('accepts a bare pool name', () => {
76
- expect(getTrancheDisplayName('Senior', 'Whole Ledger Funding')).toBe(
77
- UPPER_MEZZANINE,
78
- );
79
- expect(getTrancheDisplayName('Senior', 'Payment Finance (PayFi)')).toBe(
80
- 'Senior',
81
- );
82
- });
83
-
84
- it('gives the same answer for the raw and facade shapes of one pool', () => {
85
- for (const poolName of [
86
- 'Taxation Funding (Tax Pay)',
87
- 'Payment Finance (PayFi)',
88
- 'Whole Ledger Funding',
89
- '',
90
- ]) {
91
- expect(getTrancheDisplayName('Senior', { poolName })).toBe(
92
- getTrancheDisplayName('Senior', { name: poolName }),
93
- );
94
- }
95
- });
96
- });