@kasufinance/kasu-sdk 2.4.2 → 2.6.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 +201 -253
- package/dist/bundle.cjs.js +5472 -1710
- package/dist/bundle.esm.js +5415 -1713
- package/dist/domain/au-minimum.d.ts +135 -0
- package/dist/domain/au-minimum.js +154 -0
- package/dist/domain/au-minimum.js.map +1 -0
- package/dist/domain/au-minimum.test.js +202 -0
- package/dist/domain/au-minimum.test.js.map +1 -0
- 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.d.ts +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 +40 -0
- package/dist/domain/index.js +33 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/domain/loan-contract.d.ts +174 -0
- package/dist/domain/loan-contract.js +160 -0
- package/dist/domain/loan-contract.js.map +1 -0
- package/dist/domain/loan-contract.test.d.ts +1 -0
- package/dist/domain/loan-contract.test.js +255 -0
- package/dist/domain/loan-contract.test.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/requests.d.ts +181 -0
- package/dist/domain/requests.js +202 -0
- package/dist/domain/requests.js.map +1 -0
- package/dist/domain/requests.test.d.ts +1 -0
- package/dist/domain/requests.test.js +470 -0
- package/dist/domain/requests.test.js.map +1 -0
- package/dist/domain/settlement.d.ts +97 -0
- package/dist/domain/settlement.js +117 -0
- package/dist/domain/settlement.js.map +1 -0
- package/dist/domain/settlement.test.d.ts +1 -0
- package/dist/domain/settlement.test.js +152 -0
- package/dist/domain/settlement.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/domain/wallet-errors.d.ts +37 -0
- package/dist/domain/wallet-errors.js +56 -0
- package/dist/domain/wallet-errors.js.map +1 -0
- package/dist/domain/wallet-errors.test.d.ts +1 -0
- package/dist/domain/wallet-errors.test.js +71 -0
- package/dist/domain/wallet-errors.test.js.map +1 -0
- package/dist/facade/chain-configs.js +61 -1
- 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 +87 -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/facade/user-portfolio.d.ts +18 -0
- package/dist/facade/user-portfolio.js +23 -0
- package/dist/facade/user-portfolio.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -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 +10 -12
- package/dist/services/DataService/data-service.js.map +1 -1
- package/dist/services/DataService/directus-client.d.ts +26 -0
- package/dist/services/DataService/directus-client.js +38 -0
- package/dist/services/DataService/directus-client.js.map +1 -0
- 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/services/UserLending/user-lending.js +11 -7
- package/dist/services/UserLending/user-lending.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/au-minimum.test.ts +371 -0
- package/src/domain/au-minimum.ts +192 -0
- package/src/domain/deposit-bounds.test.ts +200 -0
- package/src/domain/deposit-bounds.ts +118 -0
- package/src/domain/index.ts +134 -0
- package/src/domain/loan-contract.test.ts +343 -0
- package/src/domain/loan-contract.ts +275 -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/requests.test.ts +653 -0
- package/src/domain/requests.ts +414 -0
- package/src/domain/settlement.test.ts +198 -0
- package/src/domain/settlement.ts +161 -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/domain/wallet-errors.test.ts +100 -0
- package/src/domain/wallet-errors.ts +56 -0
- package/src/facade/chain-configs.ts +62 -1
- package/src/facade/config.test.ts +265 -0
- package/src/facade/deposits.ts +25 -0
- package/src/{tests → facade}/facade.test.ts +134 -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/facade/user-portfolio.ts +24 -0
- package/src/index.ts +8 -0
- package/src/sdk-config.ts +19 -1
- package/src/services/DataService/data-service.ts +14 -26
- package/src/services/DataService/directus-client.ts +54 -0
- package/src/services/Locking/calculate-apy.test.ts +52 -0
- package/src/services/Locking/locking.ts +16 -2
- package/src/services/UserLending/user-lending.ts +17 -21
- 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/au-minimum.test.d.ts} +0 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { maxNetRateCeiling, pickHighestYieldTranche, poolMaxApy, selectVisiblePools, } from './pools';
|
|
2
|
+
import { netEffectiveApy } from './rates';
|
|
3
|
+
/**
|
|
4
|
+
* Ported from kasu-ui `select-visible-pools.test.ts` and the
|
|
5
|
+
* `pickHighestYieldTranche` / `formatMaxNetRate` cases of
|
|
6
|
+
* `pick-best-tranche.test.ts`. The `formatMaxNetRate` assertions are on the
|
|
7
|
+
* ceiling NUMBER here; their rendered strings stay in kasu-ui.
|
|
8
|
+
*/
|
|
9
|
+
function tranche(name, apy, poolCapacity, maxApy) {
|
|
10
|
+
return { id: name, name, apy, poolCapacity, maxApy };
|
|
11
|
+
}
|
|
12
|
+
function pool(over) {
|
|
13
|
+
return Object.assign({ id: 'p', poolName: 'p', isActive: true, isOversubscribed: false, tranches: [tranche('Senior', '0.09', '250000', '0.10')] }, over);
|
|
14
|
+
}
|
|
15
|
+
const withCapacity = () => [
|
|
16
|
+
tranche('Senior', '0.09', '250000', '0.10'),
|
|
17
|
+
];
|
|
18
|
+
const noCapacity = () => [tranche('Senior', '0.09', '0', '0.10')];
|
|
19
|
+
/** The live performance fee on all deployments. */
|
|
20
|
+
const FEE = 10;
|
|
21
|
+
describe('poolMaxApy', () => {
|
|
22
|
+
it('takes the highest usable maxApy across tranches', () => {
|
|
23
|
+
expect(poolMaxApy(pool({
|
|
24
|
+
tranches: [
|
|
25
|
+
tranche('Senior', '0.09', '1', '0.10'),
|
|
26
|
+
tranche('Junior', '0.12', '1', '0.18'),
|
|
27
|
+
],
|
|
28
|
+
}))).toBe(0.18);
|
|
29
|
+
});
|
|
30
|
+
it('is 0 when nothing is usable', () => {
|
|
31
|
+
expect(poolMaxApy(pool({ tranches: [] }))).toBe(0);
|
|
32
|
+
expect(poolMaxApy(pool({ tranches: [tranche('Senior', '0.09', '1', 'x')] }))).toBe(0);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe('selectVisiblePools', () => {
|
|
36
|
+
it('drops inactive pools', () => {
|
|
37
|
+
const pools = [
|
|
38
|
+
pool({ id: 'active', isActive: true }),
|
|
39
|
+
pool({ id: 'inactive', isActive: false }),
|
|
40
|
+
];
|
|
41
|
+
expect(selectVisiblePools(pools).map((p) => p.id)).toEqual(['active']);
|
|
42
|
+
});
|
|
43
|
+
it('drops oversubscribed pools', () => {
|
|
44
|
+
const pools = [
|
|
45
|
+
pool({ id: 'open', isOversubscribed: false }),
|
|
46
|
+
pool({ id: 'full', isOversubscribed: true }),
|
|
47
|
+
];
|
|
48
|
+
expect(selectVisiblePools(pools).map((p) => p.id)).toEqual(['open']);
|
|
49
|
+
});
|
|
50
|
+
it('sorts pools with capacity ahead of pools without', () => {
|
|
51
|
+
const pools = [
|
|
52
|
+
pool({ id: 'empty', tranches: noCapacity() }),
|
|
53
|
+
pool({ id: 'has-cap', tranches: withCapacity() }),
|
|
54
|
+
];
|
|
55
|
+
expect(selectVisiblePools(pools).map((p) => p.id)).toEqual([
|
|
56
|
+
'has-cap',
|
|
57
|
+
'empty',
|
|
58
|
+
]);
|
|
59
|
+
});
|
|
60
|
+
it('sorts by highest max APY within the same capacity bucket', () => {
|
|
61
|
+
const pools = [
|
|
62
|
+
pool({
|
|
63
|
+
id: 'low',
|
|
64
|
+
tranches: [tranche('Senior', '0.08', '250000', '0.08')],
|
|
65
|
+
}),
|
|
66
|
+
pool({
|
|
67
|
+
id: 'high',
|
|
68
|
+
tranches: [tranche('Senior', '0.15', '250000', '0.15')],
|
|
69
|
+
}),
|
|
70
|
+
];
|
|
71
|
+
expect(selectVisiblePools(pools).map((p) => p.id)).toEqual([
|
|
72
|
+
'high',
|
|
73
|
+
'low',
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
it('does not mutate the input array', () => {
|
|
77
|
+
const pools = [
|
|
78
|
+
pool({ id: 'a', tranches: noCapacity() }),
|
|
79
|
+
pool({ id: 'b', tranches: withCapacity() }),
|
|
80
|
+
];
|
|
81
|
+
const before = pools.map((p) => p.id);
|
|
82
|
+
selectVisiblePools(pools);
|
|
83
|
+
expect(pools.map((p) => p.id)).toEqual(before);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
describe('pickHighestYieldTranche', () => {
|
|
87
|
+
const named = (poolName, tranches) => pool({ id: poolName, poolName, tranches });
|
|
88
|
+
it('returns null for empty / nullish input', () => {
|
|
89
|
+
expect(pickHighestYieldTranche(undefined)).toBeNull();
|
|
90
|
+
expect(pickHighestYieldTranche(null)).toBeNull();
|
|
91
|
+
expect(pickHighestYieldTranche([])).toBeNull();
|
|
92
|
+
});
|
|
93
|
+
it('picks the highest base APY across all pools with capacity', () => {
|
|
94
|
+
const best = pickHighestYieldTranche([
|
|
95
|
+
named('A', [tranche('Junior', '0.12', '5000')]),
|
|
96
|
+
named('B', [
|
|
97
|
+
tranche('Senior', '0.18', '5000'),
|
|
98
|
+
tranche('Mezz', '0.15', '5000'),
|
|
99
|
+
]),
|
|
100
|
+
]);
|
|
101
|
+
expect(best === null || best === void 0 ? void 0 : best.pool.poolName).toBe('B');
|
|
102
|
+
expect(best === null || best === void 0 ? void 0 : best.tranche.name).toBe('Senior');
|
|
103
|
+
expect(best === null || best === void 0 ? void 0 : best.apy).toBeCloseTo(0.18);
|
|
104
|
+
});
|
|
105
|
+
it('skips tranches without capacity (< 1 unit)', () => {
|
|
106
|
+
const best = pickHighestYieldTranche([
|
|
107
|
+
named('Full', [tranche('Junior', '0.30', '0')]),
|
|
108
|
+
named('Live', [tranche('Senior', '0.10', '2000')]),
|
|
109
|
+
]);
|
|
110
|
+
expect(best === null || best === void 0 ? void 0 : best.pool.poolName).toBe('Live');
|
|
111
|
+
expect(best === null || best === void 0 ? void 0 : best.apy).toBeCloseTo(0.1);
|
|
112
|
+
});
|
|
113
|
+
it('breaks APY ties toward more remaining capacity', () => {
|
|
114
|
+
const best = pickHighestYieldTranche([
|
|
115
|
+
named('Small', [tranche('Junior', '0.15', '1000')]),
|
|
116
|
+
named('Big', [tranche('Junior', '0.15', '9000')]),
|
|
117
|
+
]);
|
|
118
|
+
expect(best === null || best === void 0 ? void 0 : best.pool.poolName).toBe('Big');
|
|
119
|
+
});
|
|
120
|
+
it('ignores zero / non-finite APY values', () => {
|
|
121
|
+
const best = pickHighestYieldTranche([
|
|
122
|
+
named('Zero', [tranche('Junior', '0', '5000')]),
|
|
123
|
+
named('NaN', [tranche('Junior', 'not-a-number', '5000')]),
|
|
124
|
+
named('Real', [tranche('Junior', '0.09', '5000')]),
|
|
125
|
+
]);
|
|
126
|
+
expect(best === null || best === void 0 ? void 0 : best.pool.poolName).toBe('Real');
|
|
127
|
+
});
|
|
128
|
+
it('reads the BASE rate, not the FTD-widened maxApy', () => {
|
|
129
|
+
// A ceiling claim and a named tranche's rate are different statements.
|
|
130
|
+
const best = pickHighestYieldTranche([
|
|
131
|
+
named('A', [tranche('Junior', '0.12', '5000', '0.30')]),
|
|
132
|
+
named('B', [tranche('Senior', '0.18', '5000', '0.18')]),
|
|
133
|
+
]);
|
|
134
|
+
expect(best === null || best === void 0 ? void 0 : best.pool.poolName).toBe('B');
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
describe('maxNetRateCeiling', () => {
|
|
138
|
+
const named = (poolName, tranches) => pool({ id: poolName, poolName, tranches });
|
|
139
|
+
it('returns the highest ceiling across all pools, net of the fee', () => {
|
|
140
|
+
const rate = maxNetRateCeiling([
|
|
141
|
+
named('A', [tranche('Junior', '0.12', '5000', '0.18')]),
|
|
142
|
+
named('B', [tranche('Senior', '0.10', '5000', '0.22')]),
|
|
143
|
+
], FEE);
|
|
144
|
+
// 0.22 gross → 19.60% net, the same arithmetic as everywhere else.
|
|
145
|
+
expect(rate).toBe(netEffectiveApy(0.22, FEE));
|
|
146
|
+
expect(rate).toBeCloseTo(0.196, 4);
|
|
147
|
+
});
|
|
148
|
+
it('prefers the FTD-inclusive maxApy over the base rate', () => {
|
|
149
|
+
// The strategies card's range tops out at maxApy; an "up to" claim
|
|
150
|
+
// quoting the base rate would contradict it on the same screen.
|
|
151
|
+
expect(maxNetRateCeiling([named('A', [tranche('Junior', '0.10', '5000', '0.22')])], FEE)).toBe(netEffectiveApy(0.22, FEE));
|
|
152
|
+
});
|
|
153
|
+
it('falls back to the base rate when a tranche carries no maxApy', () => {
|
|
154
|
+
expect(maxNetRateCeiling([named('A', [tranche('Junior', '0.22', '5000')])], FEE)).toBe(netEffectiveApy(0.22, FEE));
|
|
155
|
+
});
|
|
156
|
+
it('skips tranches without capacity — an unreachable rate is not a claim', () => {
|
|
157
|
+
const rate = maxNetRateCeiling([
|
|
158
|
+
named('Full', [tranche('Junior', '0.30', '0', '0.30')]),
|
|
159
|
+
named('Live', [tranche('Senior', '0.10', '5000', '0.10')]),
|
|
160
|
+
], FEE);
|
|
161
|
+
expect(rate).toBe(netEffectiveApy(0.1, FEE));
|
|
162
|
+
expect(rate).toBeCloseTo(0.0896, 4);
|
|
163
|
+
});
|
|
164
|
+
it('returns null rather than a gross figure when the fee is out of domain', () => {
|
|
165
|
+
const pools = [named('A', [tranche('Junior', '0.22', '5000', '0.22')])];
|
|
166
|
+
expect(maxNetRateCeiling(pools, 1000)).toBeNull();
|
|
167
|
+
expect(maxNetRateCeiling(pools, -1)).toBeNull();
|
|
168
|
+
expect(maxNetRateCeiling(pools, NaN)).toBeNull();
|
|
169
|
+
// A 100% fee leaves nothing to claim.
|
|
170
|
+
expect(maxNetRateCeiling(pools, 100)).toBeNull();
|
|
171
|
+
});
|
|
172
|
+
it('returns null for empty, nullish or rate-less input', () => {
|
|
173
|
+
expect(maxNetRateCeiling(undefined, FEE)).toBeNull();
|
|
174
|
+
expect(maxNetRateCeiling(null, FEE)).toBeNull();
|
|
175
|
+
expect(maxNetRateCeiling([], FEE)).toBeNull();
|
|
176
|
+
expect(maxNetRateCeiling([named('Zero', [tranche('Junior', '0', '5000', '0')])], FEE)).toBeNull();
|
|
177
|
+
});
|
|
178
|
+
it('agrees with the top of the tranche range it must sit alongside', () => {
|
|
179
|
+
// Both read maxApy through netEffectiveApy; they must not disagree.
|
|
180
|
+
const tranches = [tranche('Senior', '0.14', '5000', '0.22')];
|
|
181
|
+
expect(maxNetRateCeiling([named('A', tranches)], FEE)).toBe(netEffectiveApy(0.22, FEE));
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
//# sourceMappingURL=pools.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pools.test.js","sourceRoot":"","sources":["../../src/domain/pools.test.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,iBAAiB,EACjB,uBAAuB,EACvB,UAAU,EACV,kBAAkB,GACrB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C;;;;;GAKG;AAEH,SAAS,OAAO,CACZ,IAAY,EACZ,GAAW,EACX,YAAoB,EACpB,MAAe;IAEf,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,YAAY,EAAE,MAAM,EAA4B,CAAC;AACnF,CAAC;AAED,SAAS,IAAI,CAAC,IAA2B;IACrC,OAAO,gBACH,EAAE,EAAE,GAAG,EACP,QAAQ,EAAE,GAAG,EACb,QAAQ,EAAE,IAAI,EACd,gBAAgB,EAAE,KAAK,EACvB,QAAQ,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,IACpD,IAAI,CACiB,CAAC;AACjC,CAAC;AAED,MAAM,YAAY,GAAG,GAAkB,EAAE,CAAC;IACtC,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC;CAC9C,CAAC;AACF,MAAM,UAAU,GAAG,GAAkB,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC;AAEjF,mDAAmD;AACn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GAAG,CAAC,CAAC,CAAC,IAAI,CACvD,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,CAC7B,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Epochs in a year — the single source of truth for every rate conversion in
|
|
3
|
+
* the SDK and in every consumer that undoes one.
|
|
4
|
+
*
|
|
5
|
+
* The subgraph carries a per-epoch `interestRate`; `epochRateToApy` compounds
|
|
6
|
+
* it into an APY and `apyToEpochRate` inverts exactly that. A second copy of
|
|
7
|
+
* this number anywhere would recover a different weekly rate and every derived
|
|
8
|
+
* figure — displayed APYs, net rates, projected interest — would be wrong, so
|
|
9
|
+
* `DataService.calculateApyForTranche` and `KSULocking.calculateApy` both read
|
|
10
|
+
* it from here rather than declaring their own.
|
|
11
|
+
*/
|
|
12
|
+
export declare const EPOCHS_IN_YEAR = 52.17857;
|
|
13
|
+
/**
|
|
14
|
+
* A per-epoch interest rate compounded into an annual rate.
|
|
15
|
+
*
|
|
16
|
+
* ```
|
|
17
|
+
* apy = (1 + r)^E − 1
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* This is the exact expression `DataService.calculateApyForTranche` has always
|
|
21
|
+
* used, kept in its literal `**` form rather than rewritten through
|
|
22
|
+
* `expm1`/`log1p`: it is the SDK's OWN definition of `tranche.apy`, so its
|
|
23
|
+
* float behaviour is part of the contract every consumer already calibrated
|
|
24
|
+
* against. `apyToEpochRate` is the one that must be numerically careful,
|
|
25
|
+
* because it runs on the result.
|
|
26
|
+
*
|
|
27
|
+
* @param epochRate the per-epoch rate as a 0..1 fraction (e.g. `0.003`).
|
|
28
|
+
* @returns the compounded annual rate as a 0..1 fraction.
|
|
29
|
+
*/
|
|
30
|
+
export declare function epochRateToApy(epochRate: number): number;
|
|
31
|
+
/**
|
|
32
|
+
* The inverse of `epochRateToApy` — the per-epoch rate an APY was compounded
|
|
33
|
+
* from.
|
|
34
|
+
*
|
|
35
|
+
* `expm1`/`log1p` rather than the literal `(1 + apy) ** (1 / E) - 1`.
|
|
36
|
+
* Algebraically identical, but the direct form computes a double just above 1
|
|
37
|
+
* and subtracts 1 from it, discarding the low bits of a result that is itself
|
|
38
|
+
* ~1e-3 for a realistic rate. These two never form the intermediate near-1
|
|
39
|
+
* value.
|
|
40
|
+
*
|
|
41
|
+
* @param apy the compounded annual rate as a 0..1 fraction.
|
|
42
|
+
* @returns the per-epoch rate as a 0..1 fraction.
|
|
43
|
+
*/
|
|
44
|
+
export declare function apyToEpochRate(apy: number): number;
|
|
45
|
+
/**
|
|
46
|
+
* The NET Effective Interest Rate — the compounded annual rate a lender
|
|
47
|
+
* actually earns, after the platform performance fee.
|
|
48
|
+
*
|
|
49
|
+
* ```
|
|
50
|
+
* r = (1 + grossApy)^(1/E) − 1 // the gross per-epoch rate
|
|
51
|
+
* net = (1 + r · (1 − feePercent/100))^E − 1 // fee taken each epoch, then compounded
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* `LendingPool._applyTrancheInterest` mints the lender
|
|
55
|
+
* `interest × (1 − performanceFee)` at every epoch close, and it is that NET
|
|
56
|
+
* amount which capitalises and earns interest in the following epoch. So the
|
|
57
|
+
* fee is applied per epoch and the result is re-compounded — not deducted from
|
|
58
|
+
* the annual figure.
|
|
59
|
+
*
|
|
60
|
+
* ⚠️ UNITS — the one thing that must not be got wrong.
|
|
61
|
+
* `feePercent` is a PERCENTAGE IN 0..100, **not** a 0..1 fraction.
|
|
62
|
+
* `DataService.getPerformanceFee()` (and `StrategiesFacade.getPerformanceFeePercent()`)
|
|
63
|
+
* returns the subgraph's `integerToPercentage2(1000)` = `10`, meaning **ten
|
|
64
|
+
* percent**. It is NOT `0.10`. Treating it as a fraction computes
|
|
65
|
+
* `r · (1 − 10)` = `−9r` and yields a nonsense negative rate that would render
|
|
66
|
+
* as a plausible-looking `-…%`, which is why `feePercent > 100` and
|
|
67
|
+
* `feePercent < 0` both return `NaN` and why the parameter is named
|
|
68
|
+
* `feePercent` and never `fee`.
|
|
69
|
+
*
|
|
70
|
+
* This returns a NUMBER and nothing else. Rendering it — the 2dp convention,
|
|
71
|
+
* the ` p.a.` suffix, and the fail-closed em-dash for a `NaN` or non-positive
|
|
72
|
+
* result — is the consumer's job: kasu-ui does it in
|
|
73
|
+
* `src/features/lending/lib/interest-rate.ts` (`formatEffectiveRate`,
|
|
74
|
+
* `RATE_UNAVAILABLE`), which now wraps this function instead of restating it.
|
|
75
|
+
*
|
|
76
|
+
* @param grossApy compounded GROSS APY as a 0..1 fraction (e.g. `0.22` for
|
|
77
|
+
* 22%), exactly as the SDK carries it on `tranche.apy` / `tranche.maxApy`.
|
|
78
|
+
* @param feePercent the chain's performance fee as a percentage, `0..100`
|
|
79
|
+
* (e.g. `10` for the live 10% fee).
|
|
80
|
+
* @returns the net compounded annual rate as a 0..1 fraction, or `NaN` when
|
|
81
|
+
* either input is outside its domain. Callers render `NaN` as "—".
|
|
82
|
+
*/
|
|
83
|
+
export declare function netEffectiveApy(grossApy: number, feePercent: number): number;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Epochs in a year — the single source of truth for every rate conversion in
|
|
3
|
+
* the SDK and in every consumer that undoes one.
|
|
4
|
+
*
|
|
5
|
+
* The subgraph carries a per-epoch `interestRate`; `epochRateToApy` compounds
|
|
6
|
+
* it into an APY and `apyToEpochRate` inverts exactly that. A second copy of
|
|
7
|
+
* this number anywhere would recover a different weekly rate and every derived
|
|
8
|
+
* figure — displayed APYs, net rates, projected interest — would be wrong, so
|
|
9
|
+
* `DataService.calculateApyForTranche` and `KSULocking.calculateApy` both read
|
|
10
|
+
* it from here rather than declaring their own.
|
|
11
|
+
*/
|
|
12
|
+
export const EPOCHS_IN_YEAR = 52.17857;
|
|
13
|
+
/**
|
|
14
|
+
* A per-epoch interest rate compounded into an annual rate.
|
|
15
|
+
*
|
|
16
|
+
* ```
|
|
17
|
+
* apy = (1 + r)^E − 1
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* This is the exact expression `DataService.calculateApyForTranche` has always
|
|
21
|
+
* used, kept in its literal `**` form rather than rewritten through
|
|
22
|
+
* `expm1`/`log1p`: it is the SDK's OWN definition of `tranche.apy`, so its
|
|
23
|
+
* float behaviour is part of the contract every consumer already calibrated
|
|
24
|
+
* against. `apyToEpochRate` is the one that must be numerically careful,
|
|
25
|
+
* because it runs on the result.
|
|
26
|
+
*
|
|
27
|
+
* @param epochRate the per-epoch rate as a 0..1 fraction (e.g. `0.003`).
|
|
28
|
+
* @returns the compounded annual rate as a 0..1 fraction.
|
|
29
|
+
*/
|
|
30
|
+
export function epochRateToApy(epochRate) {
|
|
31
|
+
return Math.pow((1 + epochRate), EPOCHS_IN_YEAR) - 1;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The inverse of `epochRateToApy` — the per-epoch rate an APY was compounded
|
|
35
|
+
* from.
|
|
36
|
+
*
|
|
37
|
+
* `expm1`/`log1p` rather than the literal `(1 + apy) ** (1 / E) - 1`.
|
|
38
|
+
* Algebraically identical, but the direct form computes a double just above 1
|
|
39
|
+
* and subtracts 1 from it, discarding the low bits of a result that is itself
|
|
40
|
+
* ~1e-3 for a realistic rate. These two never form the intermediate near-1
|
|
41
|
+
* value.
|
|
42
|
+
*
|
|
43
|
+
* @param apy the compounded annual rate as a 0..1 fraction.
|
|
44
|
+
* @returns the per-epoch rate as a 0..1 fraction.
|
|
45
|
+
*/
|
|
46
|
+
export function apyToEpochRate(apy) {
|
|
47
|
+
return Math.expm1(Math.log1p(apy) / EPOCHS_IN_YEAR);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The NET Effective Interest Rate — the compounded annual rate a lender
|
|
51
|
+
* actually earns, after the platform performance fee.
|
|
52
|
+
*
|
|
53
|
+
* ```
|
|
54
|
+
* r = (1 + grossApy)^(1/E) − 1 // the gross per-epoch rate
|
|
55
|
+
* net = (1 + r · (1 − feePercent/100))^E − 1 // fee taken each epoch, then compounded
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* `LendingPool._applyTrancheInterest` mints the lender
|
|
59
|
+
* `interest × (1 − performanceFee)` at every epoch close, and it is that NET
|
|
60
|
+
* amount which capitalises and earns interest in the following epoch. So the
|
|
61
|
+
* fee is applied per epoch and the result is re-compounded — not deducted from
|
|
62
|
+
* the annual figure.
|
|
63
|
+
*
|
|
64
|
+
* ⚠️ UNITS — the one thing that must not be got wrong.
|
|
65
|
+
* `feePercent` is a PERCENTAGE IN 0..100, **not** a 0..1 fraction.
|
|
66
|
+
* `DataService.getPerformanceFee()` (and `StrategiesFacade.getPerformanceFeePercent()`)
|
|
67
|
+
* returns the subgraph's `integerToPercentage2(1000)` = `10`, meaning **ten
|
|
68
|
+
* percent**. It is NOT `0.10`. Treating it as a fraction computes
|
|
69
|
+
* `r · (1 − 10)` = `−9r` and yields a nonsense negative rate that would render
|
|
70
|
+
* as a plausible-looking `-…%`, which is why `feePercent > 100` and
|
|
71
|
+
* `feePercent < 0` both return `NaN` and why the parameter is named
|
|
72
|
+
* `feePercent` and never `fee`.
|
|
73
|
+
*
|
|
74
|
+
* This returns a NUMBER and nothing else. Rendering it — the 2dp convention,
|
|
75
|
+
* the ` p.a.` suffix, and the fail-closed em-dash for a `NaN` or non-positive
|
|
76
|
+
* result — is the consumer's job: kasu-ui does it in
|
|
77
|
+
* `src/features/lending/lib/interest-rate.ts` (`formatEffectiveRate`,
|
|
78
|
+
* `RATE_UNAVAILABLE`), which now wraps this function instead of restating it.
|
|
79
|
+
*
|
|
80
|
+
* @param grossApy compounded GROSS APY as a 0..1 fraction (e.g. `0.22` for
|
|
81
|
+
* 22%), exactly as the SDK carries it on `tranche.apy` / `tranche.maxApy`.
|
|
82
|
+
* @param feePercent the chain's performance fee as a percentage, `0..100`
|
|
83
|
+
* (e.g. `10` for the live 10% fee).
|
|
84
|
+
* @returns the net compounded annual rate as a 0..1 fraction, or `NaN` when
|
|
85
|
+
* either input is outside its domain. Callers render `NaN` as "—".
|
|
86
|
+
*/
|
|
87
|
+
export function netEffectiveApy(grossApy, feePercent) {
|
|
88
|
+
if (!Number.isFinite(grossApy) || grossApy < 0)
|
|
89
|
+
return NaN;
|
|
90
|
+
if (!Number.isFinite(feePercent) || feePercent < 0 || feePercent > 100) {
|
|
91
|
+
return NaN;
|
|
92
|
+
}
|
|
93
|
+
// The lender's share of each epoch's interest. Short-circuiting on `=== 1`
|
|
94
|
+
// (rather than on `feePercent === 0`) also covers a fee small enough to
|
|
95
|
+
// underflow the multiplier: if it cannot change the epoch rate, the net
|
|
96
|
+
// rate IS the gross rate, and it must be returned unchanged rather than
|
|
97
|
+
// round-tripped through the de-compound/re-compound pair below — which is
|
|
98
|
+
// exact in algebra but not in floating point (0.22 comes back as
|
|
99
|
+
// 0.22000000000000663 in the naive form). If the fee is ever set to zero
|
|
100
|
+
// on-chain, the displayed rate must equal the SDK's APY exactly.
|
|
101
|
+
const lenderShare = 1 - feePercent / 100;
|
|
102
|
+
if (lenderShare === 1)
|
|
103
|
+
return grossApy;
|
|
104
|
+
// `expm1`/`log1p` rather than the literal `(1 + x) ** n - 1`. Algebraically
|
|
105
|
+
// identical, but the direct form computes `(1 + grossApy) ** (1 / E) - 1`
|
|
106
|
+
// by subtracting 1 from a double just above 1, discarding the low bits of a
|
|
107
|
+
// result that is itself ~1e-8 for a realistic rate. The ~1e-8 relative
|
|
108
|
+
// error that survives is enough to push the net rate ABOVE the gross one;
|
|
109
|
+
// these two never form the intermediate near-1 value.
|
|
110
|
+
const grossEpochRate = apyToEpochRate(grossApy);
|
|
111
|
+
const netEpochRate = grossEpochRate * lenderShare;
|
|
112
|
+
const net = Math.expm1(EPOCHS_IN_YEAR * Math.log1p(netEpochRate));
|
|
113
|
+
// `net ≤ grossApy` for every `lenderShare ≤ 1` is a theorem, but at the
|
|
114
|
+
// last ULP it is not a float identity — a fee of 1e-14 percent leaves a
|
|
115
|
+
// `lenderShare` one ULP below 1 and the round trip can land one ULP above
|
|
116
|
+
// `grossApy`. Clamping enforces the proven bound rather than letting a
|
|
117
|
+
// rounding artefact put a net rate above the gross rate it came from.
|
|
118
|
+
return Math.min(net, grossApy);
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=rates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rates.js","sourceRoot":"","sources":["../../src/domain/rates.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,QAAQ,CAAC;AAEvC;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,cAAc,CAAC,SAAiB;IAC5C,OAAO,SAAA,CAAC,CAAC,GAAG,SAAS,CAAC,EAAI,cAAc,CAAA,GAAG,CAAC,CAAC;AACjD,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,cAAc,CAAC,GAAW;IACtC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,cAAc,CAAC,CAAC;AACxD,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,UAAU,eAAe,CAAC,QAAgB,EAAE,UAAkB;IAChE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,QAAQ,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC;IAC3D,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,UAAU,GAAG,CAAC,IAAI,UAAU,GAAG,GAAG,EAAE,CAAC;QACrE,OAAO,GAAG,CAAC;IACf,CAAC;IACD,2EAA2E;IAC3E,wEAAwE;IACxE,wEAAwE;IACxE,wEAAwE;IACxE,0EAA0E;IAC1E,iEAAiE;IACjE,yEAAyE;IACzE,iEAAiE;IACjE,MAAM,WAAW,GAAG,CAAC,GAAG,UAAU,GAAG,GAAG,CAAC;IACzC,IAAI,WAAW,KAAK,CAAC;QAAE,OAAO,QAAQ,CAAC;IACvC,4EAA4E;IAC5E,0EAA0E;IAC1E,4EAA4E;IAC5E,uEAAuE;IACvE,0EAA0E;IAC1E,sDAAsD;IACtD,MAAM,cAAc,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAC;IAChD,MAAM,YAAY,GAAG,cAAc,GAAG,WAAW,CAAC;IAClD,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;IAClE,wEAAwE;IACxE,wEAAwE;IACxE,0EAA0E;IAC1E,uEAAuE;IACvE,sEAAsE;IACtE,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import * as fc from 'fast-check';
|
|
2
|
+
import { apyToEpochRate, epochRateToApy, EPOCHS_IN_YEAR, netEffectiveApy, } from './rates';
|
|
3
|
+
/**
|
|
4
|
+
* Ported from kasu-ui `src/features/lending/lib/interest-rate.test.ts`. The
|
|
5
|
+
* `formatEffectiveRate` cases stay there — they assert copy and locale, which
|
|
6
|
+
* this layer does not own.
|
|
7
|
+
*/
|
|
8
|
+
/** The live performance fee on all deployments (subgraph units: 10 = ten percent). */
|
|
9
|
+
const LIVE_FEE_PERCENT = 10;
|
|
10
|
+
/**
|
|
11
|
+
* Spec §9 — every distinct per-epoch rate configured on Base, read on-chain
|
|
12
|
+
* 2026-09-01, with the NET Effective Interest Rate each must display at the
|
|
13
|
+
* live 10% fee. This table IS the review sanity check: a correct
|
|
14
|
+
* implementation reproduces the whole column, including the 30% crossover row
|
|
15
|
+
* (26.30% gross nominal → 26.64% net APY) which moves UP while every other row
|
|
16
|
+
* moves down, because the two errors in the old display — no fee deducted, and
|
|
17
|
+
* a spurious de-compounding — pull in opposite directions and cross over near
|
|
18
|
+
* 28%.
|
|
19
|
+
*/
|
|
20
|
+
const GROUND_TRUTH = [
|
|
21
|
+
{ grossApy: 0.1, netPercent: 8.96 },
|
|
22
|
+
{ grossApy: 0.105, netPercent: 9.4 },
|
|
23
|
+
{ grossApy: 0.12, netPercent: 10.74 },
|
|
24
|
+
{ grossApy: 0.14, netPercent: 12.52 },
|
|
25
|
+
{ grossApy: 0.15, netPercent: 13.41 },
|
|
26
|
+
{ grossApy: 0.16, netPercent: 14.29 },
|
|
27
|
+
{ grossApy: 0.2, netPercent: 17.84 },
|
|
28
|
+
{ grossApy: 0.22, netPercent: 19.6 },
|
|
29
|
+
{ grossApy: 0.3, netPercent: 26.64 },
|
|
30
|
+
];
|
|
31
|
+
describe('netEffectiveApy — spec §9 ground truth', () => {
|
|
32
|
+
it.each(GROUND_TRUTH)('gross $grossApy at a 10% fee is $netPercent% net', ({ grossApy, netPercent }) => {
|
|
33
|
+
expect(netEffectiveApy(grossApy, LIVE_FEE_PERCENT) * 100).toBeCloseTo(netPercent, 2);
|
|
34
|
+
});
|
|
35
|
+
it('reproduces the 30% crossover — the net APY is ABOVE the old gross nominal figure', () => {
|
|
36
|
+
// Old display: 52.17857 · ((1.30)^(1/52.17857) − 1) = 26.30%. New: 26.64%.
|
|
37
|
+
const oldGrossNominal = EPOCHS_IN_YEAR * (Math.pow(1.3, (1 / EPOCHS_IN_YEAR)) - 1);
|
|
38
|
+
expect(oldGrossNominal * 100).toBeCloseTo(26.3, 1);
|
|
39
|
+
expect(netEffectiveApy(0.3, LIVE_FEE_PERCENT)).toBeGreaterThan(oldGrossNominal);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
describe('netEffectiveApy — units (spec §2.3)', () => {
|
|
43
|
+
it('reads feePercent as a PERCENTAGE: 22% gross at fee 10 is ≈19.602%', () => {
|
|
44
|
+
expect(netEffectiveApy(0.22, 10)).toBeCloseTo(0.19602, 5);
|
|
45
|
+
});
|
|
46
|
+
it('does not silently accept the 0..1 fraction — 0.1 is a 0.1% fee, not 10%', () => {
|
|
47
|
+
const wrongUnit = netEffectiveApy(0.22, 0.1);
|
|
48
|
+
const correct = netEffectiveApy(0.22, 10);
|
|
49
|
+
// It is a finite, in-range number (we cannot reject it — 0.1% is a
|
|
50
|
+
// legal fee), so the guard cannot catch this. What protects us is that
|
|
51
|
+
// it is MATERIALLY different: ~2.37pp too high, not a plausible
|
|
52
|
+
// near-miss that could sit on screen unnoticed.
|
|
53
|
+
expect(wrongUnit).toBeCloseTo(0.21976, 5);
|
|
54
|
+
expect(wrongUnit - correct).toBeGreaterThan(0.02);
|
|
55
|
+
});
|
|
56
|
+
it('rejects the RAW subgraph integer (1000 = 10%, pre-integerToPercentage2)', () => {
|
|
57
|
+
expect(netEffectiveApy(0.22, 1000)).toBeNaN();
|
|
58
|
+
});
|
|
59
|
+
it('rejects a fee outside 0..100', () => {
|
|
60
|
+
expect(netEffectiveApy(0.22, 101)).toBeNaN();
|
|
61
|
+
expect(netEffectiveApy(0.22, -1)).toBeNaN();
|
|
62
|
+
expect(netEffectiveApy(0.22, NaN)).toBeNaN();
|
|
63
|
+
expect(netEffectiveApy(0.22, Infinity)).toBeNaN();
|
|
64
|
+
});
|
|
65
|
+
it('accepts the domain edges', () => {
|
|
66
|
+
expect(netEffectiveApy(0.22, 0)).toBe(0.22);
|
|
67
|
+
// A 100% fee leaves the lender nothing — 0, not NaN: the fee is
|
|
68
|
+
// knowable and the answer is "you earn zero", which consumers render
|
|
69
|
+
// as "—".
|
|
70
|
+
expect(netEffectiveApy(0.22, 100)).toBe(0);
|
|
71
|
+
});
|
|
72
|
+
it('returns NaN for a non-finite or negative gross APY (callers render "—")', () => {
|
|
73
|
+
expect(netEffectiveApy(NaN, 10)).toBeNaN();
|
|
74
|
+
expect(netEffectiveApy(Infinity, 10)).toBeNaN();
|
|
75
|
+
expect(netEffectiveApy(-Infinity, 10)).toBeNaN();
|
|
76
|
+
expect(netEffectiveApy(-0.01, 10)).toBeNaN();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
// The FULL in-domain range, down to and including zero and the denormals. The
|
|
80
|
+
// invariants below are the reason `netEffectiveApy` is written with
|
|
81
|
+
// `log1p`/`expm1`: the direct `(1 + x) ** n - 1` form breaks the `net <= gross`
|
|
82
|
+
// invariant for tiny rates, and the property runner finds that in a few hundred
|
|
83
|
+
// cases. Narrowing this arb would hide it.
|
|
84
|
+
const grossApyArb = fc.double({
|
|
85
|
+
min: 0,
|
|
86
|
+
max: 5,
|
|
87
|
+
noNaN: true,
|
|
88
|
+
noDefaultInfinity: true,
|
|
89
|
+
});
|
|
90
|
+
const feePercentArb = fc.double({
|
|
91
|
+
min: 0,
|
|
92
|
+
max: 100,
|
|
93
|
+
noNaN: true,
|
|
94
|
+
noDefaultInfinity: true,
|
|
95
|
+
});
|
|
96
|
+
/**
|
|
97
|
+
* A fee as the subgraph can actually express one: `integerToPercentage2`
|
|
98
|
+
* divides a uint by 100, so every reachable value is a 2dp multiple of 0.01 in
|
|
99
|
+
* 0..100. Used only for the STRICT-decrease property, which cannot hold for a
|
|
100
|
+
* fee of 1e-300 percent — `1 − feePercent/100` rounds to exactly 1 and the net
|
|
101
|
+
* rate is the gross one. That is arithmetic, not a defect, and no such fee
|
|
102
|
+
* exists.
|
|
103
|
+
*/
|
|
104
|
+
const realisticFeePercentArb = fc
|
|
105
|
+
.integer({ min: 1, max: 10000 })
|
|
106
|
+
.map((n) => n / 100);
|
|
107
|
+
describe('netEffectiveApy — invariants', () => {
|
|
108
|
+
it('never returns more than the gross APY it came from', () => {
|
|
109
|
+
fc.assert(fc.property(grossApyArb, feePercentArb, (grossApy, feePercent) => {
|
|
110
|
+
expect(netEffectiveApy(grossApy, feePercent)).toBeLessThanOrEqual(grossApy);
|
|
111
|
+
}));
|
|
112
|
+
});
|
|
113
|
+
it('equals the gross APY EXACTLY when the fee is zero', () => {
|
|
114
|
+
fc.assert(
|
|
115
|
+
// Exact equality, not "close to": if the fee is ever set to zero
|
|
116
|
+
// on-chain, the rate must be the SDK's own APY, with no float drift
|
|
117
|
+
// from the de-compound/re-compound round trip.
|
|
118
|
+
fc.property(grossApyArb, (grossApy) => {
|
|
119
|
+
expect(netEffectiveApy(grossApy, 0)).toBe(grossApy);
|
|
120
|
+
}));
|
|
121
|
+
});
|
|
122
|
+
it('is STRICTLY below the gross APY for any real (2dp) non-zero fee', () => {
|
|
123
|
+
fc.assert(fc.property(grossApyArb, realisticFeePercentArb, (grossApy, feePercent) => {
|
|
124
|
+
if (grossApy === 0)
|
|
125
|
+
return;
|
|
126
|
+
expect(netEffectiveApy(grossApy, feePercent)).toBeLessThan(grossApy);
|
|
127
|
+
}));
|
|
128
|
+
});
|
|
129
|
+
it('decreases monotonically as the fee rises', () => {
|
|
130
|
+
fc.assert(fc.property(grossApyArb, feePercentArb, feePercentArb, (grossApy, a, b) => {
|
|
131
|
+
const [lo, hi] = a <= b ? [a, b] : [b, a];
|
|
132
|
+
expect(netEffectiveApy(grossApy, hi)).toBeLessThanOrEqual(netEffectiveApy(grossApy, lo));
|
|
133
|
+
}));
|
|
134
|
+
});
|
|
135
|
+
it('increases monotonically as the gross APY rises', () => {
|
|
136
|
+
fc.assert(fc.property(grossApyArb, grossApyArb, feePercentArb, (a, b, feePercent) => {
|
|
137
|
+
const [lo, hi] = a <= b ? [a, b] : [b, a];
|
|
138
|
+
expect(netEffectiveApy(hi, feePercent)).toBeGreaterThanOrEqual(netEffectiveApy(lo, feePercent));
|
|
139
|
+
}));
|
|
140
|
+
});
|
|
141
|
+
it('is never negative for any in-domain input', () => {
|
|
142
|
+
fc.assert(fc.property(grossApyArb, feePercentArb, (grossApy, feePercent) => {
|
|
143
|
+
expect(netEffectiveApy(grossApy, feePercent)).toBeGreaterThanOrEqual(0);
|
|
144
|
+
}));
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
describe('epochRateToApy / apyToEpochRate', () => {
|
|
148
|
+
it('compounds a per-epoch rate exactly as the data service always has', () => {
|
|
149
|
+
// The literal expression `calculateApyForTranche` carried inline.
|
|
150
|
+
for (const r of [0, 0.001, 0.0025, 0.003, 0.005, 0.01]) {
|
|
151
|
+
expect(epochRateToApy(r)).toBe(Math.pow((1 + r), EPOCHS_IN_YEAR) - 1);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
it('round-trips within 1e-12 for realistic per-epoch rates', () => {
|
|
155
|
+
fc.assert(fc.property(
|
|
156
|
+
// 0.0001..0.02 per epoch ≈ 0.5%..190% APY — wider than any
|
|
157
|
+
// rate the platform has configured.
|
|
158
|
+
fc.double({
|
|
159
|
+
min: 0.0001,
|
|
160
|
+
max: 0.02,
|
|
161
|
+
noNaN: true,
|
|
162
|
+
noDefaultInfinity: true,
|
|
163
|
+
}), (r) => {
|
|
164
|
+
expect(apyToEpochRate(epochRateToApy(r))).toBeCloseTo(r, 12);
|
|
165
|
+
}));
|
|
166
|
+
});
|
|
167
|
+
it('inverts the APYs the platform actually quotes', () => {
|
|
168
|
+
for (const apy of [0.1, 0.12, 0.14, 0.16, 0.22, 0.3]) {
|
|
169
|
+
expect(epochRateToApy(apyToEpochRate(apy))).toBeCloseTo(apy, 12);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
it('is the same de-compounding netEffectiveApy uses at a zero-ish fee', () => {
|
|
173
|
+
// A fee of exactly 0 short-circuits; the smallest fee that does not
|
|
174
|
+
// must still agree with the round trip to well under a basis point.
|
|
175
|
+
const gross = 0.22;
|
|
176
|
+
const net = netEffectiveApy(gross, 0.01);
|
|
177
|
+
expect(net).toBeLessThan(gross);
|
|
178
|
+
expect(gross - net).toBeLessThan(0.0005);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
//# sourceMappingURL=rates.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rates.test.js","sourceRoot":"","sources":["../../src/domain/rates.test.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA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