@elemental-stv-core/sdk 0.9.0 → 0.9.1
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.
|
@@ -99,6 +99,10 @@ export interface JlpEffectiveWeights {
|
|
|
99
99
|
export interface EffectiveWeightInput {
|
|
100
100
|
owned: bigint;
|
|
101
101
|
locked: bigint;
|
|
102
|
+
/** Raw debt u128 (DEBT_MULTIPLIER-scaled by 1e9 internally, then by 10^decimals). */
|
|
103
|
+
debt: bigint;
|
|
104
|
+
/** Raw borrow-lend interests accrued u128 (same scale as `debt`). */
|
|
105
|
+
borrowLendInterestsAccrued: bigint;
|
|
102
106
|
shortSizeUsd: bigint;
|
|
103
107
|
avgShortPriceUsd: bigint;
|
|
104
108
|
guaranteedUsd: bigint;
|
|
@@ -107,25 +111,31 @@ export interface EffectiveWeightInput {
|
|
|
107
111
|
priceScaled8: bigint;
|
|
108
112
|
}
|
|
109
113
|
export interface EffectiveWeightOutput {
|
|
110
|
-
/** (owned − locked) × price, normalized to 8-decimal USD. Clamped to 0 if negative. */
|
|
114
|
+
/** (owned + netDebt − locked) × price, normalized to 8-decimal USD. Clamped to 0 if negative. */
|
|
111
115
|
spotEffUsd: bigint;
|
|
112
116
|
/** shortSize × price / avgShortPrice, normalized to 8-decimal USD. 0 if no shorts. */
|
|
113
117
|
shortEffUsd: bigint;
|
|
114
118
|
/** spotEffUsd + shortEffUsd. */
|
|
115
119
|
effUsd: bigint;
|
|
116
|
-
/** True if (owned − locked) < 0 — spot term was clamped. */
|
|
120
|
+
/** True if (owned + netDebt − locked) < 0 — spot term was clamped. */
|
|
117
121
|
spotClamped: boolean;
|
|
122
|
+
/** netDebt tokens applied to spot term (already converted to token-base units). */
|
|
123
|
+
netDebtTokens: bigint;
|
|
118
124
|
}
|
|
119
125
|
/** Compute effective USD (price-sensitivity-weighted) for a single custody.
|
|
120
126
|
*
|
|
121
127
|
* Formula (in 8-decimal USD):
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
128
|
+
* netDebtTokens = max(debt − borrowLendInterestsAccrued, 0) / DEBT_MULTIPLIER
|
|
129
|
+
* spot = (owned + netDebtTokens − locked) × price_8dec / 10^decimals
|
|
130
|
+
* short = shortSize_6dec × price_8dec / avgShortPrice_6dec (zeroed if either operand is 0)
|
|
131
|
+
* effUsd = max(spot, 0) + short
|
|
125
132
|
*
|
|
126
133
|
* Algebra for unit normalization:
|
|
127
|
-
* - owned/locked is in 10^decimals base units; price is 10^8 USD.
|
|
128
|
-
* spot = (owned − locked) × price_8dec / 10^decimals → 10^8 USD ✓
|
|
134
|
+
* - owned/locked/netDebt is in 10^decimals base units; price is 10^8 USD.
|
|
135
|
+
* spot = (owned + netDebt − locked) × price_8dec / 10^decimals → 10^8 USD ✓
|
|
136
|
+
* - debt is stored as u128 scaled by DEBT_MULTIPLIER (1e9) on top of 10^decimals.
|
|
137
|
+
* netDebtTokens = (debt − borrowLendInterestsAccrued) / DEBT_MULTIPLIER
|
|
138
|
+
* (integer division — small rounding under one base unit, acceptable).
|
|
129
139
|
* - shortSize and avgShortPrice are both 10^6; ratio is dimensionless.
|
|
130
140
|
* short = shortSize × price_8dec / avgShortPrice → 10^8 USD ✓
|
|
131
141
|
*/
|
|
@@ -422,22 +422,34 @@ function priceToScaled8(priceFloat) {
|
|
|
422
422
|
/** Compute effective USD (price-sensitivity-weighted) for a single custody.
|
|
423
423
|
*
|
|
424
424
|
* Formula (in 8-decimal USD):
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
425
|
+
* netDebtTokens = max(debt − borrowLendInterestsAccrued, 0) / DEBT_MULTIPLIER
|
|
426
|
+
* spot = (owned + netDebtTokens − locked) × price_8dec / 10^decimals
|
|
427
|
+
* short = shortSize_6dec × price_8dec / avgShortPrice_6dec (zeroed if either operand is 0)
|
|
428
|
+
* effUsd = max(spot, 0) + short
|
|
428
429
|
*
|
|
429
430
|
* Algebra for unit normalization:
|
|
430
|
-
* - owned/locked is in 10^decimals base units; price is 10^8 USD.
|
|
431
|
-
* spot = (owned − locked) × price_8dec / 10^decimals → 10^8 USD ✓
|
|
431
|
+
* - owned/locked/netDebt is in 10^decimals base units; price is 10^8 USD.
|
|
432
|
+
* spot = (owned + netDebt − locked) × price_8dec / 10^decimals → 10^8 USD ✓
|
|
433
|
+
* - debt is stored as u128 scaled by DEBT_MULTIPLIER (1e9) on top of 10^decimals.
|
|
434
|
+
* netDebtTokens = (debt − borrowLendInterestsAccrued) / DEBT_MULTIPLIER
|
|
435
|
+
* (integer division — small rounding under one base unit, acceptable).
|
|
432
436
|
* - shortSize and avgShortPrice are both 10^6; ratio is dimensionless.
|
|
433
437
|
* short = shortSize × price_8dec / avgShortPrice → 10^8 USD ✓
|
|
434
438
|
*/
|
|
435
439
|
function computeEffectiveUsd(input) {
|
|
436
|
-
const { owned, locked, shortSizeUsd, avgShortPriceUsd, decimals, priceScaled8 } = input;
|
|
440
|
+
const { owned, locked, debt, borrowLendInterestsAccrued, shortSizeUsd, avgShortPriceUsd, decimals, priceScaled8, } = input;
|
|
437
441
|
const tokScale = 10n ** BigInt(decimals);
|
|
438
|
-
//
|
|
439
|
-
//
|
|
440
|
-
|
|
442
|
+
// Net debt tokens: (debt − interestsAccrued) / DEBT_MULTIPLIER, clamped to 0.
|
|
443
|
+
// Matches `calculateDebtTokens` in `calculateCustodyAum` to keep both code paths
|
|
444
|
+
// consistent. DEBT_MULTIPLIER is 1e9, scaled before token decimals.
|
|
445
|
+
const DEBT_MULTIPLIER_BIG = 1000000000n; // 1e9, matches DEBT_MULTIPLIER constant
|
|
446
|
+
let netDebtTokens = 0n;
|
|
447
|
+
if (debt > borrowLendInterestsAccrued) {
|
|
448
|
+
netDebtTokens = (debt - borrowLendInterestsAccrued) / DEBT_MULTIPLIER_BIG;
|
|
449
|
+
}
|
|
450
|
+
// Spot term: (owned + netDebt − locked) × price / 10^decimals
|
|
451
|
+
// Use signed bigint subtraction; clamp to 0 if (owned + netDebt) < locked.
|
|
452
|
+
const netTokens = owned + netDebtTokens - locked; // bigint — can go negative
|
|
441
453
|
let spotEffUsd;
|
|
442
454
|
let spotClamped = false;
|
|
443
455
|
if (netTokens <= 0n) {
|
|
@@ -459,6 +471,7 @@ function computeEffectiveUsd(input) {
|
|
|
459
471
|
shortEffUsd,
|
|
460
472
|
effUsd: spotEffUsd + shortEffUsd,
|
|
461
473
|
spotClamped,
|
|
474
|
+
netDebtTokens,
|
|
462
475
|
};
|
|
463
476
|
}
|
|
464
477
|
/** Compute basis-point shares for each effective USD value, summing to 10000.
|
|
@@ -529,6 +542,8 @@ async function fetchJlpEffectiveWeights(connection) {
|
|
|
529
542
|
const effInput = {
|
|
530
543
|
owned: raw.owned,
|
|
531
544
|
locked: raw.locked,
|
|
545
|
+
debt: raw.debt,
|
|
546
|
+
borrowLendInterestsAccrued: raw.borrowLendInterestsAccrued,
|
|
532
547
|
shortSizeUsd: raw.globalShortSizes,
|
|
533
548
|
avgShortPriceUsd: raw.globalShortAveragePrices,
|
|
534
549
|
guaranteedUsd: raw.guaranteedUsd,
|