Skip to content

fix(repo-dvp): make the borrower's leg-2 allowance additive across settled trades - #1083

Merged
DevTobis merged 4 commits into
DevTobis:mainfrom
Salmatcre8:fix/29-additive-borrower-allowance
Oct 23, 2026
Merged

DevTobis merged 4 commits into
DevTobis:mainfrom
Salmatcre8:fix/29-additive-borrower-allowance

Conversation

@Salmatcre8

Copy link
Copy Markdown
Contributor

Summary

Implements #29 with the issue's first option — an additive allowance:

  • accept_and_settle approves the sum of the borrower's outstanding leg-2 due ceilings. The sum lives in contract storage (DataKey::BorrowerDue(Address), added on settle, released on unwind and default_close); each ceiling is total_due(trade, proposed_at), the same deterministic figure the old single approve used (it is an argument to a nested authorized call, so it must come out identical at simulation and execution).
  • The allowance expiry is never shortened: a tracked BorrowerDueExpiry keeps the furthest expiry granted, so a later-settling, earlier-maturing trade cannot cut the window an earlier trade's keeper unwind still needs.
  • A non-borrower unwind consumes its own trade's share of the summed allowance via transfer_from; a defaulted trade's unconsumed share stays in the live allowance only until the next settle re-approves the accurate sum or the allowance expires — shrinking it would need the borrower's own signature. The module docs and a new AUDIT_NOTES.md bullet describe the model and this residue, per the acceptance criteria.

Tests (SAC-harness module, keeper-driven)

  • The issue's reproduction: borrower settles due≈90 then due≈50; the keeper unwinds trade 1 then trade 2. Verified failing on the unfixed code (the summed-allowance assertion trips exactly as the issue predicts) and passing now. Residual borrower→contract allowance after the last close is 0 (settles happen in the proposal ledger, so ceilings equal dues exactly).
  • Reverse size order and reverse unwind order, same zero residual.
  • A default releases its share: crash → call_margin → lapse → default_close, then the next settle approves exactly its own due, not the stale sum.

Verification

cargo test -p ballast-repo-dvp: 22/22. Full contracts workspace: 131 passed, 0 failed.

Heads-up: #31 (PR #1079) and #32 (PR #1080) also touch contracts/repo-dvp/src from the sibling account's assignments; all three are based on main and whichever lands later gets a prompt rebase.

Closes #29

…ttled trades

SEP-41 approve replaces the (owner, spender) allowance, so a
borrower's second settled trade overwrote the first one's leg-2
allowance: at maturity the keeper's or lender's unwind(1) called
transfer_from for more than the remaining allowance and reverted,
leaving only the borrower's own signature able to close trade 1 —
in either size order. accept_and_settle now approves the sum of the
borrower's outstanding leg-2 due ceilings, tracked per borrower in
contract storage (added on settle, released on unwind and
default_close), and the allowance expiry is never shortened below
what an earlier-settled trade still needs. A non-borrower unwind
consumes its own share via transfer_from; a default's unconsumed
share is replaced by the next settle's accurate sum or dies at
expiry, since shrinking the live allowance would need the borrower's
signature. Module docs and AUDIT_NOTES.md describe the model and its
residue.

The reproduction test fails on the previous code exactly as the
issue describes (verified by running it against the unfixed lib) and
passes now; reverse order and sizes covered; residual allowance
after the last close is zero; a default releases its share so the
next settle approves exactly its own due.

Closes DevTobis#29
@drips-wave

drips-wave Bot commented Oct 19, 2026

Copy link
Copy Markdown

@Salmatcre8 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Salmatcre8
Salmatcre8 marked this pull request as ready for review October 20, 2026 09:37
@Salmatcre8
Salmatcre8 requested a review from DevTobis as a code owner October 20, 2026 09:37
@DevTobis
DevTobis merged commit ac410df into DevTobis:main Oct 23, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Borrower's leg-2 allowance is overwritten by a second settled repo, breaking keeper unwind

2 participants