Skip to content

Commit

Permalink
Remove productOnly only - use isDeposit instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Nov 14, 2024
1 parent 1ea0727 commit f70a21d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 47 deletions.
29 changes: 15 additions & 14 deletions src/xrpld/app/misc/AMMHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,21 +695,21 @@ 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(
Rules const& rules,
std::function<Number()>&& noRoundCb,
STAmount const& balance,
std::function<Number()>&& productCb,
bool productOnly,
IsDeposit isDeposit);

/** Round AMM deposit/withdrawal LPToken amount. Deposit/withdrawal formulas
Expand All @@ -727,21 +727,22 @@ 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(
Rules const& rules,
std::function<Number()>&& noRoundCb,
STAmount const& lptAMMBalance,
std::function<Number()>&& productCb,
bool productOnly,
IsDeposit isDeposit);

/* Next two functions adjust asset in/out amount to factor in the adjusted
Expand Down
14 changes: 5 additions & 9 deletions src/xrpld/app/misc/detail/AMMHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,16 @@ getRoundedAsset(
std::function<Number()>&& noRoundCb,
STAmount const& balance,
std::function<Number()>&& 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
Expand All @@ -329,15 +326,14 @@ getRoundedLPTokens(
std::function<Number()>&& noRoundCb,
STAmount const& lptAMMBalance,
std::function<Number()>&& productCb,
bool productOnly,
IsDeposit isDeposit)
{
if (!rules.enabled(fixAMMv1_3))
return toSTAmount(lptAMMBalance.issue(), noRoundCb());

auto const tokens = [&] {
auto const rm = detail::getLPTokenRounding(isDeposit);
if (productOnly)
if (isDeposit == IsDeposit::Yes)
{
NumberRoundModeGuard g(rm);
return toSTAmount(lptAMMBalance.issue(), productCb(), rm);
Expand Down
14 changes: 2 additions & 12 deletions src/xrpld/app/tx/detail/AMMDeposit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 2 additions & 12 deletions src/xrpld/app/tx/detail/AMMWithdraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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(
Expand Down

0 comments on commit f70a21d

Please sign in to comment.