diff --git a/contracts/AUDIT_NOTES.md b/contracts/AUDIT_NOTES.md index ebfb581..c57b9c2 100644 --- a/contracts/AUDIT_NOTES.md +++ b/contracts/AUDIT_NOTES.md @@ -391,9 +391,8 @@ The table below catalogs every admin, risk, and pauser entry point in `Registry` (`accept_and_settle`). It therefore approves the running sum of the borrower's outstanding leg-2 ceilings (`DataKey::BorrowerDue`) and never shortens the expiry (`accept_and_settle`, `write_borrower_due`), rather than this trade's ceiling alone, which would strand every earlier trade's keeper or lender `unwind`. - The sum is released by `unwind` and `default_close` (`release_borrower_due`, called from `unwind` and `default_close`) but **not** by - `default_at_maturity`, so a trade defaulted at maturity stays counted and the next settle - approves more than the borrower owes. A closed trade's share also stays spendable in the live + The sum is released by `unwind`, `default_close` and `default_at_maturity` + (`release_borrower_due`), so a closed trade stops counting toward what the next settle approves. A closed trade's share also stays spendable in the live allowance until the next settle or expiry (#843). - **Terms.** `propose` holds a trade's term, counted from the proposal to `maturity_at`, to its `kind`: 24h for intraday, 48h for overnight, 30 days for term and for open. It also refuses a diff --git a/contracts/repo-dvp/src/lib.rs b/contracts/repo-dvp/src/lib.rs index 9abc1a4..c5f1d83 100644 --- a/contracts/repo-dvp/src/lib.rs +++ b/contracts/repo-dvp/src/lib.rs @@ -1530,6 +1530,9 @@ impl RepoDvp { clear_margin_call(&mut trade); trade.status = RepoStatus::Defaulted; write_trade(&env, &trade); + // Like `unwind` and `default_close`: this trade is closed, so its ceiling leaves the + // borrower's outstanding leg-2 sum and the next settle approves only what is still owed. + release_borrower_due(&env, &trade); env.events() .publish((symbol_short!("mat_def"), id), (lender_units, borrower_units)); diff --git a/contracts/repo-dvp/src/test.rs b/contracts/repo-dvp/src/test.rs index 28d47a7..ecae221 100644 --- a/contracts/repo-dvp/src/test.rs +++ b/contracts/repo-dvp/src/test.rs @@ -1118,6 +1118,37 @@ fn default_at_maturity_exact_fit() { assert_eq!(trade.status, RepoStatus::Defaulted); } +#[test] +fn default_at_maturity_releases_the_trades_share_of_the_borrowers_due() { + let h = setup(); + let units = 100 * PRICE_SCALE; + let cash_amount = 100 * PRICE_SCALE; + let maturity = h.env.ledger().timestamp() + 3_600; + let id = propose(&h, units, cash_amount, 0, maturity); + MockTokenClient::new(&h.env, &h.rwa).mint(&h.cash_borrower, &units); + MockTokenClient::new(&h.env, &h.usdc).mint(&h.cash_lender, &cash_amount); + let repo = RepoDvpClient::new(&h.env, &h.repo); + repo.accept_and_settle(&h.cash_borrower, &id); + + let due = |h: &Harness| -> i128 { + h.env.as_contract(&h.repo, || { + h.env + .storage() + .persistent() + .get(&crate::DataKey::BorrowerDue(h.cash_borrower.clone())) + .unwrap_or(0) + }) + }; + assert!(due(&h) > 0, "settling records the trade's leg-2 ceiling"); + + h.env + .ledger() + .set_timestamp(maturity + repo.maturity_grace_seconds()); + repo.default_at_maturity(&h.cash_lender, &id); + + assert_eq!(due(&h), 0, "a trade defaulted at maturity no longer counts as owed"); +} + #[test] fn default_at_maturity_surplus() { let h = setup();