From f70a21d057cdc8a163bb762dbf264c57f68f4ef7 Mon Sep 17 00:00:00 2001 From: Gregory Tsipenyuk Date: Thu, 14 Nov 2024 09:36:51 -0500 Subject: [PATCH] Remove productOnly only - use isDeposit instead --- src/xrpld/app/misc/AMMHelpers.h | 29 ++++++++++++------------ src/xrpld/app/misc/detail/AMMHelpers.cpp | 14 ++++-------- src/xrpld/app/tx/detail/AMMDeposit.cpp | 14 ++---------- src/xrpld/app/tx/detail/AMMWithdraw.cpp | 14 ++---------- 4 files changed, 24 insertions(+), 47 deletions(-) diff --git a/src/xrpld/app/misc/AMMHelpers.h b/src/xrpld/app/misc/AMMHelpers.h index 31585cdcbbe..1ad8b324580 100644 --- a/src/xrpld/app/misc/AMMHelpers.h +++ b/src/xrpld/app/misc/AMMHelpers.h @@ -695,13 +695,14 @@ getRoundedAsset( return multiply(balance, frac, rm); } -/** Round AMM single deposit/withdrawal amount. In this case - * there is no a shared frac value, which is calculated the same way pre/post - * AMMv1_3. The lambda is used to delay evaluation until the function +/** Round AMM single deposit/withdrawal amount. + * The lambda's are used to delay evaluation until the function * is executed so that the calculation is not done twice. noRoundCb() is - * called if AMMv1_3 is disabled. If productOnly is true then the requested - * rounding is set and the result of productCb() is returned. Otherwise - * the balance is multiplied by productCb() with the requested rounding. + * called if AMMv1_3 is disabled. Otherwise, the rounding is set and + * the amount is: + * isDeposit is Yes - the balance multiplied by productCb() + * isDeposit is No - the result of productCb(). The rounding is + * the same for all calculations in productCb() */ STAmount getRoundedAsset( @@ -709,7 +710,6 @@ getRoundedAsset( std::function&& noRoundCb, STAmount const& balance, std::function&& productCb, - bool productOnly, IsDeposit isDeposit); /** Round AMM deposit/withdrawal LPToken amount. Deposit/withdrawal formulas @@ -727,13 +727,15 @@ getRoundedLPTokens( IsDeposit isDeposit); /** Round AMM single deposit/withdrawal LPToken amount. - * The lambda are used to delay evaluation until the function is executed + * The lambda's are used to delay evaluation until the function is executed * so that the calculations are not done twice. - * noRoundCb() is called if AMMv1_3 is disabled. If productOnly is true then - * the requested rounding is set and the result of productCb() is returned. - * Otherwise the balance is multiplied by productCb() with the requested - * rounding. The tokens are then adjusted to factor in the loss in precision - * (we only keep 16 significant digits) when adding the tokens to the balance. + * noRoundCb() is called if AMMv1_3 is disabled. Otherwise, the rounding is set + * and the lptokens are: + * if isDeposit is Yes - the result of productCb(). The rounding is + * the same for all calculations in productCb() + * if isDeposit is No - the balance multiplied by productCb() + * The lptokens are then adjusted to factor in the loss in precision + * (we only keep 16 significant digits) when adding the lptokens to the balance. */ STAmount getRoundedLPTokens( @@ -741,7 +743,6 @@ getRoundedLPTokens( std::function&& noRoundCb, STAmount const& lptAMMBalance, std::function&& productCb, - bool productOnly, IsDeposit isDeposit); /* Next two functions adjust asset in/out amount to factor in the adjusted diff --git a/src/xrpld/app/misc/detail/AMMHelpers.cpp b/src/xrpld/app/misc/detail/AMMHelpers.cpp index a72d15b7a25..2ff8d1a296e 100644 --- a/src/xrpld/app/misc/detail/AMMHelpers.cpp +++ b/src/xrpld/app/misc/detail/AMMHelpers.cpp @@ -293,19 +293,16 @@ getRoundedAsset( std::function&& noRoundCb, STAmount const& balance, std::function&& productCb, - bool productOnly, IsDeposit isDeposit) { if (!rules.enabled(fixAMMv1_3)) return toSTAmount(balance.issue(), noRoundCb()); auto const rm = detail::getAssetRounding(isDeposit); - if (productOnly) - { - NumberRoundModeGuard g(rm); - return toSTAmount(balance.issue(), productCb(), rm); - } - return multiply(balance, productCb(), rm); + if (isDeposit == IsDeposit::Yes) + return multiply(balance, productCb(), rm); + NumberRoundModeGuard g(rm); + return toSTAmount(balance.issue(), productCb(), rm); } STAmount @@ -329,7 +326,6 @@ getRoundedLPTokens( std::function&& noRoundCb, STAmount const& lptAMMBalance, std::function&& productCb, - bool productOnly, IsDeposit isDeposit) { if (!rules.enabled(fixAMMv1_3)) @@ -337,7 +333,7 @@ getRoundedLPTokens( auto const tokens = [&] { auto const rm = detail::getLPTokenRounding(isDeposit); - if (productOnly) + if (isDeposit == IsDeposit::Yes) { NumberRoundModeGuard g(rm); return toSTAmount(lptAMMBalance.issue(), productCb(), rm); diff --git a/src/xrpld/app/tx/detail/AMMDeposit.cpp b/src/xrpld/app/tx/detail/AMMDeposit.cpp index c021ab309cc..f21387e6081 100644 --- a/src/xrpld/app/tx/detail/AMMDeposit.cpp +++ b/src/xrpld/app/tx/detail/AMMDeposit.cpp @@ -962,23 +962,13 @@ AMMDeposit::singleDepositEPrice( }; auto amtProdCb = [&] { return f1 * solveQuadraticEq(a1, b1, c1); }; auto const amountDeposit = getRoundedAsset( - view.rules(), - amtNoRoundCb, - amountBalance, - amtProdCb, - /*productOnly*/ false, - IsDeposit::Yes); + view.rules(), amtNoRoundCb, amountBalance, amtProdCb, IsDeposit::Yes); if (amountDeposit <= beast::zero) return {tecAMM_FAILED, STAmount{}}; auto tokNoRoundCb = [&] { return amountDeposit / ePrice; }; auto tokProdCb = [&] { return amountDeposit / ePrice; }; auto const tokens = getRoundedLPTokens( - view.rules(), - tokNoRoundCb, - lptAMMBalance, - tokProdCb, - /*productOnly*/ true, - IsDeposit::Yes); + view.rules(), tokNoRoundCb, lptAMMBalance, tokProdCb, IsDeposit::Yes); // factor in the adjusted tokens auto const [tokensAdj, amountDepositAdj] = adjustAssetInByTokens( view.rules(), diff --git a/src/xrpld/app/tx/detail/AMMWithdraw.cpp b/src/xrpld/app/tx/detail/AMMWithdraw.cpp index d649f41b1f9..c622c13092e 100644 --- a/src/xrpld/app/tx/detail/AMMWithdraw.cpp +++ b/src/xrpld/app/tx/detail/AMMWithdraw.cpp @@ -1022,12 +1022,7 @@ AMMWithdraw::singleWithdrawEPrice( return (lptAMMBalance + ae * (f - 2)) / (lptAMMBalance * f - ae); }; auto const tokens = getRoundedLPTokens( - view.rules(), - tokNoRoundCb, - lptAMMBalance, - tokProdCb, - /*productOnly*/ false, - IsDeposit::No); + view.rules(), tokNoRoundCb, lptAMMBalance, tokProdCb, IsDeposit::No); if (tokens <= beast::zero) { if (!view.rules().enabled(fixAMMv1_3)) @@ -1039,12 +1034,7 @@ AMMWithdraw::singleWithdrawEPrice( auto amtProdCb = [&] { return tokens / ePrice; }; // the adjusted tokens are factored in auto const amountWithdraw = getRoundedAsset( - view.rules(), - amtNoRoundCb, - amount, - amtProdCb, - /*productOnly*/ true, - IsDeposit::No); + view.rules(), amtNoRoundCb, amount, amtProdCb, IsDeposit::No); if (amount == beast::zero || amountWithdraw >= amount) { return withdraw(