Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/day-v3/DayV3Goals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export default function DayV3Goals({
onRecoveryMode,
onResetExit,
onResetProtection,
poolPremiumEdited = false,
poolTurnoverPerYear,
defaultPremiumBps,
onPoolPremiumBps,
Expand Down Expand Up @@ -166,6 +167,7 @@ export default function DayV3Goals({
swapFeeBps: MaybeNumber;
defaultPremiumBps: MaybeNumber;
onPoolPremiumBps: (value: number | null) => void;
poolPremiumEdited?: boolean;
poolPremiumBps: MaybeNumber;
restingSeniorWeight: MaybeNumber;
}) {
Expand Down Expand Up @@ -487,6 +489,7 @@ export default function DayV3Goals({
onLabel={onQuoteAssetLabel}
defaultPremiumBps={defaultPremiumBps}
onPoolPremiumBps={onPoolPremiumBps}
poolPremiumEdited={poolPremiumEdited}
poolPremiumBps={poolPremiumBps}
restingSeniorWeight={restingSeniorWeight}
onSwapFeeBps={onSwapFeeBps}
Expand Down
5 changes: 4 additions & 1 deletion components/day-v3/DayV3QuoteAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function DayV3QuoteAsset({
onLabel,
defaultPremiumBps,
onPoolPremiumBps,
poolPremiumEdited,
poolPremiumBps,
restingSeniorWeight,
onSwapFeeBps,
Expand All @@ -57,6 +58,8 @@ export default function DayV3QuoteAsset({
/** What the pool rests on today, in bps, so an empty field can say so. */
defaultPremiumBps: number | null;
onPoolPremiumBps: (value: number | null) => void;
/** Whether the displayed premium is an issuer edit or a loaded default. */
poolPremiumEdited: boolean;
/** The maximum premium the issuer will accept, in bps. Null uses the market's. */
poolPremiumBps: number | null;
/** Where the resulting curve rests, 0..1, for the field's own readout. */
Expand Down Expand Up @@ -190,7 +193,7 @@ export default function DayV3QuoteAsset({
: `Sets the pool's balance point. The pool currently rests on ${(restingSeniorWeight * 100).toFixed(restingSeniorWeight < 0.1 ? 2 : 1)}% Senior shares and ${((1 - restingSeniorWeight) * 100).toFixed(2)}% ${label}. Wider means more Senior inventory and less depth for a seller. Empty models the pool at the premium shown.`
}
onChange={onPoolPremiumBps}
origin={poolPremiumBps === null ? "model-assumption" : "manual-override"}
origin={poolPremiumEdited ? "manual-override" : "model-assumption"}
// The premium actually in force, whatever set it — the market's own
// declared curve, a live template, or the modeled default. Calling it
// "the market's own" was false whenever it was not.
Expand Down
164 changes: 137 additions & 27 deletions components/day-v3/DayV3Summary.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { useDeferredValue, useEffect, useMemo, useState } from "react";
import { useDeferredValue, useEffect, useMemo, useRef, useState } from "react";

import { Badge } from "@/components/ui/badge";
import DayV3Backtest from "@/components/day-v3/DayV3Backtest";
Expand Down Expand Up @@ -67,6 +67,7 @@ import {
normalizeDayV3Defaults,
recommendDayV3Coverage,
validateDayV3YieldCurveDesign,
type DayV3YieldCurveDesign,
type DayV3StarterDefaultField,
type DayV3UrlState,
} from "@/lib/day-v3";
Expand Down Expand Up @@ -126,6 +127,46 @@ const CUSTOM_SOURCE_MARKET = buildDayYieldDraftMarket({
label: "Custom yield source",
sourceApy: 0.12,
});

type DayV3YieldCurveOverrideFields = Pick<
DayV3Overrides,
| "jrYieldShareAtZeroPct"
| "jrYieldShareAtTargetPct"
| "jrYieldShareAtFullPct"
| "slpYieldShareAtZeroPct"
| "slpYieldShareAtTargetPct"
| "slpYieldShareAtFullPct"
>;

const DAY_V3_YIELD_CURVE_FIELDS = [
"jrYieldShareAtZeroPct",
"jrYieldShareAtTargetPct",
"jrYieldShareAtFullPct",
"slpYieldShareAtZeroPct",
"slpYieldShareAtTargetPct",
"slpYieldShareAtFullPct",
] as const satisfies readonly (keyof DayV3YieldCurveOverrideFields)[];

function hasAnyDayV3YieldCurveOverride(
overrides: Partial<DayV3YieldCurveOverrideFields>,
): boolean {
return DAY_V3_YIELD_CURVE_FIELDS.some(
(field) => overrides[field] !== null && overrides[field] !== undefined,
);
}

function dayV3YieldCurveDesignToOverrides(
design: DayV3YieldCurveDesign,
): DayV3YieldCurveOverrideFields {
return {
jrYieldShareAtZeroPct: design.junior.y0Pct,
jrYieldShareAtTargetPct: design.junior.yTargetPct,
jrYieldShareAtFullPct: design.junior.y100Pct,
slpYieldShareAtZeroPct: design.slp.y0Pct,
slpYieldShareAtTargetPct: design.slp.yTargetPct,
slpYieldShareAtFullPct: design.slp.y100Pct,
};
}
/**
* Everything the shared accountant runs with instead of the market's own
* config. Every key is optional because this object is spread over that
Expand Down Expand Up @@ -158,6 +199,16 @@ export default function DayV3Summary({
...EMPTY_DAY_V3_OVERRIDES,
...linked?.overrides,
});
// The starting yield curves are a loaded design, not a live formula tied to
// the latest exit sizing. We hydrate them once per explicit design load and
// then keep them stable until the user edits the curve itself.
const [yieldCurveLoadVersion, setYieldCurveLoadVersion] = useState(0);
const [loadedYieldCurve, setLoadedYieldCurve] =
useState<DayV3YieldCurveDesign | null>(null);
const yieldCurveHydratedVersion = useRef(-1);
const [yieldCurveEdited, setYieldCurveEdited] = useState(() =>
hasAnyDayV3YieldCurveOverride(linked?.overrides ?? EMPTY_DAY_V3_OVERRIDES),
);
const [starterFields, setStarterFields] = useState<
Set<DayV3StarterDefaultField>
>(() => new Set(starterDefaultFields));
Expand Down Expand Up @@ -243,11 +294,17 @@ export default function DayV3Summary({
);
const feeOverridden = swapFeeBps !== null;
// The pool's balance point, set the way deployment sets it: by the maximum
// premium. Empty leaves the curve solved for the simulator's own resting
// weight, which is the behaviour every existing link describes.
// premium. Empty starts with the model's loaded premium; a value here means
// the issuer explicitly changed this field. Keeping those states separate
// lets the loaded default stay stable without mislabeling it as a manual
// override or suppressing a live pool recommendation.
const [poolPremiumBps, setPoolPremiumBps] = useState<number | null>(
linked?.poolPremiumBps ?? null,
);
const [loadedPoolPremiumBps, setLoadedPoolPremiumBps] = useState<
number | null
>(null);
const poolPremiumHydratedVersion = useRef(-1);
const premiumOverridden = poolPremiumBps !== null;
// An outside desk's terms. These describe who might arbitrage the pool back
// to NAV; they price nothing in the market itself and never gate a result.
Expand Down Expand Up @@ -344,6 +401,8 @@ export default function DayV3Summary({
// A fee is a property of the pool being designed, so it is released with
// the rest of the exit rather than carried onto the next market.
setSwapFeeBps(marketDefaults?.swapFeeBps ?? null);
setPoolPremiumBps(null);
setLoadedPoolPremiumBps(null);
setMarketMakerCostOfCapitalPct(
marketDefaults?.marketMakerCostOfCapitalPct ?? 12,
);
Expand All @@ -353,6 +412,9 @@ export default function DayV3Summary({
...EMPTY_DAY_V3_OVERRIDES,
...(marketDefaults?.overrides ?? {}),
});
setLoadedYieldCurve(null);
setYieldCurveEdited(false);
setYieldCurveLoadVersion((current) => current + 1);
// Adopted with the rest of the market's terms, for the same reason: the
// toggle must describe the market on screen, not the previous one.
setMaintainCoverage(next.defaults.maintainCoverage);
Expand Down Expand Up @@ -383,22 +445,12 @@ export default function DayV3Summary({
};

const resetYieldCurveOverrides = () => {
const marketDefaults = dayV3MarketDefaults(market.id);
if (!loadedYieldCurve) return;
setManualOverrides((current) => ({
...current,
jrYieldShareAtZeroPct:
marketDefaults?.overrides.jrYieldShareAtZeroPct ?? null,
jrYieldShareAtFullPct:
marketDefaults?.overrides.jrYieldShareAtFullPct ?? null,
slpYieldShareAtZeroPct:
marketDefaults?.overrides.slpYieldShareAtZeroPct ?? null,
slpYieldShareAtFullPct:
marketDefaults?.overrides.slpYieldShareAtFullPct ?? null,
jrYieldShareAtTargetPct:
marketDefaults?.overrides.jrYieldShareAtTargetPct ?? null,
slpYieldShareAtTargetPct:
marketDefaults?.overrides.slpYieldShareAtTargetPct ?? null,
...dayV3YieldCurveDesignToOverrides(loadedYieldCurve),
}));
setYieldCurveEdited(false);
};

const protectionRecommendation = useMemo(
Expand Down Expand Up @@ -570,6 +622,13 @@ export default function DayV3Summary({
defaults.eclpParams !== undefined &&
Math.abs(floorBandPct / 100 - defaults.eclpBandWidth) < 1e-12;
if (declaredCurveSurvives) return null;
if (loadedPoolPremiumBps !== null) {
return dayV3PoolCurveFromPremium({
bandPct: floorBandPct,
premiumBps: loadedPoolPremiumBps,
lambda,
});
}
// Otherwise build the pool the deploy step would build for this band at its
// own 90/10 default, rather than leaving the engine's fallback.
//
Expand Down Expand Up @@ -597,6 +656,7 @@ export default function DayV3Summary({
defaults.eclpBandWidth,
defaults.eclpParams,
floorBandPct,
loadedPoolPremiumBps,
poolPremiumBps,
premiumOverridden,
]);
Expand Down Expand Up @@ -838,9 +898,9 @@ export default function DayV3Summary({
}
: targetShareDesign;
// A zero capital requirement always pays zero. This also prevents a stale
// manual curve from charging Senior after the corresponding tranche is
// removed. Every non-zero automatic anchor comes from the one complete
// starting policy above; endpoints are no longer frozen template values.
// saved curve from charging Senior after the corresponding tranche is
// removed. The parent materializes the initial design after this snapshot
// is ready, so later exit/protection edits do not replace its anchors.
const junior = coverage > 0 ? design.junior : zero;
const slp = minLiquidity > 0 ? design.slp : zero;
const maxLiquidityCurve = Math.max(slp.y0Pct, slp.yTargetPct, slp.y100Pct);
Expand All @@ -864,6 +924,39 @@ export default function DayV3Summary({
};
}, [inputs, simulationDefaults]);

// Defaults are loaded once for each explicit source/market design. Before
// this hydration, `resolved` may use the capital-parity policy to produce the
// first complete curve. After hydration, the six anchors live in
// `manualOverrides` and therefore remain stable while exit and protection
// inputs recalculate their own derived capital requirements.
useEffect(() => {
if (
yieldCurveHydratedVersion.current === yieldCurveLoadVersion ||
inputs !== immediateModelInput
) {
return;
}
if (resolved.startingPolicy.design === null) return;
const snapshot = {
junior: {
y0Pct: resolved.y0 * 100,
yTargetPct: resolved.riskYieldShare * 100,
y100Pct: resolved.y100 * 100,
},
slp: {
y0Pct: resolved.liqY0 * 100,
yTargetPct: resolved.liquidityYieldShare * 100,
y100Pct: resolved.liqY100 * 100,
},
} satisfies DayV3YieldCurveDesign;
yieldCurveHydratedVersion.current = yieldCurveLoadVersion;
setLoadedYieldCurve(snapshot);
setManualOverrides((current) => ({
...current,
...dayV3YieldCurveDesignToOverrides(snapshot),
}));
}, [immediateModelInput, inputs, resolved, yieldCurveLoadVersion]);

// Capital sizing, loss absorption, and pool depth do not depend on how yield
// is split. Keeping them in a separate shared-engine run prevents a yield
// slider tick from rebuilding the expensive stress and exit explainers.
Expand Down Expand Up @@ -1131,6 +1224,27 @@ export default function DayV3Summary({
}),
[baseReturns, riskOnly, scenario, structuralModel],
);
// Materialize the first modelled premium once per explicit source/market
// design. The visible field can then show the loaded default and the local
// curve keeps the same beta when payout, depth, or protection inputs move.
// A live canonical curve still wins unless the issuer explicitly enters a
// premium, so this snapshot does not turn an inherited value into a manual
// override.
useEffect(() => {
if (
poolPremiumHydratedVersion.current === yieldCurveLoadVersion ||
inputs !== immediateModelInput
) {
return;
}
const premium = model.pool.premiumBps;
if (premium === null || !Number.isFinite(premium)) return;
poolPremiumHydratedVersion.current = yieldCurveLoadVersion;
// The field's placeholder already showed two decimal places. Keep the
// materialized value equally readable instead of exposing bisection noise
// such as 7.3727794094802235 bps as if it were an issuer input.
setLoadedPoolPremiumBps(Number(premium.toFixed(2)));
}, [immediateModelInput, inputs, model, yieldCurveLoadVersion]);
// Nothing in the contract refills the exit pool. It comes back only when an
// outside desk is paid enough to buy the discounted Senior and redeem it, so
// the issuer's own exit terms decide whether capacity ever returns.
Expand Down Expand Up @@ -1266,13 +1380,7 @@ export default function DayV3Summary({
);
}, [query]);

const curveOverridden =
riskShareOverride !== null ||
liqShareOverride !== null ||
y0Override !== null ||
y100Override !== null ||
liqY0Override !== null ||
liqY100Override !== null;
const curveOverridden = yieldCurveEdited;
const yieldCurveValidation = validateDayV3YieldCurveDesign({
junior: {
y0Pct: resolved.y0 * 100,
Expand Down Expand Up @@ -1303,6 +1411,7 @@ export default function DayV3Summary({
>,
value: number,
) => {
setYieldCurveEdited(true);
setManualOverrides((current) => ({
...current,
// Endpoint edits must travel as one complete six-anchor policy. Preserve
Expand Down Expand Up @@ -1824,7 +1933,8 @@ export default function DayV3Summary({
exit={exitView}
defaultPremiumBps={model.pool.premiumBps}
onPoolPremiumBps={setPoolPremiumBps}
poolPremiumBps={poolPremiumBps}
poolPremiumEdited={premiumOverridden}
poolPremiumBps={poolPremiumBps ?? loadedPoolPremiumBps}
restingSeniorWeight={model.pool.restingSeniorWeight}
exitSharePct={immediateExitSharePct}
indexOffset={1}
Expand Down
57 changes: 57 additions & 0 deletions scripts/day-v3/model-output-parity.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,63 @@ assert.match(
/deriveDayV3StartingYieldCurvePolicy/,
"V3 must derive one complete starting policy behind the six visible anchors",
);
// The starting policy is a page-load snapshot. It must not be recomputed from
// the latest exit sizing after an issuer changes a different visible field.
assert.match(
summary,
/const \[yieldCurveLoadVersion, setYieldCurveLoadVersion\]/,
"yield-share defaults need an explicit design-load version",
);
assert.match(
summary,
/yieldCurveHydratedVersion\.current === yieldCurveLoadVersion[\s\S]*inputs !== immediateModelInput/,
"yield-share hydration must wait for the current model snapshot and run once",
);
assert.match(
summary,
/setLoadedYieldCurve\(snapshot\)[\s\S]*dayV3YieldCurveDesignToOverrides\(snapshot\)/,
"the initial six yield anchors must be materialized into the editable state",
);
assert.match(
summary,
/junior:\s*\{\s*y0Pct: resolved\.y0 \* 100,[\s\S]*yTargetPct: resolved\.riskYieldShare \* 100,[\s\S]*y100Pct: resolved\.y100 \* 100,[\s\S]*slp:\s*\{\s*y0Pct: resolved\.liqY0 \* 100,[\s\S]*yTargetPct: resolved\.liquidityYieldShare \* 100,[\s\S]*y100Pct: resolved\.liqY100 \* 100,/,
"an explicit linked curve must be snapshotted as displayed, not overwritten by the capital-parity fallback",
);
assert.match(
summary,
/const curveOverridden = yieldCurveEdited;/,
"loaded yield defaults must not be mislabeled as issuer-edited until a slider moves",
);
assert.match(
summary,
/const \[loadedPoolPremiumBps, setLoadedPoolPremiumBps\]/,
"the initial pool premium must have its own loaded-default state",
);
assert.match(
summary,
/setLoadedPoolPremiumBps\(Number\(premium\.toFixed\(2\)\)\)/,
"the initial modelled premium must be captured once per design load",
);
assert.match(
summary,
/poolPremiumBps=\{poolPremiumBps \?\? loadedPoolPremiumBps\}/,
"the premium field must show the loaded default without making it a manual override",
);
assert.match(
goals,
/poolPremiumEdited=\{poolPremiumEdited\}/,
"the premium field must distinguish a loaded default from an issuer edit",
);
assert.match(
quoteAsset,
/origin=\{poolPremiumEdited \? "manual-override" : "model-assumption"\}/,
"a loaded premium must retain model-assumption provenance in the visible field",
);
assert.match(
summary,
/setPoolPremiumBps\(null\);[\s\S]*setLoadedPoolPremiumBps\(null\);[\s\S]*setYieldCurveLoadVersion\(\(current\) => current \+ 1\)/,
"switching source or market must start a fresh default snapshot",
);
assert.doesNotMatch(
summary,
/dayV3EffectiveShares/,
Expand Down
Loading