@elemental-stv-core/sdk 0.9.0 → 0.9.2

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
- * spot = (ownedlocked) × price_8dec / 10^decimals
123
- * short = shortSize_6dec × price_8dec / avgShortPrice_6dec (zeroed if either operand is 0)
124
- * effUsd = max(spot, 0) + short
128
+ * netDebtTokens = max(debtborrowLendInterestsAccrued, 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
  */
@@ -105,19 +105,41 @@ async function fetchAllCustodies(connection) {
105
105
  };
106
106
  });
107
107
  }
108
+ // Module-level memo for Jupiter price/v3 — 3 consumers in this file
109
+ // (fetchJlpCustodyData, fetchJlpPoolDetails, fetchJlpEffectiveWeights) used to
110
+ // each hit the API independently. A single jlp-pool-snapshot route would fire
111
+ // 3 redundant calls per cache miss. 30s TTL is short enough that prices stay
112
+ // fresh for downstream NAV/weight math, long enough to coalesce snapshot loads.
113
+ const PRICES_TTL_MS = 30000;
114
+ let pricesCache = null;
115
+ let pricesInFlight = null;
108
116
  async function fetchPrices() {
117
+ if (pricesCache && Date.now() - pricesCache.at < PRICES_TTL_MS) {
118
+ return pricesCache.data;
119
+ }
120
+ if (pricesInFlight)
121
+ return pricesInFlight;
109
122
  const ids = CUSTODIES.map((c) => c.mintAddress).join(",");
110
123
  const url = `${JUPITER_PRICE_API}?ids=${ids}`;
111
- const response = await fetch(url);
112
- if (!response.ok) {
113
- throw new Error(`Jupiter Price API error: ${response.status}`);
114
- }
115
- const json = (await response.json());
116
- const prices = {};
117
- for (const [mint, data] of Object.entries(json)) {
118
- prices[mint] = data.usdPrice;
119
- }
120
- return prices;
124
+ pricesInFlight = (async () => {
125
+ try {
126
+ const response = await fetch(url);
127
+ if (!response.ok) {
128
+ throw new Error(`Jupiter Price API error: ${response.status}`);
129
+ }
130
+ const json = (await response.json());
131
+ const prices = {};
132
+ for (const [mint, data] of Object.entries(json)) {
133
+ prices[mint] = data.usdPrice;
134
+ }
135
+ pricesCache = { at: Date.now(), data: prices };
136
+ return prices;
137
+ }
138
+ finally {
139
+ pricesInFlight = null;
140
+ }
141
+ })();
142
+ return pricesInFlight;
121
143
  }
122
144
  function calculateDebtTokens(raw) {
123
145
  if (raw.debt <= raw.borrowLendInterestsAccrued)
@@ -422,22 +444,34 @@ function priceToScaled8(priceFloat) {
422
444
  /** Compute effective USD (price-sensitivity-weighted) for a single custody.
423
445
  *
424
446
  * Formula (in 8-decimal USD):
425
- * spot = (ownedlocked) × price_8dec / 10^decimals
426
- * short = shortSize_6dec × price_8dec / avgShortPrice_6dec (zeroed if either operand is 0)
427
- * effUsd = max(spot, 0) + short
447
+ * netDebtTokens = max(debtborrowLendInterestsAccrued, 0) / DEBT_MULTIPLIER
448
+ * spot = (owned + netDebtTokens locked) × price_8dec / 10^decimals
449
+ * short = shortSize_6dec × price_8dec / avgShortPrice_6dec (zeroed if either operand is 0)
450
+ * effUsd = max(spot, 0) + short
428
451
  *
429
452
  * 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 ✓
453
+ * - owned/locked/netDebt is in 10^decimals base units; price is 10^8 USD.
454
+ * spot = (owned + netDebt − locked) × price_8dec / 10^decimals → 10^8 USD ✓
455
+ * - debt is stored as u128 scaled by DEBT_MULTIPLIER (1e9) on top of 10^decimals.
456
+ * netDebtTokens = (debt − borrowLendInterestsAccrued) / DEBT_MULTIPLIER
457
+ * (integer division — small rounding under one base unit, acceptable).
432
458
  * - shortSize and avgShortPrice are both 10^6; ratio is dimensionless.
433
459
  * short = shortSize × price_8dec / avgShortPrice → 10^8 USD ✓
434
460
  */
435
461
  function computeEffectiveUsd(input) {
436
- const { owned, locked, shortSizeUsd, avgShortPriceUsd, decimals, priceScaled8 } = input;
462
+ const { owned, locked, debt, borrowLendInterestsAccrued, shortSizeUsd, avgShortPriceUsd, decimals, priceScaled8, } = input;
437
463
  const tokScale = 10n ** BigInt(decimals);
438
- // Spot term: (ownedlocked) × price / 10^decimals
439
- // Use signed bigint subtraction; clamp to 0 if owned < locked (defensive).
440
- const netTokens = owned - locked; // bigint — can go negative
464
+ // Net debt tokens: (debtinterestsAccrued) / DEBT_MULTIPLIER, clamped to 0.
465
+ // Matches `calculateDebtTokens` in `calculateCustodyAum` to keep both code paths
466
+ // consistent. DEBT_MULTIPLIER is 1e9, scaled before token decimals.
467
+ const DEBT_MULTIPLIER_BIG = 1000000000n; // 1e9, matches DEBT_MULTIPLIER constant
468
+ let netDebtTokens = 0n;
469
+ if (debt > borrowLendInterestsAccrued) {
470
+ netDebtTokens = (debt - borrowLendInterestsAccrued) / DEBT_MULTIPLIER_BIG;
471
+ }
472
+ // Spot term: (owned + netDebt − locked) × price / 10^decimals
473
+ // Use signed bigint subtraction; clamp to 0 if (owned + netDebt) < locked.
474
+ const netTokens = owned + netDebtTokens - locked; // bigint — can go negative
441
475
  let spotEffUsd;
442
476
  let spotClamped = false;
443
477
  if (netTokens <= 0n) {
@@ -459,6 +493,7 @@ function computeEffectiveUsd(input) {
459
493
  shortEffUsd,
460
494
  effUsd: spotEffUsd + shortEffUsd,
461
495
  spotClamped,
496
+ netDebtTokens,
462
497
  };
463
498
  }
464
499
  /** Compute basis-point shares for each effective USD value, summing to 10000.
@@ -529,6 +564,8 @@ async function fetchJlpEffectiveWeights(connection) {
529
564
  const effInput = {
530
565
  owned: raw.owned,
531
566
  locked: raw.locked,
567
+ debt: raw.debt,
568
+ borrowLendInterestsAccrued: raw.borrowLendInterestsAccrued,
532
569
  shortSizeUsd: raw.globalShortSizes,
533
570
  avgShortPriceUsd: raw.globalShortAveragePrices,
534
571
  guaranteedUsd: raw.guaranteedUsd,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elemental-stv-core/sdk",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "TypeScript SDK for Elemental Vaults — p-STV Core, Elemental Lend, JLPD Strategy",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [