20#include <xrpld/app/misc/AMMHelpers.h>
28 Issue const& lptIssue)
30 auto const tokens =
root2(asset1 * asset2);
49 Number const r = asset1Deposit / asset1Balance;
50 auto const c =
root2(f2 * f2 + r / f1) - f2;
51 auto const t = lptAMMBalance * (r - c) / (1 + c);
75 auto const t1 = lpTokens / lptAMMBalance;
76 auto const t2 = 1 + t1;
77 auto const d = f2 - t1 / t2;
78 auto const a = 1 / (t2 * t2);
79 auto const b = 2 * d / t2 - 1 / f1;
80 auto const c = d * d - f2 * f2;
96 Number const fr = asset1Withdraw / asset1Balance;
97 auto const f1 =
getFee(tfee);
98 auto const c = fr * f1 + 2 - f1;
99 auto const t = lptAMMBalance * (c -
root2(c * c - 4 * fr)) / 2;
120 auto const f =
getFee(tfee);
121 Number const t1 = lpTokens / lptAMMBalance;
122 auto const b = assetBalance * (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
142 return (lptAMMBalance + lpTokens) - lptAMMBalance;
143 return (lpTokens - lptAMMBalance) + lptAMMBalance;
156 auto const lpTokensActual =
159 if (lpTokensActual == beast::zero)
161 auto const amount2Opt =
166 if (lpTokensActual < lpTokens)
168 bool const ammRoundingEnabled = [&]() {
170 rules && rules->enabled(fixAMMv1_1))
178 Number const fr = lpTokensActual / lpTokens;
180 auto const amount2Actual =
182 if (!ammRoundingEnabled)
184 amountActual < amount ? amountActual : amount,
185 amount2Actual < amount2 ? amount2Actual : amount2,
189 amountActual, amount2Actual, lpTokensActual);
193 auto const amountActual = [&]() {
196 amountBalance, lptAMMBalance, lpTokensActual, tfee);
197 else if (!ammRoundingEnabled)
199 amountBalance, lptAMMBalance, lpTokens, tfee);
202 amountBalance, lptAMMBalance, lpTokensActual, tfee);
204 if (!ammRoundingEnabled)
205 return amountActual < amount
213 lpTokensActual == lpTokens,
214 "ripple::adjustAmountsByLPTokens : LP tokens match actual");
216 return {amount, amount2, lpTokensActual};
222 return (-b +
root2(b * b - 4 * a * c)) / (2 * a);
229 auto const d = b * b - 4 * a * c;
235 return (2 * c) / (-b -
root2(d));
237 return (2 * c) / (-b +
root2(d));
A currency issued by an account.
static rounding_mode setround(rounding_mode mode)
Issue const & issue() const
T make_optional(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
STAmount lpTokensIn(STAmount const &asset1Balance, STAmount const &asset1Deposit, STAmount const &lptAMMBalance, std::uint16_t tfee)
Calculate LP Tokens given asset's deposit amount.
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, bool isDeposit)
Adjust LP tokens to deposit/withdraw.
std::optional< Number > solveQuadraticEqSmallest(Number const &a, Number const &b, Number const &c)
Solve quadratic equation to find takerGets or takerPays.
Number solveQuadraticEq(Number const &a, Number const &b, Number const &c)
Positive solution for quadratic equation: x = (-b + sqrt(b**2 + 4*a*c))/(2*a)
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
STAmount lpTokensOut(STAmount const &asset1Balance, STAmount const &asset1Withdraw, STAmount const &lptAMMBalance, std::uint16_t tfee)
Calculate LP Tokens given asset's withdraw amount.
STAmount ammAssetIn(STAmount const &asset1Balance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Calculate asset deposit given LP Tokens.
Number square(Number const &n)
Return square of n.
std::tuple< STAmount, std::optional< STAmount >, STAmount > adjustAmountsByLPTokens(STAmount const &amountBalance, STAmount const &amount, std::optional< STAmount > const &amount2, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee, bool isDeposit)
Calls adjustLPTokens() and adjusts deposit or withdraw amounts if the adjusted LP tokens are less tha...
Number feeMult(std::uint16_t tfee)
Get fee multiplier (1 - tfee) @tfee trading fee in basis points.
Number getFee(std::uint16_t tfee)
Convert to the fee from the basis points.
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
std::optional< Rules > const & getCurrentTransactionRules()
Number feeMultHalf(std::uint16_t tfee)
Get fee multiplier (1 - tfee / 2) @tfee trading fee in basis points.
STAmount withdrawByTokens(STAmount const &assetBalance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Calculate asset withdrawal by tokens.