1#include <xrpld/app/misc/AMMHelpers.h>
15 auto const tokens =
root2(asset1 * asset2);
34 Number const r = asset1Deposit / asset1Balance;
35 auto const c =
root2(f2 * f2 + r / f1) - f2;
38 auto const t = lptAMMBalance * (r - c) / (1 + c);
44 auto const frac = (r - c) / (1 + c);
69 auto const t1 = lpTokens / lptAMMBalance;
70 auto const t2 = 1 + t1;
71 auto const d = f2 - t1 / t2;
72 auto const a = 1 / (t2 * t2);
73 auto const b = 2 * d / t2 - 1 / f1;
74 auto const c = d * d - f2 * f2;
99 Number const fr = asset1Withdraw / asset1Balance;
100 auto const f1 =
getFee(tfee);
101 auto const c = fr * f1 + 2 - f1;
104 auto const t = lptAMMBalance * (c -
root2(c * c - 4 * fr)) / 2;
110 auto const frac = (c -
root2(c * c - 4 * fr)) / 2;
132 auto const f =
getFee(tfee);
133 Number const t1 = lpTokens / lptAMMBalance;
136 auto const b = assetBalance * (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
142 auto const frac = (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
163 return (lptAMMBalance + lpTokens) - lptAMMBalance;
164 return (lpTokens - lptAMMBalance) + lptAMMBalance;
181 auto const lpTokensActual =
184 if (lpTokensActual == beast::zero)
186 auto const amount2Opt =
191 if (lpTokensActual < lpTokens)
193 bool const ammRoundingEnabled = [&]() {
195 rules && rules->enabled(fixAMMv1_1))
203 Number const fr = lpTokensActual / lpTokens;
205 auto const amount2Actual =
207 if (!ammRoundingEnabled)
209 amountActual < amount ? amountActual : amount,
210 amount2Actual < amount2 ? amount2Actual : amount2,
214 amountActual, amount2Actual, lpTokensActual);
218 auto const amountActual = [&]() {
221 amountBalance, lptAMMBalance, lpTokensActual, tfee);
222 else if (!ammRoundingEnabled)
224 amountBalance, lptAMMBalance, lpTokens, tfee);
227 amountBalance, lptAMMBalance, lpTokensActual, tfee);
229 if (!ammRoundingEnabled)
230 return amountActual < amount
238 lpTokensActual == lpTokens,
239 "ripple::adjustAmountsByLPTokens : LP tokens match actual");
241 return {amount, amount2, lpTokensActual};
247 return (-b +
root2(b * b - 4 * a * c)) / (2 * a);
254 auto const d = b * b - 4 * a * c;
260 return (2 * c) / (-b -
root2(d));
262 return (2 * c) / (-b +
root2(d));
269 auto const t = amount * frac;
281 if (!rules.
enabled(fixAMMv1_3))
286 return multiply(balance, productCb(), rm);
298 if (!rules.
enabled(fixAMMv1_3))
302 auto const tokens =
multiply(balance, frac, rm);
314 if (!rules.
enabled(fixAMMv1_3))
317 auto const tokens = [&] {
324 return multiply(lptAMMBalance, productCb(), rm);
338 if (!rules.
enabled(fixAMMv1_3))
339 return {tokens, amount};
340 auto assetAdj =
ammAssetIn(balance, lptAMMBalance, tokens, tfee);
341 auto tokensAdj = tokens;
345 if (assetAdj > amount)
347 auto const adjAmount = amount - (assetAdj - amount);
348 auto const t =
lpTokensOut(balance, adjAmount, lptAMMBalance, tfee);
350 assetAdj =
ammAssetIn(balance, lptAMMBalance, tokensAdj, tfee);
352 return {tokensAdj,
std::min(amount, assetAdj)};
364 if (!rules.
enabled(fixAMMv1_3))
365 return {tokens, amount};
366 auto assetAdj =
ammAssetOut(balance, lptAMMBalance, tokens, tfee);
367 auto tokensAdj = tokens;
371 if (assetAdj > amount)
373 auto const adjAmount = amount - (assetAdj - amount);
374 auto const t =
lpTokensIn(balance, adjAmount, lptAMMBalance, tfee);
376 assetAdj =
ammAssetOut(balance, lptAMMBalance, tokensAdj, tfee);
378 return {tokensAdj,
std::min(amount, assetAdj)};
388 if (!rules.
enabled(fixAMMv1_3))
390 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.