20#include <xrpld/app/misc/AMMHelpers.h>
28 Issue const& lptIssue)
34 auto const tokens =
root2(asset1 * asset2);
53 Number const r = asset1Deposit / asset1Balance;
54 auto const c =
root2(f2 * f2 + r / f1) - f2;
57 auto const t = lptAMMBalance * (r - c) / (1 + c);
63 auto const frac = (r - c) / (1 + c);
88 auto const t1 = lpTokens / lptAMMBalance;
89 auto const t2 = 1 + t1;
90 auto const d = f2 - t1 / t2;
91 auto const a = 1 / (t2 * t2);
92 auto const b = 2 * d / t2 - 1 / f1;
93 auto const c = d * d - f2 * f2;
118 Number const fr = asset1Withdraw / asset1Balance;
119 auto const f1 =
getFee(tfee);
120 auto const c = fr * f1 + 2 - f1;
123 auto const t = lptAMMBalance * (c -
root2(c * c - 4 * fr)) / 2;
129 auto const frac = (c -
root2(c * c - 4 * fr)) / 2;
151 auto const f =
getFee(tfee);
152 Number const t1 = lpTokens / lptAMMBalance;
155 auto const b = assetBalance * (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
161 auto const frac = (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
182 return (lptAMMBalance + lpTokens) - lptAMMBalance;
183 return (lpTokens - lptAMMBalance) + lptAMMBalance;
200 auto const lpTokensActual =
203 if (lpTokensActual == beast::zero)
205 auto const amount2Opt =
210 if (lpTokensActual < lpTokens)
212 bool const ammRoundingEnabled = [&]() {
214 rules && rules->enabled(fixAMMv1_1))
222 Number const fr = lpTokensActual / lpTokens;
224 auto const amount2Actual =
226 if (!ammRoundingEnabled)
228 amountActual < amount ? amountActual : amount,
229 amount2Actual < amount2 ? amount2Actual : amount2,
233 amountActual, amount2Actual, lpTokensActual);
237 auto const amountActual = [&]() {
240 amountBalance, lptAMMBalance, lpTokensActual, tfee);
241 else if (!ammRoundingEnabled)
243 amountBalance, lptAMMBalance, lpTokens, tfee);
246 amountBalance, lptAMMBalance, lpTokensActual, tfee);
248 if (!ammRoundingEnabled)
249 return amountActual < amount
257 lpTokensActual == lpTokens,
258 "ripple::adjustAmountsByLPTokens : LP tokens match actual");
260 return {amount, amount2, lpTokensActual};
266 return (-b +
root2(b * b - 4 * a * c)) / (2 * a);
273 auto const d = b * b - 4 * a * c;
279 return (2 * c) / (-b -
root2(d));
281 return (2 * c) / (-b +
root2(d));
288 auto const t = amount * frac;
300 if (!rules.
enabled(fixAMMv1_3))
305 return multiply(balance, productCb(), rm);
317 if (!rules.
enabled(fixAMMv1_3))
321 auto const tokens =
multiply(balance, frac, rm);
333 if (!rules.
enabled(fixAMMv1_3))
336 auto const tokens = [&] {
343 return multiply(lptAMMBalance, productCb(), rm);
357 if (!rules.
enabled(fixAMMv1_3))
358 return {tokens, amount};
359 auto assetAdj =
ammAssetIn(balance, lptAMMBalance, tokens, tfee);
360 auto tokensAdj = tokens;
364 if (assetAdj > amount)
366 auto const adjAmount = amount - (assetAdj - amount);
367 auto const t =
lpTokensOut(balance, adjAmount, lptAMMBalance, tfee);
369 assetAdj =
ammAssetIn(balance, lptAMMBalance, tokensAdj, tfee);
371 return {tokensAdj,
std::min(amount, assetAdj)};
383 if (!rules.
enabled(fixAMMv1_3))
384 return {tokens, amount};
385 auto assetAdj =
ammAssetOut(balance, lptAMMBalance, tokens, tfee);
386 auto tokensAdj = tokens;
390 if (assetAdj > amount)
392 auto const adjAmount = amount - (assetAdj - amount);
393 auto const t =
lpTokensIn(balance, adjAmount, lptAMMBalance, tfee);
395 assetAdj =
ammAssetOut(balance, lptAMMBalance, tokensAdj, tfee);
397 return {tokensAdj,
std::min(amount, assetAdj)};
407 if (!rules.
enabled(fixAMMv1_3))
409 return tokens / lptAMMBalance;
A currency issued by an account.
static rounding_mode getround()
static rounding_mode setround(rounding_mode mode)
Rules controlling protocol behavior.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Issue const & issue() const
T make_optional(T... args)
Number::rounding_mode getAssetRounding(IsDeposit isDeposit)
Number::rounding_mode getLPTokenRounding(IsDeposit isDeposit)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool isFeatureEnabled(uint256 const &feature)
std::pair< STAmount, STAmount > adjustAssetInByTokens(Rules const &rules, STAmount const &balance, STAmount const &amount, STAmount const &lptAMMBalance, STAmount const &tokens, std::uint16_t tfee)
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)
std::pair< STAmount, STAmount > adjustAssetOutByTokens(Rules const &rules, STAmount const &balance, STAmount const &amount, STAmount const &lptAMMBalance, STAmount const &tokens, std::uint16_t tfee)
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
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.
STAmount multiply(STAmount const &amount, Rate const &rate)
STAmount getRoundedLPTokens(Rules const &rules, STAmount const &balance, Number const &frac, IsDeposit isDeposit)
Round AMM deposit/withdrawal LPToken amount.
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, IsDeposit isDeposit)
Adjust LP tokens to deposit/withdraw.
Number feeMult(std::uint16_t tfee)
Get fee multiplier (1 - tfee) @tfee trading fee in basis points.
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, IsDeposit isDeposit)
Calls adjustLPTokens() and adjusts deposit or withdraw amounts if the adjusted LP tokens are less tha...
STAmount ammAssetOut(STAmount const &assetBalance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Calculate asset withdrawal by tokens.
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()
STAmount lpTokensIn(STAmount const &asset1Balance, STAmount const &asset1Withdraw, STAmount const &lptAMMBalance, std::uint16_t tfee)
Calculate LP Tokens given asset's withdraw amount.
Number feeMultHalf(std::uint16_t tfee)
Get fee multiplier (1 - tfee / 2) @tfee trading fee in basis points.
STAmount lpTokensOut(STAmount const &asset1Balance, STAmount const &asset1Deposit, STAmount const &lptAMMBalance, std::uint16_t tfee)
Calculate LP Tokens given asset's deposit amount.
STAmount getRoundedAsset(Rules const &rules, STAmount const &balance, A const &frac, IsDeposit isDeposit)
Round AMM equal deposit/withdrawal amount.
Number adjustFracByTokens(Rules const &rules, STAmount const &lptAMMBalance, STAmount const &tokens, Number const &frac)
Find a fraction of tokens after the tokens are adjusted.