@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,300 +0,0 @@
1
- import * as fc from 'fast-check';
2
-
3
- import { PoolOverview, TrancheData } from '../services/DataService/types';
4
-
5
- import { netEffectiveApy } from './rates';
6
- import {
7
- compareTrancheSeniority,
8
- derivePoolStatus,
9
- MIN_TRANCHE_CAPACITY,
10
- netTrancheApyBounds,
11
- pickDefaultTrancheId,
12
- poolAllTranchesFull,
13
- trancheApyBounds,
14
- trancheHasCapacity,
15
- trancheRiskRank,
16
- } from './tranches';
17
-
18
- /**
19
- * Ported from kasu-ui `pick-default-tranche.test.ts` and the NUMERIC half of
20
- * `format-tranche-apy.range.test.ts`. The formatting cases — whole-percent
21
- * rendering, the en-dash shape, the locale table and the "collapse when both
22
- * bounds print the same" rule — stay in kasu-ui: they are decided on rendered
23
- * digits, which this layer never produces.
24
- */
25
-
26
- // `id === name` keeps assertions readable; `poolCapacity` drives
27
- // `trancheHasCapacity` (≥ 1 stable unit → selectable).
28
- function tranche(name: string, poolCapacity: string, apy = '0.12'): TrancheData {
29
- return { id: name, name, apy, poolCapacity } as unknown as TrancheData;
30
- }
31
- function pool(tranches: TrancheData[], enabled = true): PoolOverview {
32
- return { id: 'p', poolName: 'p', enabled, tranches } as unknown as PoolOverview;
33
- }
34
- function rated(minApy: string, maxApy: string): TrancheData {
35
- return { minApy, maxApy } as unknown as TrancheData;
36
- }
37
-
38
- /** The live performance fee on all deployments (subgraph units: 10 = 10%). */
39
- const FEE = 10;
40
-
41
- describe('trancheHasCapacity', () => {
42
- it('accepts a tranche with at least one whole stable unit left', () => {
43
- expect(trancheHasCapacity({ poolCapacity: '5000' })).toBe(true);
44
- expect(
45
- trancheHasCapacity({ poolCapacity: String(MIN_TRANCHE_CAPACITY) }),
46
- ).toBe(true);
47
- });
48
-
49
- it('rejects sub-unit dust, zero and unparseable capacity', () => {
50
- expect(trancheHasCapacity({ poolCapacity: '0.99' })).toBe(false);
51
- expect(trancheHasCapacity({ poolCapacity: '0' })).toBe(false);
52
- expect(
53
- trancheHasCapacity({ poolCapacity: '1.4551915228366852e-11' }),
54
- ).toBe(false);
55
- expect(trancheHasCapacity({ poolCapacity: '' })).toBe(false);
56
- expect(trancheHasCapacity({ poolCapacity: 'not-a-number' })).toBe(false);
57
- });
58
-
59
- it('reads the facade tranche shape (availableCapacity) identically', () => {
60
- // `StrategyTranche` renames the same number; one gate, both shapes.
61
- expect(trancheHasCapacity({ availableCapacity: '5000' })).toBe(true);
62
- expect(trancheHasCapacity({ availableCapacity: '0' })).toBe(false);
63
- });
64
- });
65
-
66
- describe('trancheRiskRank / compareTrancheSeniority', () => {
67
- it('ranks the waterfall safest-first by the RAW name', () => {
68
- expect(trancheRiskRank(tranche('Senior', '1'))).toBe(0);
69
- expect(trancheRiskRank(tranche('Mezzanine', '1'))).toBe(1);
70
- expect(trancheRiskRank(tranche('Junior', '1'))).toBe(2);
71
- });
72
-
73
- it('is case- and whitespace-insensitive, and sorts unknown names last', () => {
74
- expect(trancheRiskRank(tranche(' SENIOR ', '1'))).toBe(0);
75
- expect(trancheRiskRank(tranche('Upper Mezzanine', '1'))).toBe(
76
- Number.POSITIVE_INFINITY,
77
- );
78
- });
79
-
80
- it('sorts Senior → Mezzanine → Junior, keeping ties in input order', () => {
81
- const tranches = [
82
- tranche('Junior', '5000'),
83
- tranche('Tranche B', '5000'),
84
- tranche('Senior', '5000'),
85
- tranche('Tranche A', '5000'),
86
- tranche('Mezzanine', '5000'),
87
- ];
88
- expect(
89
- [...tranches].sort(compareTrancheSeniority).map((t) => t.name),
90
- ).toEqual([
91
- 'Senior',
92
- 'Mezzanine',
93
- 'Junior',
94
- 'Tranche B',
95
- 'Tranche A',
96
- ]);
97
- });
98
- });
99
-
100
- describe('pickDefaultTrancheId', () => {
101
- it('defaults to the lowest-risk tranche (Senior) when all have capacity', () => {
102
- // Subgraph order is Junior → Mezzanine → Senior; the default must NOT
103
- // be the highest-risk Junior that sits first (audit 3.2).
104
- const id = pickDefaultTrancheId(
105
- pool([
106
- tranche('Junior', '5000'),
107
- tranche('Mezzanine', '5000'),
108
- tranche('Senior', '5000'),
109
- ]),
110
- );
111
- expect(id).toBe('Senior');
112
- });
113
-
114
- it('picks the lowest-risk tranche that is still AVAILABLE (skips a full Senior)', () => {
115
- const id = pickDefaultTrancheId(
116
- pool([
117
- tranche('Junior', '5000'),
118
- tranche('Mezzanine', '5000'),
119
- tranche('Senior', '0'),
120
- ]),
121
- );
122
- expect(id).toBe('Mezzanine');
123
- });
124
-
125
- it('does not rank by APY — a Senior priced above Mezzanine is still preferred', () => {
126
- // Real pools can price Senior above Mezzanine; risk is ranked by name,
127
- // not rate.
128
- const id = pickDefaultTrancheId(
129
- pool([
130
- tranche('Mezzanine', '5000', '0.15'),
131
- tranche('Senior', '5000', '0.18'),
132
- ]),
133
- );
134
- expect(id).toBe('Senior');
135
- });
136
-
137
- it('falls back to the lowest-risk tranche when every tranche is full', () => {
138
- const id = pickDefaultTrancheId(
139
- pool([tranche('Junior', '0'), tranche('Senior', '0')]),
140
- );
141
- expect(id).toBe('Senior');
142
- });
143
-
144
- it('falls back to the first available tranche when names are unrecognised', () => {
145
- const id = pickDefaultTrancheId(
146
- pool([tranche('Tranche B', '5000'), tranche('Tranche A', '5000')]),
147
- );
148
- expect(id).toBe('Tranche B');
149
- });
150
-
151
- it('returns an empty string for a pool with no tranches', () => {
152
- expect(pickDefaultTrancheId(pool([]))).toBe('');
153
- });
154
- });
155
-
156
- describe('poolAllTranchesFull', () => {
157
- it('is true when every tranche lacks remaining capacity', () => {
158
- expect(
159
- poolAllTranchesFull(
160
- pool([tranche('Junior', '0'), tranche('Senior', '0')]),
161
- ),
162
- ).toBe(true);
163
- });
164
-
165
- it('is false when at least one tranche still has capacity', () => {
166
- expect(
167
- poolAllTranchesFull(
168
- pool([tranche('Junior', '0'), tranche('Senior', '5000')]),
169
- ),
170
- ).toBe(false);
171
- });
172
-
173
- it('is false for a pool with no tranches (nothing to be full)', () => {
174
- expect(poolAllTranchesFull(pool([]))).toBe(false);
175
- });
176
- });
177
-
178
- describe('derivePoolStatus', () => {
179
- it('is "Coming soon" for a pool that is not yet enabled, full or not', () => {
180
- expect(derivePoolStatus(pool([tranche('Senior', '5000')], false))).toBe(
181
- 'Coming soon',
182
- );
183
- expect(derivePoolStatus(pool([tranche('Senior', '0')], false))).toBe(
184
- 'Coming soon',
185
- );
186
- });
187
-
188
- it('is "Full" for an enabled pool with no capacity anywhere', () => {
189
- expect(derivePoolStatus(pool([tranche('Senior', '0')]))).toBe('Full');
190
- });
191
-
192
- it('is "Live" for an enabled pool with capacity, or with no tranches yet', () => {
193
- expect(derivePoolStatus(pool([tranche('Senior', '5000')]))).toBe('Live');
194
- expect(derivePoolStatus(pool([]))).toBe('Live');
195
- });
196
- });
197
-
198
- describe('trancheApyBounds', () => {
199
- it('spans the lowest minApy and the highest maxApy across tranches', () => {
200
- expect(
201
- trancheApyBounds([rated('0.12', '0.12'), rated('0.185', '0.185')]),
202
- ).toEqual({ min: 0.12, max: 0.185 });
203
- });
204
-
205
- it('carries a single-option strategy as an equal pair', () => {
206
- expect(trancheApyBounds([rated('0.1', '0.1')])).toEqual({
207
- min: 0.1,
208
- max: 0.1,
209
- });
210
- });
211
-
212
- it('skips zero and non-finite rates rather than dragging the range down', () => {
213
- // A tranche with no fixed-term config must not turn a 10.5% range into
214
- // 0–10.5%.
215
- expect(
216
- trancheApyBounds([rated('0', '0'), rated('0.105', '0.105')]),
217
- ).toEqual({ min: 0.105, max: 0.105 });
218
- expect(
219
- trancheApyBounds([rated('not-a-number', 'x'), rated('0.12', '0.14')]),
220
- ).toEqual({ min: 0.12, max: 0.14 });
221
- });
222
-
223
- it('is null when nothing usable remains', () => {
224
- expect(trancheApyBounds([])).toBeNull();
225
- expect(trancheApyBounds([rated('0', '0')])).toBeNull();
226
- expect(trancheApyBounds([rated('-0.1', '-0.1')])).toBeNull();
227
- });
228
-
229
- it('is null when only ONE side is usable — never half a range', () => {
230
- expect(trancheApyBounds([rated('0.12', '0')])).toBeNull();
231
- expect(trancheApyBounds([rated('0', '0.12')])).toBeNull();
232
- });
233
- });
234
-
235
- describe('netTrancheApyBounds', () => {
236
- it('reproduces the spec §2.5 card range: 14–22% gross is 13–20% net', () => {
237
- const bounds = netTrancheApyBounds([rated('0.14', '0.22')], FEE);
238
- expect(bounds).not.toBeNull();
239
- expect(bounds?.min).toBeCloseTo(0.1252, 4);
240
- expect(bounds?.max).toBeCloseTo(0.196, 4);
241
- });
242
-
243
- it('puts each bound through netEffectiveApy and nothing else', () => {
244
- const bounds = netTrancheApyBounds([rated('0.12', '0.185')], FEE);
245
- expect(bounds).toEqual({
246
- min: netEffectiveApy(0.12, FEE),
247
- max: netEffectiveApy(0.185, FEE),
248
- });
249
- });
250
-
251
- it('returns the gross bounds unchanged at a zero fee', () => {
252
- expect(netTrancheApyBounds([rated('0.14', '0.22')], 0)).toEqual({
253
- min: 0.14,
254
- max: 0.22,
255
- });
256
- });
257
-
258
- it('FAILS CLOSED on an out-of-domain fee — never the gross range', () => {
259
- // The raw subgraph integer (1000 = 10%) and a negative fee both land
260
- // here. A caller that does not KNOW the fee must not call at all;
261
- // substituting 0 would overstate the rate with nothing saying so.
262
- expect(netTrancheApyBounds([rated('0.14', '0.22')], 1000)).toBeNull();
263
- expect(netTrancheApyBounds([rated('0.14', '0.22')], -1)).toBeNull();
264
- expect(netTrancheApyBounds([rated('0.14', '0.22')], NaN)).toBeNull();
265
- });
266
-
267
- it('is null when a 100% fee leaves the lender nothing', () => {
268
- expect(netTrancheApyBounds([rated('0.14', '0.22')], 100)).toBeNull();
269
- });
270
-
271
- it('is null whenever the gross range is', () => {
272
- expect(netTrancheApyBounds([], FEE)).toBeNull();
273
- expect(netTrancheApyBounds([rated('0', '0')], FEE)).toBeNull();
274
- });
275
-
276
- it('never inverts the range it was given', () => {
277
- fc.assert(
278
- fc.property(
279
- fc.double({ min: 0.0001, max: 0.99, noNaN: true }),
280
- fc.double({ min: 0.0001, max: 0.99, noNaN: true }),
281
- fc.double({ min: 0, max: 99, noNaN: true }),
282
- (a, b, feePercent) => {
283
- const bounds = netTrancheApyBounds(
284
- [
285
- rated(
286
- String(Math.min(a, b)),
287
- String(Math.max(a, b)),
288
- ),
289
- ],
290
- feePercent,
291
- );
292
- if (!bounds) return;
293
- expect(bounds.min).toBeLessThanOrEqual(bounds.max);
294
- expect(bounds.min).toBeGreaterThan(0);
295
- },
296
- ),
297
- { numRuns: 2000 },
298
- );
299
- });
300
- });
@@ -1,265 +0,0 @@
1
- import { providers, Wallet } from 'ethers';
2
-
3
- import { SdkConfig } from '../sdk-config';
4
-
5
- import { CHAIN_CONFIGS } from './chain-configs';
6
- import { Kasu } from './kasu';
7
- import { ChainConfigEntry } from './types';
8
-
9
- // Pure config assertions — no network, unlike `facade.test.ts`.
10
-
11
- const BASE_CONTRACTS = CHAIN_CONFIGS.base.contracts;
12
-
13
- /** A signer with a provider attached, offline — nothing here sends anything. */
14
- function signer(): Wallet {
15
- return Wallet.createRandom().connect(
16
- new providers.StaticJsonRpcProvider(
17
- CHAIN_CONFIGS.base.rpcUrls[0],
18
- CHAIN_CONFIGS.base.chainId,
19
- ),
20
- );
21
- }
22
-
23
- describe('SdkConfig — UNUSED_LENDING_POOL_IDS normalisation', () => {
24
- it("turns an empty exclusion list into the [''] sentinel", () => {
25
- // `id_not_in: []` matches NOTHING in the subgraph: on Base it returns
26
- // 0 pools where `['']` returns all 9 (verified live 2026-09-04).
27
- const config = new SdkConfig({
28
- subgraphUrl: 'https://example.invalid/subgraph',
29
- contracts: BASE_CONTRACTS,
30
- UNUSED_LENDING_POOL_IDS: [],
31
- });
32
- expect(config.UNUSED_LENDING_POOL_IDS).toEqual(['']);
33
- });
34
-
35
- it('leaves a caller-supplied exclusion list untouched', () => {
36
- const config = new SdkConfig({
37
- subgraphUrl: 'https://example.invalid/subgraph',
38
- contracts: BASE_CONTRACTS,
39
- UNUSED_LENDING_POOL_IDS: ['0xdead'],
40
- });
41
- expect(config.UNUSED_LENDING_POOL_IDS).toEqual(['0xdead']);
42
- });
43
-
44
- it('leaves a sentinel a consumer already passes untouched', () => {
45
- // kasu-ui passes [''], kasu-mobile passes the zero address.
46
- const zeroAddress = '0x0000000000000000000000000000000000000000';
47
- for (const sentinel of [[''], [zeroAddress]]) {
48
- const config = new SdkConfig({
49
- subgraphUrl: 'https://example.invalid/subgraph',
50
- contracts: BASE_CONTRACTS,
51
- UNUSED_LENDING_POOL_IDS: sentinel,
52
- });
53
- expect(config.UNUSED_LENDING_POOL_IDS).toEqual(sentinel);
54
- }
55
- });
56
- });
57
-
58
- describe('CHAIN_CONFIGS — stable asset', () => {
59
- it('gives every chain exactly one stable asset', () => {
60
- for (const [chain, config] of Object.entries(CHAIN_CONFIGS)) {
61
- expect(config.stableAsset.address).toMatch(/^0x[0-9a-fA-F]{40}$/);
62
- expect(config.stableAsset.symbol.length).toBeGreaterThan(0);
63
- expect(config.stableAsset.decimals).toBeGreaterThan(0);
64
- expect(config.stableAsset.currencyCode).toMatch(/^[A-Z]{3}$/);
65
- expect(chain).toBeTruthy();
66
- }
67
- });
68
-
69
- it('carries the live token per deployment', () => {
70
- expect(CHAIN_CONFIGS.base.stableAsset).toEqual({
71
- address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
72
- symbol: 'USDC',
73
- name: 'USD Coin',
74
- decimals: 6,
75
- currencyCode: 'USD',
76
- });
77
- expect(CHAIN_CONFIGS.xdc.stableAsset.symbol).toBe('AUDD');
78
- expect(CHAIN_CONFIGS.xdc.stableAsset.currencyCode).toBe('AUD');
79
- expect(CHAIN_CONFIGS['xdc-usdc'].stableAsset.symbol).toBe('USDC');
80
- expect(CHAIN_CONFIGS.plume.stableAsset.symbol).toBe('pUSD');
81
- });
82
- });
83
-
84
- describe('CHAIN_CONFIGS — rpcUrls', () => {
85
- it('gives every live deployment at least one default endpoint', () => {
86
- for (const chain of ['base', 'xdc', 'xdc-usdc'] as const) {
87
- expect(CHAIN_CONFIGS[chain].rpcUrls.length).toBeGreaterThan(0);
88
- expect(CHAIN_CONFIGS[chain].retired).toBeUndefined();
89
- }
90
- });
91
-
92
- it('never lists an XDC endpoint that fails CORS preflight', () => {
93
- // `rpc.xdc.org` / `erpc.xdc.org` answer OPTIONS without
94
- // `access-control-allow-origin`, so a browser call hangs. House rule.
95
- for (const config of Object.values(CHAIN_CONFIGS)) {
96
- for (const url of config.rpcUrls) {
97
- expect(url).not.toContain('rpc.xdc.org');
98
- expect(url).not.toContain('erpc.xdc.org');
99
- }
100
- }
101
- });
102
-
103
- it('marks the retired deployment and leaves it without a default RPC', () => {
104
- expect(CHAIN_CONFIGS.plume.retired).toBe(true);
105
- expect(CHAIN_CONFIGS.plume.rpcUrls).toEqual([]);
106
- });
107
- });
108
-
109
- describe('Kasu — read-only create and connect', () => {
110
- it('creates a read-only instance with no signerOrProvider', () => {
111
- const kasu = Kasu.create({ chain: 'base' });
112
- expect(kasu.isReadOnly).toBe(true);
113
- expect(kasu.provider).toBeInstanceOf(providers.StaticJsonRpcProvider);
114
- });
115
-
116
- it('uses rpcUrls[0] and the config chain id, with no network detection', () => {
117
- const kasu = Kasu.create({ chain: 'base' });
118
- const provider = kasu.provider as providers.StaticJsonRpcProvider;
119
- expect(provider.connection.url).toBe(CHAIN_CONFIGS.base.rpcUrls[0]);
120
- expect(provider.network.chainId).toBe(CHAIN_CONFIGS.base.chainId);
121
- });
122
-
123
- it('refuses a read-only create on a retired chain with no default RPC', () => {
124
- expect(() => Kasu.create({ chain: 'plume' })).toThrow(
125
- 'Kasu.create: chain "plume" has no default RPC (retired); pass signerOrProvider',
126
- );
127
- });
128
-
129
- it('still accepts an explicit provider for the retired chain', () => {
130
- const provider = new providers.StaticJsonRpcProvider(
131
- 'https://example.invalid/plume',
132
- CHAIN_CONFIGS.plume.chainId,
133
- );
134
- const kasu = Kasu.create({ chain: 'plume', signerOrProvider: provider });
135
- expect(kasu.isReadOnly).toBe(true);
136
- expect(kasu.provider).toBe(provider);
137
- });
138
-
139
- it('is writable when created from a signer', () => {
140
- const kasu = Kasu.create({ chain: 'base', signerOrProvider: signer() });
141
- expect(kasu.isReadOnly).toBe(false);
142
- });
143
-
144
- it('connect returns a NEW writable instance, leaving the original read-only', () => {
145
- const readOnly = Kasu.create({ chain: 'base' });
146
- const connected = readOnly.connect(signer());
147
- expect(connected).not.toBe(readOnly);
148
- expect(connected.isReadOnly).toBe(false);
149
- expect(readOnly.isReadOnly).toBe(true);
150
- });
151
-
152
- it('connect keeps the chain config and the configOverrides', () => {
153
- const readOnly = Kasu.create({
154
- chain: 'base',
155
- configOverrides: { UNUSED_LENDING_POOL_IDS: ['0xhidden'] },
156
- });
157
- const connected = readOnly.connect(signer());
158
- expect(connected.chainConfig).toBe(readOnly.chainConfig);
159
- const configOf = (kasu: Kasu): SdkConfig =>
160
- (kasu.services.DataService as unknown as { _kasuConfig: SdkConfig })
161
- ._kasuConfig;
162
- expect(configOf(connected).UNUSED_LENDING_POOL_IDS).toEqual([
163
- '0xhidden',
164
- ]);
165
- });
166
-
167
- it('exposes the signer’s own provider on a connected instance', () => {
168
- const wallet = signer();
169
- const kasu = Kasu.create({ chain: 'base', signerOrProvider: wallet });
170
- expect(kasu.provider).toBe(wallet.provider);
171
- });
172
-
173
- it('throws rather than returning undefined for a provider-less signer', () => {
174
- const kasu = Kasu.create({
175
- chain: 'base',
176
- signerOrProvider: Wallet.createRandom(),
177
- });
178
- expect(() => kasu.provider).toThrow('has no provider attached');
179
- });
180
- });
181
-
182
- describe('DepositsFacade — read-only writes', () => {
183
- const readOnly = (): Kasu => Kasu.create({ chain: 'base' });
184
- const READ_ONLY_MESSAGE =
185
- 'Kasu: this instance is read-only; call kasu.connect(signer) first';
186
-
187
- it('refuses deposit before touching the contract', async () => {
188
- await expect(
189
- readOnly().deposits.deposit({
190
- poolId: '0xpool',
191
- trancheId: '0xtranche',
192
- amount: 1,
193
- kycSignature: { blockExpiration: 0, signature: '0x' },
194
- }),
195
- ).rejects.toThrow(READ_ONLY_MESSAGE);
196
- });
197
-
198
- it('refuses withdraw', async () => {
199
- await expect(
200
- readOnly().deposits.withdraw({
201
- poolId: '0xpool',
202
- trancheId: '0xtranche',
203
- amount: 1,
204
- }),
205
- ).rejects.toThrow(READ_ONLY_MESSAGE);
206
- });
207
-
208
- it('refuses withdrawMax', async () => {
209
- await expect(
210
- readOnly().deposits.withdrawMax('0xpool', '0xtranche', '0xuser'),
211
- ).rejects.toThrow(READ_ONLY_MESSAGE);
212
- });
213
-
214
- it('lets a connected instance past the guard', () => {
215
- const connected = readOnly().connect(signer());
216
- // Reaches the contract call (and fails there on a fake address) rather
217
- // than being refused up front. Probing the guard directly keeps the
218
- // assertion off the network.
219
- const guard = connected.deposits as unknown as {
220
- assertWritable(): void;
221
- };
222
- expect(() => {
223
- guard.assertWritable();
224
- }).not.toThrow();
225
- });
226
- });
227
-
228
- describe('Kasu.create — a ChainConfigEntry written before 2.5.0', () => {
229
- // kasu-app-admin hand-builds one of these for base-sepolia. TypeScript will
230
- // ask it for `stableAsset` and `rpcUrls` on bump; the runtime must not
231
- // crash first.
232
- const legacy = (): ChainConfigEntry =>
233
- ({
234
- chainId: 84532,
235
- name: 'base-sepolia',
236
- isLiteDeployment: false,
237
- contracts: BASE_CONTRACTS,
238
- subgraphUrl: 'https://example.invalid/subgraph',
239
- // Non-empty: `SdkConfig` documents `directusUrl` as optional, but
240
- // `createDirectus('')` throws `TypeError: Invalid URL`. Pre-existing,
241
- // and unrelated to what this test covers.
242
- directusUrl: 'https://example.invalid/directus/',
243
- unusedPoolIds: [''],
244
- }) as unknown as ChainConfigEntry;
245
-
246
- it('constructs with an explicit provider and falls back to 6 decimals', () => {
247
- const kasu = Kasu.create({
248
- chain: legacy(),
249
- signerOrProvider: new providers.StaticJsonRpcProvider(
250
- 'https://example.invalid/rpc',
251
- 84532,
252
- ),
253
- });
254
- const config = (
255
- kasu.services.DataService as unknown as { _kasuConfig: SdkConfig }
256
- )._kasuConfig;
257
- expect(config.stableAssetDecimals).toBe(6);
258
- });
259
-
260
- it('refuses a read-only create with the same message a retired chain gives', () => {
261
- expect(() => Kasu.create({ chain: legacy() })).toThrow(
262
- 'Kasu.create: chain "base-sepolia" has no default RPC (retired); pass signerOrProvider',
263
- );
264
- });
265
- });