@kasufinance/kasu-sdk 2.4.2 → 2.5.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/LICENSE +21 -0
- package/README.md +189 -253
- package/dist/bundle.cjs.js +4775 -1794
- package/dist/bundle.esm.js +4764 -1816
- package/dist/domain/deposit-bounds.d.ts +62 -0
- package/dist/domain/deposit-bounds.js +84 -0
- package/dist/domain/deposit-bounds.js.map +1 -0
- package/dist/domain/deposit-bounds.test.js +135 -0
- package/dist/domain/deposit-bounds.test.js.map +1 -0
- package/dist/domain/index.d.ts +26 -0
- package/dist/domain/index.js +23 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/domain/partners.d.ts +34 -0
- package/dist/domain/partners.js +43 -0
- package/dist/domain/partners.js.map +1 -0
- package/dist/domain/partners.test.d.ts +1 -0
- package/dist/domain/partners.test.js +53 -0
- package/dist/domain/partners.test.js.map +1 -0
- package/dist/domain/pools.d.ts +70 -0
- package/dist/domain/pools.js +134 -0
- package/dist/domain/pools.js.map +1 -0
- package/dist/domain/pools.test.d.ts +1 -0
- package/dist/domain/pools.test.js +184 -0
- package/dist/domain/pools.test.js.map +1 -0
- package/dist/domain/rates.d.ts +83 -0
- package/dist/domain/rates.js +120 -0
- package/dist/domain/rates.js.map +1 -0
- package/dist/domain/rates.test.d.ts +1 -0
- package/dist/domain/rates.test.js +181 -0
- package/dist/domain/rates.test.js.map +1 -0
- package/dist/domain/tranche-display-name.d.ts +17 -0
- package/dist/domain/tranche-display-name.js +30 -0
- package/dist/domain/tranche-display-name.js.map +1 -0
- package/dist/domain/tranche-display-name.test.d.ts +1 -0
- package/dist/domain/tranche-display-name.test.js +58 -0
- package/dist/domain/tranche-display-name.test.js.map +1 -0
- package/dist/domain/tranches.d.ts +89 -0
- package/dist/domain/tranches.js +150 -0
- package/dist/domain/tranches.js.map +1 -0
- package/dist/domain/tranches.test.d.ts +1 -0
- package/dist/domain/tranches.test.js +206 -0
- package/dist/domain/tranches.test.js.map +1 -0
- package/dist/facade/chain-configs.js +54 -0
- package/dist/facade/chain-configs.js.map +1 -1
- package/dist/facade/config.test.d.ts +1 -0
- package/dist/facade/config.test.js +216 -0
- package/dist/facade/config.test.js.map +1 -0
- package/dist/facade/deposits.d.ts +22 -1
- package/dist/facade/deposits.js +24 -1
- package/dist/facade/deposits.js.map +1 -1
- package/dist/facade/facade.test.d.ts +1 -0
- package/dist/{tests → facade}/facade.test.js +10 -5
- package/dist/facade/facade.test.js.map +1 -0
- package/dist/facade/index.d.ts +2 -1
- package/dist/facade/index.js +2 -0
- package/dist/facade/index.js.map +1 -1
- package/dist/facade/kasu.d.ts +49 -8
- package/dist/facade/kasu.js +117 -13
- package/dist/facade/kasu.js.map +1 -1
- package/dist/facade/strategies.d.ts +25 -0
- package/dist/facade/strategies.js +36 -0
- package/dist/facade/strategies.js.map +1 -1
- package/dist/facade/types.d.ts +57 -2
- package/dist/facade/unused-pool-ids.d.ts +31 -0
- package/dist/facade/unused-pool-ids.js +57 -0
- package/dist/facade/unused-pool-ids.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/sdk-config.d.ts +11 -0
- package/dist/sdk-config.js +8 -1
- package/dist/sdk-config.js.map +1 -1
- package/dist/services/DataService/data-service.d.ts +5 -0
- package/dist/services/DataService/data-service.js +7 -2
- package/dist/services/DataService/data-service.js.map +1 -1
- package/dist/services/Locking/calculate-apy.test.d.ts +1 -0
- package/dist/services/Locking/calculate-apy.test.js +41 -0
- package/dist/services/Locking/calculate-apy.test.js.map +1 -0
- package/dist/services/Locking/locking.d.ts +14 -0
- package/dist/services/Locking/locking.js +16 -2
- package/dist/services/Locking/locking.js.map +1 -1
- package/dist/tests/sample.test.js +46 -25
- package/dist/tests/sample.test.js.map +1 -1
- package/package.json +43 -12
- package/src/domain/deposit-bounds.test.ts +200 -0
- package/src/domain/deposit-bounds.ts +118 -0
- package/src/domain/index.ts +70 -0
- package/src/domain/partners.test.ts +83 -0
- package/src/domain/partners.ts +66 -0
- package/src/domain/pools.test.ts +260 -0
- package/src/domain/pools.ts +150 -0
- package/src/domain/rates.test.ts +254 -0
- package/src/domain/rates.ts +120 -0
- package/src/domain/tranche-display-name.test.ts +96 -0
- package/src/domain/tranche-display-name.ts +34 -0
- package/src/domain/tranches.test.ts +300 -0
- package/src/domain/tranches.ts +177 -0
- package/src/facade/chain-configs.ts +55 -0
- package/src/facade/config.test.ts +265 -0
- package/src/facade/deposits.ts +25 -0
- package/src/{tests → facade}/facade.test.ts +10 -5
- package/src/facade/index.ts +4 -0
- package/src/facade/kasu.ts +155 -12
- package/src/facade/strategies.ts +36 -0
- package/src/facade/types.ts +60 -2
- package/src/facade/unused-pool-ids.ts +59 -0
- package/src/index.ts +6 -0
- package/src/sdk-config.ts +19 -1
- package/src/services/DataService/data-service.ts +7 -2
- package/src/services/Locking/calculate-apy.test.ts +52 -0
- package/src/services/Locking/locking.ts +16 -2
- package/src/tests/sample.test.ts +54 -33
- package/.eslintignore +0 -3
- package/.eslintrc.json +0 -90
- package/.github/workflows/ci.yml +0 -39
- package/.github/workflows/gitleaks.yml +0 -22
- package/.github/workflows/main.yml +0 -41
- package/.gitleaksignore +0 -19
- package/.prettierignore +0 -3
- package/.prettierrc +0 -28
- package/CLAUDE.md +0 -410
- package/copy-abi.ps1 +0 -15
- package/dist/tests/facade.test.js.map +0 -1
- package/index.ts +0 -2
- package/jest-config.ts +0 -9
- package/jest.config.js +0 -8
- package/rollup.config.js +0 -45
- package/tsconfig.json +0 -27
- /package/dist/{tests/facade.test.d.ts → domain/deposit-bounds.test.d.ts} +0 -0
|
@@ -0,0 +1,260 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { PoolOverview, TrancheData } from '../services/DataService/types';
|
|
2
|
+
|
|
3
|
+
import { netEffectiveApy } from './rates';
|
|
4
|
+
import { trancheHasCapacity } from './tranches';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The highest FTD-inclusive upper bound across a pool's tranches, or `0` when
|
|
8
|
+
* none is usable. The sort key behind `selectVisiblePools`.
|
|
9
|
+
*/
|
|
10
|
+
export function poolMaxApy(pool: PoolOverview): number {
|
|
11
|
+
let hi = 0;
|
|
12
|
+
for (const t of pool.tranches) {
|
|
13
|
+
const v = parseFloat(t.maxApy);
|
|
14
|
+
if (Number.isFinite(v) && v > hi) hi = v;
|
|
15
|
+
}
|
|
16
|
+
return hi;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A lending grid's visible pool set: *active* and *not yet oversubscribed*
|
|
21
|
+
* pools, sorted with has-capacity strategies first, then by highest max APY.
|
|
22
|
+
*
|
|
23
|
+
* Shared so a server-rendered payload and a client re-fetch can never
|
|
24
|
+
* disagree — kasu-ui's SSR fetcher and client hook used to carry verbatim
|
|
25
|
+
* copies of this filter + sort, and any drift surfaced as an SSR→CSR content
|
|
26
|
+
* flip on the lending page.
|
|
27
|
+
*
|
|
28
|
+
* Pure: operates on a fresh array (`filter` output), so the in-place `sort`
|
|
29
|
+
* does not mutate the caller's input.
|
|
30
|
+
*/
|
|
31
|
+
export function selectVisiblePools(all: PoolOverview[]): PoolOverview[] {
|
|
32
|
+
const active = all.filter((p) => p.isActive && !p.isOversubscribed);
|
|
33
|
+
return active.sort((a, b) => {
|
|
34
|
+
const aHas = a.tranches.some(trancheHasCapacity) ? 1 : 0;
|
|
35
|
+
const bHas = b.tranches.some(trancheHasCapacity) ? 1 : 0;
|
|
36
|
+
if (aHas !== bHas) return bHas - aHas;
|
|
37
|
+
return poolMaxApy(b) - poolMaxApy(a);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// `tranches` is typed as a required array but arrives from the subgraph, so
|
|
42
|
+
// both scanners below read it as possibly missing — a pool with no tranche
|
|
43
|
+
// list contributes nothing instead of throwing. Mirrors the `?? []` the
|
|
44
|
+
// kasu-ui originals carried.
|
|
45
|
+
function tranchesOf(tranches: TrancheData[] | undefined): TrancheData[] {
|
|
46
|
+
return tranches ?? [];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface BestTranche {
|
|
50
|
+
pool: PoolOverview;
|
|
51
|
+
tranche: TrancheData;
|
|
52
|
+
/** Base variable-rate APY as a decimal fraction (e.g. `0.12`). */
|
|
53
|
+
apy: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The highest-yielding tranche that still has capacity, across all pools.
|
|
58
|
+
*
|
|
59
|
+
* Compares `tranche.apy` — the base variable rate — NOT `minApy`/`maxApy`,
|
|
60
|
+
* which the data service widens with fixed-term-deposit variants. A headline
|
|
61
|
+
* naming one tranche should advertise the rate every depositor gets, not an
|
|
62
|
+
* FTD-locked uplift.
|
|
63
|
+
*
|
|
64
|
+
* Capacity is gated by the shared `trancheHasCapacity` (≥ 1 stable unit), so
|
|
65
|
+
* full tranches never win. Ties on APY break toward more remaining capacity.
|
|
66
|
+
* Returns `null` when no tranche qualifies (empty list, all full, or all rates
|
|
67
|
+
* unset) — callers fall back to static copy.
|
|
68
|
+
*/
|
|
69
|
+
export function pickHighestYieldTranche(
|
|
70
|
+
pools?: PoolOverview[] | null,
|
|
71
|
+
): BestTranche | null {
|
|
72
|
+
if (!pools?.length) return null;
|
|
73
|
+
|
|
74
|
+
let best: BestTranche | null = null;
|
|
75
|
+
|
|
76
|
+
for (const pool of pools) {
|
|
77
|
+
for (const tranche of tranchesOf(pool.tranches)) {
|
|
78
|
+
if (!trancheHasCapacity(tranche)) continue;
|
|
79
|
+
|
|
80
|
+
const apy = parseFloat(tranche.apy);
|
|
81
|
+
if (!Number.isFinite(apy) || apy <= 0) continue;
|
|
82
|
+
|
|
83
|
+
if (!best) {
|
|
84
|
+
best = { pool, tranche, apy };
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const capacity = parseFloat(tranche.poolCapacity);
|
|
89
|
+
const bestCapacity = parseFloat(best.tranche.poolCapacity);
|
|
90
|
+
if (apy > best.apy || (apy === best.apy && capacity > bestCapacity)) {
|
|
91
|
+
best = { pool, tranche, apy };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return best;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The highest NET Effective Interest Rate on offer across the pools passed in
|
|
101
|
+
* — the ceiling behind an "up to …" claim. The numeric core of kasu-ui's
|
|
102
|
+
* `formatMaxNetRate`.
|
|
103
|
+
*
|
|
104
|
+
* `null` when there is nothing to quote, and callers must then DROP the claim
|
|
105
|
+
* rather than print a placeholder: this feeds text that gets posted publicly.
|
|
106
|
+
*
|
|
107
|
+
* Scope follows the caller's pool list, which is normally keyed to one chain —
|
|
108
|
+
* so a lender on XDC AUDD quotes the AUDD ceiling, not Base's. That is the
|
|
109
|
+
* intended reading: they are sharing the deployment they are on.
|
|
110
|
+
*
|
|
111
|
+
* Reads `maxApy` — the FTD-inclusive upper bound — unlike
|
|
112
|
+
* `pickHighestYieldTranche` above, which names one tranche's variable rate. A
|
|
113
|
+
* ceiling claim and a specific tranche's rate are different statements, and
|
|
114
|
+
* the ceiling must agree with the top of `netTrancheApyBounds`, which reads
|
|
115
|
+
* the same field. `apy` is taken into the comparison too: `maxApy` is derived
|
|
116
|
+
* from the base rate plus any fixed-term configs, so it can never be lower,
|
|
117
|
+
* and a tranche that carries no `maxApy` at all still contributes its base
|
|
118
|
+
* rate instead of dropping out.
|
|
119
|
+
*
|
|
120
|
+
* Capacity-gated by `trancheHasCapacity` — we never advertise a rate no lender
|
|
121
|
+
* could take up.
|
|
122
|
+
*
|
|
123
|
+
* FAIL CLOSED: an out-of-domain fee, or a net rate that is non-finite or ≤ 0,
|
|
124
|
+
* returns `null`. The gross figure is never a substitute, and a caller that
|
|
125
|
+
* does not yet know the fee must not pass `0`.
|
|
126
|
+
*
|
|
127
|
+
* @param feePercent the chain's performance fee, 0..100 — never a fraction.
|
|
128
|
+
*/
|
|
129
|
+
export function maxNetRateCeiling(
|
|
130
|
+
pools: PoolOverview[] | undefined | null,
|
|
131
|
+
feePercent: number,
|
|
132
|
+
): number | null {
|
|
133
|
+
if (!pools?.length) return null;
|
|
134
|
+
|
|
135
|
+
let ceiling = 0;
|
|
136
|
+
for (const pool of pools) {
|
|
137
|
+
for (const tranche of tranchesOf(pool.tranches)) {
|
|
138
|
+
if (!trancheHasCapacity(tranche)) continue;
|
|
139
|
+
for (const raw of [tranche.maxApy, tranche.apy]) {
|
|
140
|
+
const apy = parseFloat(raw);
|
|
141
|
+
if (Number.isFinite(apy) && apy > ceiling) ceiling = apy;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (ceiling <= 0) return null;
|
|
146
|
+
|
|
147
|
+
const rate = netEffectiveApy(ceiling, feePercent);
|
|
148
|
+
if (!Number.isFinite(rate) || rate <= 0) return null;
|
|
149
|
+
return rate;
|
|
150
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
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
|
+
});
|