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
5 changes: 5 additions & 0 deletions .changeset/kind-walls-write.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly/lib": patch
---

🩹 market: mirror fixed repay rounding in position preview
7 changes: 6 additions & 1 deletion src/market/fixedRepayPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export default function fixedRepayPosition(
const r = mulDiv(netUnassignedEarnings, k, backupSupplied);
if (r >= WAD) return min(assets + netUnassignedEarnings, totalPosition);
const x = divWad(assets, WAD - r);
if (mulWad(k, x) <= backupSupplied && x <= totalPosition) return x;
const scaledPrincipal = mulDiv(x, principal, totalPosition);
const gross = mulDiv(unassignedEarnings, min(scaledPrincipal, backupSupplied), backupSupplied);
const pos = min(assets + gross - mulWad(gross, backupFeeRate), totalPosition);
const earned = mulDiv(unassignedEarnings, min(mulDiv(pos, principal, totalPosition), backupSupplied), backupSupplied);
const repay = pos - earned + mulWad(earned, backupFeeRate);
Comment thread
cruzdanilo marked this conversation as resolved.
if (scaledPrincipal <= backupSupplied && x <= totalPosition) return pos - (repay > assets ? repay - assets : 0n);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Consider documenting the return adjustment logic.

The expression pos - (repay > assets ? repay - assets : 0n) performs a non-trivial adjustment. When repay > assets, this effectively returns pos - repay + assets, which algebraically simplifies to a complex relationship between earned amounts, fees, and the backup fee rate. A brief inline comment explaining the business logic (e.g., "adjust position to account for repayment shortfall") would aid maintainability.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Revalidate the corrected position

After this patch's one-shot subtraction, the returned position can still cost more than the caller's asset limit because lowering pos also lowers the earned-yield offset used by fixedRepayAssets. For example, with principal=7743n, fee=408n, borrowed=813061n, supplied=647081n, unassignedEarnings=298005n, backupFeeRate=450351310267204224n, lastAccrual=1n, maturity=1000, timestamp=1, and assets=119n, this branch returns 1900n, but fixedRepayAssets(..., 1900n, 1) is 120n. Please iterate/recompute until the forward calculation is <= assets rather than subtracting only the first observed excess.

Useful? React with 👍 / 👎.

return min(assets + netUnassignedEarnings, totalPosition);
}