fix(settings): fail closed on blank loan input
This commit is contained in:
@@ -53,20 +53,24 @@ def settings_page(workspace_id: str) -> None:
|
||||
return None
|
||||
return parsed if parsed > 0 else None
|
||||
|
||||
def as_non_negative_float(value: object) -> float:
|
||||
def as_non_negative_float(value: object) -> float | None:
|
||||
try:
|
||||
parsed = float(value)
|
||||
except (TypeError, ValueError):
|
||||
return 0.0
|
||||
return max(parsed, 0.0)
|
||||
return None
|
||||
return parsed if parsed >= 0 else None
|
||||
|
||||
def build_preview_config() -> PortfolioConfig:
|
||||
parsed_loan_amount = as_non_negative_float(loan_amount.value)
|
||||
if parsed_loan_amount is None:
|
||||
raise ValueError("Loan amount must be zero or greater")
|
||||
|
||||
return PortfolioConfig(
|
||||
gold_value=as_positive_float(gold_value.value),
|
||||
entry_price=as_positive_float(entry_price.value),
|
||||
gold_ounces=as_positive_float(gold_ounces.value),
|
||||
entry_basis_mode=str(entry_basis_mode.value),
|
||||
loan_amount=as_non_negative_float(loan_amount.value),
|
||||
loan_amount=parsed_loan_amount,
|
||||
margin_threshold=float(margin_threshold.value),
|
||||
monthly_budget=float(monthly_budget.value),
|
||||
ltv_warning=float(ltv_warning.value),
|
||||
@@ -125,12 +129,14 @@ def settings_page(workspace_id: str) -> None:
|
||||
step=0.01,
|
||||
).classes("w-full")
|
||||
|
||||
loan_amount = ui.number(
|
||||
"Loan amount ($)",
|
||||
value=config.loan_amount,
|
||||
min=0,
|
||||
step=1000,
|
||||
).classes("w-full")
|
||||
loan_amount = (
|
||||
ui.input(
|
||||
"Loan amount ($)",
|
||||
value=str(config.loan_amount),
|
||||
)
|
||||
.props("type=number min=0 step=1000")
|
||||
.classes("w-full")
|
||||
)
|
||||
|
||||
margin_threshold = ui.number(
|
||||
"Margin call LTV threshold",
|
||||
@@ -337,7 +343,7 @@ def settings_page(workspace_id: str) -> None:
|
||||
loan = as_non_negative_float(loan_amount.value)
|
||||
margin = as_positive_float(margin_threshold.value)
|
||||
|
||||
if collateral_value is not None and collateral_value > 0:
|
||||
if collateral_value is not None and collateral_value > 0 and loan is not None:
|
||||
ltv = (loan / collateral_value) * 100
|
||||
buffer = ((margin or 0.0) - loan / collateral_value) * 100 if margin is not None else 0.0
|
||||
ltv_display.set_text(f"{ltv:.1f}%")
|
||||
@@ -346,9 +352,15 @@ def settings_page(workspace_id: str) -> None:
|
||||
ltv_display.set_text("—")
|
||||
buffer_display.set_text("—")
|
||||
|
||||
if margin is not None and ounces is not None and ounces > 0:
|
||||
if loan is not None and margin is not None and ounces is not None and ounces > 0:
|
||||
margin_price_display.set_text(f"${loan / (margin * ounces):,.2f}/oz")
|
||||
elif margin is not None and price is not None and collateral_value is not None and collateral_value > 0:
|
||||
elif (
|
||||
loan is not None
|
||||
and margin is not None
|
||||
and price is not None
|
||||
and collateral_value is not None
|
||||
and collateral_value > 0
|
||||
):
|
||||
implied_ounces = collateral_value / price
|
||||
margin_price_display.set_text(f"${loan / (margin * implied_ounces):,.2f}/oz")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user