From 941f68361a97d6531de34dda38f88c1a236ee7fc Mon Sep 17 00:00:00 2001 From: danilo neves cruz Date: Mon, 11 May 2026 20:47:20 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20market:=20mirror=20fixed=20repay?= =?UTF-8?q?=20rounding=20in=20position=20preview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/kind-walls-write.md | 5 +++++ src/market/fixedRepayPosition.ts | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .changeset/kind-walls-write.md diff --git a/.changeset/kind-walls-write.md b/.changeset/kind-walls-write.md new file mode 100644 index 0000000..b18b671 --- /dev/null +++ b/.changeset/kind-walls-write.md @@ -0,0 +1,5 @@ +--- +"@exactly/lib": patch +--- + +🩹 market: mirror fixed repay rounding in position preview diff --git a/src/market/fixedRepayPosition.ts b/src/market/fixedRepayPosition.ts index e06e079..c8d75fc 100644 --- a/src/market/fixedRepayPosition.ts +++ b/src/market/fixedRepayPosition.ts @@ -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); + if (scaledPrincipal <= backupSupplied && x <= totalPosition) return pos - (repay > assets ? repay - assets : 0n); return min(assets + netUnassignedEarnings, totalPosition); }