@epicentral/sos-sdk 0.6.1-alpha → 0.7.0-alpha

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.

Potentially problematic release.


This version of @epicentral/sos-sdk might be problematic. Click here for more details.

@@ -87,6 +87,8 @@ export interface UnwindPreflightSummary {
87
87
 
88
88
  export interface UnwindPreflightResult {
89
89
  canUnwind: boolean;
90
+ canRepayRequestedSlice: boolean;
91
+ /** @deprecated Use canRepayRequestedSlice. */
90
92
  canRepayFully: boolean;
91
93
  reason?: string;
92
94
  writerPositionAddress: string;
@@ -145,6 +147,7 @@ export async function preflightUnwindWriterUnsold(
145
147
  if (unwindQty <= 0n) {
146
148
  return {
147
149
  canUnwind: false,
150
+ canRepayRequestedSlice: false,
148
151
  canRepayFully: false,
149
152
  reason: "unwindQty must be > 0",
150
153
  writerPositionAddress: String(writerPositionAddress),
@@ -178,6 +181,7 @@ export async function preflightUnwindWriterUnsold(
178
181
  if (unwindQty > unsoldQty) {
179
182
  return {
180
183
  canUnwind: false,
184
+ canRepayRequestedSlice: false,
181
185
  canRepayFully: false,
182
186
  reason: "unwindQty exceeds writer unsold quantity",
183
187
  writerPositionAddress: String(writerPositionAddress),
@@ -211,7 +215,7 @@ export async function preflightUnwindWriterUnsold(
211
215
 
212
216
  const slotNow = toBigInt(currentSlot);
213
217
  const protocolFeeBps = BigInt(vault.protocolFeeBps);
214
- const slotsPerYear = 63_072_000n;
218
+ const slotsPerYear = 78_840_000n;
215
219
  const loanBreakdown: Array<UnwindLoanBreakdown> = [];
216
220
 
217
221
  for (const loan of loans) {
@@ -267,20 +271,20 @@ export async function preflightUnwindWriterUnsold(
267
271
 
268
272
  // Calculate proportional obligations for partial unwinds
269
273
  const writtenQty = toBigInt(writerPosition.writtenQty);
270
- const unwindRatio = writtenQty > 0n ? (unwindQty * 1_000_000n) / writtenQty : 0n; // Basis points precision
271
- const unwindRatioDecimal = Number(unwindRatio) / 1_000_000; // Convert to decimal
272
-
273
- // Proportional obligations (for partial unwind logic)
274
274
  const proportionalPrincipal = writtenQty > 0n ? (totals.principal * unwindQty) / writtenQty : 0n;
275
275
  const proportionalInterest = writtenQty > 0n ? (totals.interest * unwindQty) / writtenQty : 0n;
276
276
  const proportionalProtocolFees = writtenQty > 0n ? (totals.fees * unwindQty) / writtenQty : 0n;
277
277
  const proportionalTotalOwed = proportionalPrincipal + proportionalInterest + proportionalProtocolFees;
278
278
 
279
- // Collateral return calculation (proportional share minus proportional obligations)
279
+ // On-chain unwind repays from collateral vault first, then optional wallet fallback.
280
+ // The collateral share returned to the writer is therefore reduced only by the
281
+ // amount actually sourced from the collateral vault, not by the entire debt slice.
280
282
  const collateralDeposited = toBigInt(writerPosition.collateralDeposited);
281
283
  const proportionalCollateralShare = writtenQty > 0n ? (collateralDeposited * unwindQty) / writtenQty : 0n;
282
- const returnableCollateral = proportionalCollateralShare > proportionalTotalOwed
283
- ? proportionalCollateralShare - proportionalTotalOwed
284
+ const collateralUsedForRepay =
285
+ proportionalTotalOwed > collateralVaultAvailable ? collateralVaultAvailable : proportionalTotalOwed;
286
+ const returnableCollateral = proportionalCollateralShare > collateralUsedForRepay
287
+ ? proportionalCollateralShare - collateralUsedForRepay
284
288
  : 0n;
285
289
 
286
290
  // Calculate shortfall against proportional obligations
@@ -295,14 +299,13 @@ export async function preflightUnwindWriterUnsold(
295
299
  const effectiveShortfall =
296
300
  proportionalTotalOwed > effectiveTotalAvailable ? proportionalTotalOwed - effectiveTotalAvailable : 0n;
297
301
 
298
- // For top-up UX: explicit collateral vault shortfall
299
- const collateralVaultShortfall = returnableCollateral > collateralVaultAvailable
300
- ? returnableCollateral - collateralVaultAvailable
301
- : 0n;
302
- const needsWalletTopUp = collateralVaultShortfall > 0n && walletFallbackAvailable < collateralVaultShortfall;
302
+ // For top-up UX, surface the wallet contribution required after collateral-vault-first repayment.
303
+ const collateralVaultShortfall = walletFallbackRequired;
304
+ const needsWalletTopUp = walletFallbackRequired > walletFallbackAvailable;
303
305
 
304
306
  return {
305
307
  canUnwind: true,
308
+ canRepayRequestedSlice: effectiveShortfall === 0n,
306
309
  canRepayFully: effectiveShortfall === 0n,
307
310
  reason:
308
311
  effectiveShortfall === 0n