@kasufinance/kasu-sdk 2.6.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.
- package/README.md +128 -3
- package/dist/bundle.cjs.js +1119 -254
- package/dist/bundle.esm.js +1109 -255
- package/dist/domain/index.d.ts +3 -2
- package/dist/domain/index.js +2 -2
- package/dist/domain/index.js.map +1 -1
- package/dist/domain/requests.d.ts +42 -4
- package/dist/domain/requests.js +20 -4
- package/dist/domain/requests.js.map +1 -1
- package/dist/domain/wallet-errors.d.ts +63 -8
- package/dist/domain/wallet-errors.js +115 -16
- package/dist/domain/wallet-errors.js.map +1 -1
- package/dist/facade/deposits.js +2 -1
- package/dist/facade/deposits.js.map +1 -1
- package/dist/facade/flows.d.ts +95 -0
- package/dist/facade/flows.js +116 -0
- package/dist/facade/flows.js.map +1 -0
- package/dist/facade/index.d.ts +3 -0
- package/dist/facade/index.js +3 -0
- package/dist/facade/index.js.map +1 -1
- package/dist/facade/kasu.d.ts +6 -2
- package/dist/facade/kasu.js +28 -3
- package/dist/facade/kasu.js.map +1 -1
- package/dist/facade/read-only.d.ts +12 -0
- package/dist/facade/read-only.js +13 -0
- package/dist/facade/read-only.js.map +1 -0
- package/dist/flows/deposit-flow.d.ts +301 -0
- package/dist/flows/deposit-flow.js +358 -0
- package/dist/flows/deposit-flow.js.map +1 -0
- package/dist/flows/flow.d.ts +72 -0
- package/dist/flows/flow.js +110 -0
- package/dist/flows/flow.js.map +1 -0
- package/dist/flows/index.d.ts +19 -0
- package/dist/flows/index.js +17 -0
- package/dist/flows/index.js.map +1 -0
- package/dist/flows/observable.d.ts +59 -0
- package/dist/flows/observable.js +81 -0
- package/dist/flows/observable.js.map +1 -0
- package/dist/flows/withdraw-flow.d.ts +99 -0
- package/dist/flows/withdraw-flow.js +83 -0
- package/dist/flows/withdraw-flow.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/services/DataService/data-service.js +0 -1
- package/dist/services/DataService/data-service.js.map +1 -1
- package/package.json +8 -2
- package/src/domain/index.ts +7 -1
- package/src/domain/requests.ts +57 -4
- package/src/domain/wallet-errors.ts +135 -18
- package/src/facade/deposits.ts +2 -3
- package/src/facade/flows.ts +172 -0
- package/src/facade/index.ts +8 -0
- package/src/facade/kasu.ts +43 -3
- package/src/facade/read-only.ts +13 -0
- package/src/flows/deposit-flow.ts +775 -0
- package/src/flows/flow.ts +108 -0
- package/src/flows/index.ts +45 -0
- package/src/flows/observable.ts +97 -0
- package/src/flows/withdraw-flow.ts +210 -0
- package/src/index.ts +7 -0
- package/src/services/DataService/data-service.ts +0 -1
- package/dist/domain/au-minimum.test.d.ts +0 -1
- package/dist/domain/au-minimum.test.js +0 -202
- package/dist/domain/au-minimum.test.js.map +0 -1
- package/dist/domain/deposit-bounds.test.d.ts +0 -1
- package/dist/domain/deposit-bounds.test.js +0 -135
- package/dist/domain/deposit-bounds.test.js.map +0 -1
- package/dist/domain/loan-contract.test.d.ts +0 -1
- package/dist/domain/loan-contract.test.js +0 -255
- package/dist/domain/loan-contract.test.js.map +0 -1
- package/dist/domain/partners.test.d.ts +0 -1
- package/dist/domain/partners.test.js +0 -53
- package/dist/domain/partners.test.js.map +0 -1
- package/dist/domain/pools.test.d.ts +0 -1
- package/dist/domain/pools.test.js +0 -184
- package/dist/domain/pools.test.js.map +0 -1
- package/dist/domain/rates.test.d.ts +0 -1
- package/dist/domain/rates.test.js +0 -181
- package/dist/domain/rates.test.js.map +0 -1
- package/dist/domain/requests.test.d.ts +0 -1
- package/dist/domain/requests.test.js +0 -470
- package/dist/domain/requests.test.js.map +0 -1
- package/dist/domain/settlement.test.d.ts +0 -1
- package/dist/domain/settlement.test.js +0 -152
- package/dist/domain/settlement.test.js.map +0 -1
- package/dist/domain/tranche-display-name.test.d.ts +0 -1
- package/dist/domain/tranche-display-name.test.js +0 -58
- package/dist/domain/tranche-display-name.test.js.map +0 -1
- package/dist/domain/tranches.test.d.ts +0 -1
- package/dist/domain/tranches.test.js +0 -206
- package/dist/domain/tranches.test.js.map +0 -1
- package/dist/domain/wallet-errors.test.d.ts +0 -1
- package/dist/domain/wallet-errors.test.js +0 -71
- package/dist/domain/wallet-errors.test.js.map +0 -1
- package/dist/facade/config.test.d.ts +0 -1
- package/dist/facade/config.test.js +0 -216
- package/dist/facade/config.test.js.map +0 -1
- package/dist/facade/facade.test.d.ts +0 -1
- package/dist/facade/facade.test.js +0 -278
- package/dist/facade/facade.test.js.map +0 -1
- package/dist/services/Locking/calculate-apy.test.d.ts +0 -1
- package/dist/services/Locking/calculate-apy.test.js +0 -41
- package/dist/services/Locking/calculate-apy.test.js.map +0 -1
- package/dist/tests/sample.test.d.ts +0 -1
- package/dist/tests/sample.test.js +0 -59
- package/dist/tests/sample.test.js.map +0 -1
- package/src/domain/au-minimum.test.ts +0 -371
- package/src/domain/deposit-bounds.test.ts +0 -200
- package/src/domain/loan-contract.test.ts +0 -343
- package/src/domain/partners.test.ts +0 -83
- package/src/domain/pools.test.ts +0 -260
- package/src/domain/rates.test.ts +0 -254
- package/src/domain/requests.test.ts +0 -653
- package/src/domain/settlement.test.ts +0 -198
- package/src/domain/tranche-display-name.test.ts +0 -96
- package/src/domain/tranches.test.ts +0 -300
- package/src/domain/wallet-errors.test.ts +0 -100
- package/src/facade/config.test.ts +0 -265
- package/src/facade/facade.test.ts +0 -374
- package/src/services/Locking/calculate-apy.test.ts +0 -52
- package/src/tests/sample.test.ts +0 -63
package/src/domain/rates.test.ts
DELETED
|
@@ -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
|
-
});
|