Merge branch 'ximinez/number-round-maxrep-down' into ximinez/number-round-maxrep

This commit is contained in:
Ed Hennis
2026-06-15 15:28:59 -04:00
committed by GitHub
2 changed files with 32 additions and 4 deletions

View File

@@ -1091,10 +1091,13 @@ AMMWithdraw::singleWithdrawEPrice(
// t = T*(T + A*E*(f - 2))/(T*f - A*E)
Number const ae = amountBalance * ePrice;
auto const f = getFee(tfee);
auto tokNoRoundCb = [&] {
return lptAMMBalance * (lptAMMBalance + ae * (f - 2)) / (lptAMMBalance * f - ae);
};
auto tokProdCb = [&] { return (lptAMMBalance + ae * (f - 2)) / (lptAMMBalance * f - ae); };
auto const denom = lptAMMBalance * f - ae;
// fixCleanup3_3_0: guard against division by zero
// when ePrice == lptAMMBalance*f/amountBalance
if (view.rules().enabled(fixCleanup3_3_0) && denom == beast::kZero)
return {tecAMM_FAILED, STAmount{}};
auto tokNoRoundCb = [&] { return lptAMMBalance * (lptAMMBalance + ae * (f - 2)) / denom; };
auto tokProdCb = [&] { return (lptAMMBalance + ae * (f - 2)) / denom; };
auto const tokensAdj =
getRoundedLPTokens(view.rules(), tokNoRoundCb, lptAMMBalance, tokProdCb, IsDeposit::No);
if (tokensAdj <= beast::kZero)

View File

@@ -2229,6 +2229,31 @@ private:
ammAlice.withdraw(alice_, XRPAmount{9'999'999'999});
BEAST_EXPECT(ammAlice.expectBalances(XRPAmount{1}, USD(10'000), IOUAmount{100}));
});
// singleWithdrawEPrice: crafted ePrice = lptAMMBalance*f/amountBalance
// makes the denominator (T*f - A*E) exactly zero.
// Pre-fixCleanup3_3_0: std::overflow_error escapes to the
// transactor backstop and is returned as tefEXCEPTION.
// Post-fixCleanup3_3_0: denominator check returns tecAMM_FAILED.
//
// Pool: USD(100)/EUR(100), baseFee=1000 (1%).
// Alice is the creator so her discounted fee is 100 (0.1%), f=0.001.
// ePrice = lptAMMBalance(100) * f(0.001) / amountBalance(100) = 0.001
testAMM(
[&](AMM& ammAlice, Env& env) {
auto const err =
env.enabled(fixCleanup3_3_0) ? Ter(tecAMM_FAILED) : Ter(tefEXCEPTION);
ammAlice.withdraw(
WithdrawArg{
.account = alice_,
.asset1Out = USD(0),
.maxEP = IOUAmount{1, -3}, // ePrice=0.001 → denom=0
.err = err});
},
{{USD(100), EUR(100)}},
1000,
std::nullopt,
{all - fixCleanup3_3_0, all});
}
void