xrpld
Loading...
Searching...
No Matches
AMMHelpers.h
1#pragma once
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/ledger/ApplyView.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/ledger/Sandbox.h>
10#include <xrpl/ledger/helpers/TokenHelpers.h>
11#include <xrpl/protocol/AMMCore.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/AmountConversions.h>
14#include <xrpl/protocol/Asset.h>
15#include <xrpl/protocol/Feature.h>
16#include <xrpl/protocol/IOUAmount.h>
17#include <xrpl/protocol/Issue.h>
18#include <xrpl/protocol/MPTAmount.h>
19#include <xrpl/protocol/Quality.h>
20#include <xrpl/protocol/Rules.h>
21#include <xrpl/protocol/STAmount.h>
22#include <xrpl/protocol/STLedgerEntry.h>
23#include <xrpl/protocol/TER.h>
24#include <xrpl/protocol/XRPAmount.h>
25
26#include <algorithm>
27#include <cstdint>
28#include <expected>
29#include <functional>
30#include <optional>
31#include <tuple>
32#include <utility>
33
34namespace xrpl {
35
36namespace detail {
37
38Number
39reduceOffer(auto const& amount)
40{
41 static Number const kReducedOfferPct(9999, -4);
42
43 // Make sure the result is always less than amount or zero.
45 return amount * kReducedOfferPct;
46}
47
48} // namespace detail
49
50enum class IsDeposit : bool { No = false, Yes = true };
51
53
60STAmount
61ammLPTokens(STAmount const& asset1, STAmount const& asset2, Asset const& lptIssue);
62
71STAmount
73 STAmount const& asset1Balance,
74 STAmount const& asset1Deposit,
75 STAmount const& lptAMMBalance,
76 std::uint16_t tfee);
77
86STAmount
88 STAmount const& asset1Balance,
89 STAmount const& lptAMMBalance,
90 STAmount const& lpTokens,
91 std::uint16_t tfee);
92
102STAmount
104 STAmount const& asset1Balance,
105 STAmount const& asset1Withdraw,
106 STAmount const& lptAMMBalance,
107 std::uint16_t tfee);
108
117STAmount
119 STAmount const& assetBalance,
120 STAmount const& lptAMMBalance,
121 STAmount const& lpTokens,
122 std::uint16_t tfee);
123
132inline bool
133withinRelativeDistance(Quality const& calcQuality, Quality const& reqQuality, Number const& dist)
134{
135 if (calcQuality == reqQuality)
136 return true;
137 auto const [min, max] = std::minmax(calcQuality, reqQuality);
138 // Relative distance is (max - min)/max. Can't use basic operations
139 // on Quality. Have to use Quality::rate() instead, which
140 // is inverse of quality: (1/max.rate - 1/min.rate)/(1/max.rate)
141 return ((min.rate() - max.rate()) / min.rate()) < dist;
142}
143
152template <typename Amt>
153 requires(
157bool
158withinRelativeDistance(Amt const& calc, Amt const& req, Number const& dist)
159{
160 if (calc == req)
161 return true;
162 auto const [min, max] = std::minmax(calc, req);
163 return ((max - min) / max) < dist;
164}
165
171solveQuadraticEqSmallest(Number const& a, Number const& b, Number const& c);
172
197template <typename TIn, typename TOut>
200 TAmounts<TIn, TOut> const& pool,
201 Quality const& targetQuality,
202 std::uint16_t const& tfee)
203{
204 if (targetQuality.rate() == beast::kZero)
205 return std::nullopt;
206
208 auto const f = feeMult(tfee);
209 auto const a = 1;
210 auto const b = pool.in * (1 - 1 / f) / targetQuality.rate() - 2 * pool.out;
211 auto const c = pool.out * pool.out - (pool.in * pool.out) / targetQuality.rate();
212
213 auto nTakerGets = solveQuadraticEqSmallest(a, b, c);
214 if (!nTakerGets || *nTakerGets <= 0)
215 return std::nullopt; // LCOV_EXCL_LINE
216
217 auto const nTakerGetsConstraint = pool.out - pool.in / (targetQuality.rate() * f);
218 if (nTakerGetsConstraint <= 0)
219 return std::nullopt;
220
221 // Select the smallest to maximize the quality
222 if (nTakerGetsConstraint < *nTakerGets)
223 nTakerGets = nTakerGetsConstraint;
224
225 auto const getAmounts = [&pool, &tfee](Number const& nTakerGetsProposed) {
226 // Round downward to minimize the offer and to maximize the quality.
227 // This has the most impact when takerGets is integral.
228 auto const takerGets =
229 toAmount<TOut>(getAsset(pool.out), nTakerGetsProposed, Number::RoundingMode::Downward);
230 return TAmounts<TIn, TOut>{swapAssetOut(pool, takerGets, tfee), takerGets};
231 };
232
233 // Try to reduce the offer size to improve the quality.
234 // The quality might still not match the targetQuality for a tiny offer.
235 auto amounts = getAmounts(*nTakerGets);
236 if (Quality{amounts} < targetQuality)
237 return getAmounts(detail::reduceOffer(amounts.out));
238 return amounts;
239}
240
265template <typename TIn, typename TOut>
268 TAmounts<TIn, TOut> const& pool,
269 Quality const& targetQuality,
270 std::uint16_t tfee)
271{
272 if (targetQuality.rate() == beast::kZero)
273 return std::nullopt;
274
276 auto const f = feeMult(tfee);
277 auto const& a = f;
278 auto const b = pool.in * (1 + f);
279 auto const c = pool.in * pool.in - pool.in * pool.out * targetQuality.rate();
280
281 auto nTakerPays = solveQuadraticEqSmallest(a, b, c);
282 if (!nTakerPays || nTakerPays <= 0)
283 return std::nullopt; // LCOV_EXCL_LINE
284
285 auto const nTakerPaysConstraint = pool.out * targetQuality.rate() - pool.in / f;
286 if (nTakerPaysConstraint <= 0)
287 return std::nullopt;
288
289 // Select the smallest to maximize the quality
290 if (nTakerPaysConstraint < *nTakerPays)
291 nTakerPays = nTakerPaysConstraint;
292
293 auto const getAmounts = [&pool, &tfee](Number const& nTakerPaysProposed) {
294 // Round downward to minimize the offer and to maximize the quality.
295 // This has the most impact when takerPays is integral.
296 auto const takerPays =
297 toAmount<TIn>(getAsset(pool.in), nTakerPaysProposed, Number::RoundingMode::Downward);
298 return TAmounts<TIn, TOut>{takerPays, swapAssetIn(pool, takerPays, tfee)};
299 };
300
301 // Try to reduce the offer size to improve the quality.
302 // The quality might still not match the targetQuality for a tiny offer.
303 auto amounts = getAmounts(*nTakerPays);
304 if (Quality{amounts} < targetQuality)
305 return getAmounts(detail::reduceOffer(amounts.in));
306 return amounts;
307}
308
326template <typename TIn, typename TOut>
329 TAmounts<TIn, TOut> const& pool,
330 Quality const& quality,
331 std::uint16_t tfee,
333{
334 auto amounts = [&] {
335 bool const inIntegral = getAsset(pool.in).integral();
336 bool const outIntegral = getAsset(pool.out).integral();
337
338 // Preserve historical behavior for fractional pairs and XRP/IOU-style
339 // one-integral-side pairs. For two integral assets, pick the side whose
340 // minimum unit is economically coarser at this quality.
341 //
342 // Quality::rate() is input units per output unit, so one output unit is
343 // coarser when it costs at least one input unit. Ties use takerGets,
344 // matching the historical XRP-output behavior.
345 if (outIntegral && (!inIntegral || Number(quality.rate()) >= 1))
346 return getAMMOfferStartWithTakerGets(pool, quality, tfee);
347 return getAMMOfferStartWithTakerPays(pool, quality, tfee);
348 }();
349 if (!amounts)
350 {
351 JLOG(j.trace()) << "changeSpotPrice calc failed: " << to_string(pool.in) << " "
352 << to_string(pool.out) << " " << quality << " " << tfee;
353 return std::nullopt;
354 }
355
356 if (Quality{*amounts} < quality)
357 {
358 JLOG(j.error()) << "changeSpotPriceQuality failed: " << to_string(pool.in) << " "
359 << to_string(pool.out) << " " << quality << " " << tfee << " "
360 << to_string(amounts->in) << " " << to_string(amounts->out);
361 return std::nullopt;
362 }
363
364 JLOG(j.trace()) << "changeSpotPriceQuality succeeded: " << to_string(pool.in) << " "
365 << to_string(pool.out) << " "
366 << " " << quality << " " << tfee << " " << to_string(amounts->in) << " "
367 << to_string(amounts->out);
368
369 return amounts;
370}
371
384
395template <typename TIn, typename TOut>
396TOut
397swapAssetIn(TAmounts<TIn, TOut> const& pool, TIn const& assetIn, std::uint16_t tfee)
398{
399 // set rounding to always favor the amm. Clip to zero.
400 // calculate:
401 // pool.out -
402 // (pool.in * pool.out) / (pool.in + assetIn * feeMult(tfee)),
403 // and explicitly set the rounding modes
404 // Favoring the amm means we should:
405 // minimize:
406 // pool.out -
407 // (pool.in * pool.out) / (pool.in + assetIn * feeMult(tfee)),
408 // maximize:
409 // (pool.in * pool.out) / (pool.in + assetIn * feeMult(tfee)),
410 // (pool.in * pool.out)
411 // minimize:
412 // (pool.in + assetIn * feeMult(tfee)),
413 // minimize:
414 // assetIn * feeMult(tfee)
415 // feeMult is: (1-fee), fee is tfee/100000
416 // minimize:
417 // 1-fee
418 // maximize:
419 // fee
421
423 auto const numerator = pool.in * pool.out;
424 auto const fee = getFee(tfee);
425
427 auto const denom = pool.in + assetIn * (1 - fee);
428
429 if (denom.signum() <= 0)
430 return toAmount<TOut>(getAsset(pool.out), 0);
431
433 auto const ratio = numerator / denom;
434
436 auto const swapOut = pool.out - ratio;
437
438 if (swapOut.signum() < 0)
439 return toAmount<TOut>(getAsset(pool.out), 0);
440
442}
443
454template <typename TIn, typename TOut>
455TIn
456swapAssetOut(TAmounts<TIn, TOut> const& pool, TOut const& assetOut, std::uint16_t tfee)
457{
458 // set rounding to always favor the amm. Clip to zero.
459 // calculate:
460 // ((pool.in * pool.out) / (pool.out - assetOut) - pool.in) /
461 // (1-tfee/100000)
462 // maximize:
463 // ((pool.in * pool.out) / (pool.out - assetOut) - pool.in)
464 // maximize:
465 // (pool.in * pool.out) / (pool.out - assetOut)
466 // maximize:
467 // (pool.in * pool.out)
468 // minimize
469 // (pool.out - assetOut)
470 // minimize:
471 // (1-tfee/100000)
472 // maximize:
473 // tfee/100000
474
476
478 auto const numerator = pool.in * pool.out;
479
481 auto const denom = pool.out - assetOut;
482 if (denom.signum() <= 0)
483 {
484 return toMaxAmount<TIn>(getAsset(pool.in));
485 }
486
488 auto const ratio = numerator / denom;
489 auto const numerator2 = ratio - pool.in;
490 auto const fee = getFee(tfee);
491
493 auto const feeMult = 1 - fee;
494
496 auto const swapIn = numerator2 / feeMult;
497 if (swapIn.signum() < 0)
498 return toAmount<TIn>(getAsset(pool.in), 0);
499
501}
502
506Number
507square(Number const& n);
508
521STAmount
522adjustLPTokens(STAmount const& lptAMMBalance, STAmount const& lpTokens, IsDeposit isDeposit);
523
538 STAmount const& amountBalance,
539 STAmount const& amount,
540 std::optional<STAmount> const& amount2,
541 STAmount const& lptAMMBalance,
542 STAmount const& lpTokens,
543 std::uint16_t tfee,
544 IsDeposit isDeposit);
545
550Number
551solveQuadraticEq(Number const& a, Number const& b, Number const& c);
552
553STAmount
554multiply(STAmount const& amount, Number const& frac, Number::RoundingMode rm);
555
556namespace detail {
557
560{
561 // Minimize on deposit, maximize on withdraw to ensure
562 // AMM invariant sqrt(poolAsset1 * poolAsset2) >= LPTokensBalance
565}
566
569{
570 // Maximize on deposit, minimize on withdraw to ensure
571 // AMM invariant sqrt(poolAsset1 * poolAsset2) >= LPTokensBalance
572 return isDeposit == IsDeposit::Yes ? Number::RoundingMode::Upward
574}
575
576} // namespace detail
577
584template <typename A>
585STAmount
586getRoundedAsset(Rules const& rules, STAmount const& balance, A const& frac, IsDeposit isDeposit)
587{
588 if (!rules.enabled(fixAMMv1_3))
589 {
590 if constexpr (std::is_same_v<A, STAmount>)
591 {
592 return multiply(balance, frac, balance.asset());
593 }
594 else
595 {
596 return toSTAmount(balance.asset(), balance * frac);
597 }
598 }
599 auto const rm = detail::getAssetRounding(isDeposit);
600 return multiply(balance, frac, rm);
601}
602
613STAmount
615 Rules const& rules,
616 std::function<Number()> const& noRoundCb,
617 STAmount const& balance,
618 std::function<Number()> const& productCb,
619 IsDeposit isDeposit);
620
629STAmount
631 Rules const& rules,
632 STAmount const& balance,
633 Number const& frac,
634 IsDeposit isDeposit);
635
648STAmount
650 Rules const& rules,
651 std::function<Number()> const& noRoundCb,
652 STAmount const& lptAMMBalance,
653 std::function<Number()> const& productCb,
654 IsDeposit isDeposit);
655
656/* Next two functions adjust asset in/out amount to factor in the adjusted
657 * lptokens. The lptokens are calculated from the asset in/out. The lptokens are
658 * then adjusted to factor in the loss in precision. The adjusted lptokens might
659 * be less than the initially calculated tokens. Therefore, the asset in/out
660 * must be adjusted. The rounding might result in the adjusted amount being
661 * greater than the original asset in/out amount. If this happens,
662 * then the original amount is reduced by the difference in the adjusted amount
663 * and the original amount. The actual tokens and the actual adjusted amount
664 * are then recalculated. The minimum of the original and the actual
665 * adjusted amount is returned.
666 */
669 Rules const& rules,
670 STAmount const& balance,
671 STAmount const& amount,
672 STAmount const& lptAMMBalance,
673 STAmount const& tokens,
674 std::uint16_t tfee);
677 Rules const& rules,
678 STAmount const& balance,
679 STAmount const& amount,
680 STAmount const& lptAMMBalance,
681 STAmount const& tokens,
682 std::uint16_t tfee);
683
688Number
690 Rules const& rules,
691 STAmount const& lptAMMBalance,
692 STAmount const& tokens,
693 Number const& frac);
694
700 ReadView const& view,
701 AccountID const& ammAccountID,
702 Asset const& asset1,
703 Asset const& asset2,
704 FreezeHandling freezeHandling,
705 AuthHandling authHandling,
706 beast::Journal const j);
707
715TER
716checkAMMPrecisionLoss(Number const& poolProductMean, STAmount const& newLPTokenBalance);
717
725TER
727 ReadView const& view,
728 AccountID const& ammAccountID,
729 Asset const& asset1,
730 Asset const& asset2,
731 STAmount const& newLPTokenBalance,
732 beast::Journal const j);
733
739std::expected<std::tuple<STAmount, STAmount, STAmount>, TER>
741 ReadView const& view,
742 SLE const& ammSle,
743 std::optional<Asset> const& optAsset1,
744 std::optional<Asset> const& optAsset2,
745 FreezeHandling freezeHandling,
746 AuthHandling authHandling,
747 beast::Journal const j);
748
752STAmount
754 ReadView const& view,
755 Asset const& asset1,
756 Asset const& asset2,
757 AccountID const& ammAccount,
758 AccountID const& lpAccount,
759 beast::Journal const j);
760
761STAmount
763 ReadView const& view,
764 SLE const& ammSle,
765 AccountID const& lpAccount,
766 beast::Journal const j);
767
774getTradingFee(ReadView const& view, SLE const& ammSle, AccountID const& account);
775
779STAmount
780ammAccountHolds(ReadView const& view, AccountID const& ammAccountID, Asset const& asset);
781
786TER
787deleteAMMAccount(Sandbox& view, Asset const& asset, Asset const& asset2, beast::Journal j);
788
792void
794 ApplyView const& view,
795 SLE::pointer const& ammSle,
796 AccountID const& account,
797 Asset const& lptAsset,
798 std::uint16_t tfee);
799
805std::expected<bool, TER>
806isOnlyLiquidityProvider(ReadView const& view, Issue const& ammIssue, AccountID const& lpAccount);
807
813std::expected<bool, TER>
815 Sandbox& sb,
816 STAmount const& lpTokens,
817 SLE::pointer const& ammSle,
818 AccountID const& account);
819
820} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream error() const
Definition Journal.h:362
Stream trace() const
Severity stream access functions.
Definition Journal.h:338
bool integral() const
Definition Asset.h:133
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
static RoundingMode setround(RoundingMode inMode)
static RoundingMode getround()
Represents the logical ratio of output currency to input currency.
Definition Quality.h:90
STAmount rate() const
Returns the quality as STAmount.
Definition Quality.h:162
Rules controlling protocol behavior.
Definition Rules.h:40
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
Asset const & asset() const
Definition STAmount.h:496
std::shared_ptr< STLedgerEntry > pointer
T is_same_v
T minmax(T... args)
constexpr Zero kZero
Definition Zero.h:30
Number reduceOffer(auto const &amount)
Definition AMMHelpers.h:39
Number::RoundingMode getLPTokenRounding(IsDeposit isDeposit)
Definition AMMHelpers.h:559
Number::RoundingMode getAssetRounding(IsDeposit isDeposit)
Definition AMMHelpers.h:568
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
STAmount ammLPHolds(ReadView const &view, Asset const &asset1, Asset const &asset2, AccountID const &ammAccount, AccountID const &lpAccount, beast::Journal const j)
Get the balance of LP tokens.
FreezeHandling
Controls the treatment of frozen account balances.
Number adjustFracByTokens(Rules const &rules, STAmount const &lptAMMBalance, STAmount const &tokens, Number const &frac)
Find a fraction of tokens after the tokens are adjusted.
T toMaxAmount(Asset const &asset)
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Asset const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
std::expected< bool, TER > isOnlyLiquidityProvider(ReadView const &view, Issue const &ammIssue, AccountID const &lpAccount)
Return true if the Liquidity Provider is the only AMM provider, false otherwise.
IsDeposit
Definition AMMHelpers.h:50
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, IsDeposit isDeposit)
Adjust LP tokens to deposit/withdraw.
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).
TOut swapAssetIn(TAmounts< TIn, TOut > const &pool, TIn const &assetIn, std::uint16_t tfee)
AMM pool invariant - the product (A * B) after swap in/out has to remain at least the same: (A + in) ...
Definition AMMHelpers.h:397
STAmount ammAssetOut(STAmount const &assetBalance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Calculate asset withdrawal by tokens.
STAmount getRoundedLPTokens(Rules const &rules, STAmount const &balance, Number const &frac, IsDeposit isDeposit)
Round AMM deposit/withdrawal LPToken amount.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
std::pair< STAmount, STAmount > ammPoolHolds(ReadView const &view, AccountID const &ammAccountID, Asset const &asset1, Asset const &asset2, FreezeHandling freezeHandling, AuthHandling authHandling, beast::Journal const j)
Get AMM pool balances.
T toAmount(STAmount const &amt)=delete
STLedgerEntry SLE
@ Yes
We have consensus along with the network.
@ No
We do not have consensus.
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< TAmounts< TIn, TOut > > getAMMOfferStartWithTakerGets(TAmounts< TIn, TOut > const &pool, Quality const &targetQuality, std::uint16_t const &tfee)
Generate AMM offer starting with takerGets when AMM pool from the payment perspective is IOU(in)/XRP(...
Definition AMMHelpers.h:199
STAmount ammAccountHolds(ReadView const &view, AccountID const &ammAccountID, Asset const &asset)
Returns total amount held by AMM for the given token.
Asset getAsset(T const &amt)
std::pair< STAmount, STAmount > adjustAssetOutByTokens(Rules const &rules, STAmount const &balance, STAmount const &amount, STAmount const &lptAMMBalance, STAmount const &tokens, std::uint16_t tfee)
TER deleteAMMAccount(Sandbox &view, Asset const &asset, Asset const &asset2, beast::Journal j)
Delete trustlines to AMM.
std::optional< TAmounts< TIn, TOut > > getAMMOfferStartWithTakerPays(TAmounts< TIn, TOut > const &pool, Quality const &targetQuality, std::uint16_t tfee)
Generate AMM offer starting with takerPays when AMM pool from the payment perspective is XRP(in)/IOU(...
Definition AMMHelpers.h:267
STAmount getRoundedAsset(Rules const &rules, STAmount const &balance, A const &frac, IsDeposit isDeposit)
Round AMM equal deposit/withdrawal amount.
Definition AMMHelpers.h:586
AuthHandling
Controls the treatment of unauthorized MPT balances.
Number feeMult(std::uint16_t tfee)
Get fee multiplier (1 - tfee) @tfee trading fee in basis points.
Definition AMMCore.h:104
TER checkAMMPrecisionLoss(Number const &poolProductMean, STAmount const &newLPTokenBalance)
Check AMM pool product invariant after an AMM operation that changes LP tokens (deposit/withdraw/claw...
std::optional< TAmounts< TIn, TOut > > changeSpotPriceQuality(TAmounts< TIn, TOut > const &pool, Quality const &quality, std::uint16_t tfee, beast::Journal j)
Generate AMM offer so that either updated Spot Price Quality (SPQ) is equal to LOB quality (in this c...
Definition AMMHelpers.h:328
std::optional< Number > solveQuadraticEqSmallest(Number const &a, Number const &b, Number const &c)
Solve quadratic equation to find takerGets or takerPays.
std::expected< bool, TER > verifyAndAdjustLPTokenBalance(Sandbox &sb, STAmount const &lpTokens, SLE::pointer const &ammSle, AccountID const &account)
Due to rounding, the LPTokenBalance of the last LP might not match the LP's trustline balance.
STAmount ammAssetIn(STAmount const &asset1Balance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Calculate asset deposit given LP Tokens.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
void initializeFeeAuctionVote(ApplyView const &view, SLE::pointer const &ammSle, AccountID const &account, Asset const &lptAsset, std::uint16_t tfee)
Initialize Auction and Voting slots and set the trading/discounted fee.
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 lpTokensIn(STAmount const &asset1Balance, STAmount const &asset1Withdraw, STAmount const &lptAMMBalance, std::uint16_t tfee)
Calculate LP Tokens given asset's withdraw amount.
Number getFee(std::uint16_t tfee)
Convert to the fee from the basis points.
Definition AMMCore.h:83
TIn swapAssetOut(TAmounts< TIn, TOut > const &pool, TOut const &assetOut, std::uint16_t tfee)
Swap assetOut out of the pool and swap in a proportional amount of the other asset.
Definition AMMHelpers.h:456
TERSubset< CanCvtToTER > TER
Definition TER.h:657
Number const kAMMInvariantRelativeTolerance
Definition AMMHelpers.h:52
bool withinRelativeDistance(Quality const &calcQuality, Quality const &reqQuality, Number const &dist)
Check if the relative distance between the qualities is within the requested distance.
Definition AMMHelpers.h:133
STAmount multiply(STAmount const &amount, Number const &frac, Number::RoundingMode rm)
std::expected< std::tuple< STAmount, STAmount, STAmount >, TER > ammHolds(ReadView const &view, SLE const &ammSle, std::optional< Asset > const &optAsset1, std::optional< Asset > const &optAsset2, FreezeHandling freezeHandling, AuthHandling authHandling, beast::Journal const j)
Get AMM pool and LP token balances.
std::uint16_t getTradingFee(ReadView const &view, SLE const &ammSle, AccountID const &account)
Get AMM trading fee for the given account.
Number square(Number const &n)
Return square of n.
STAmount toSTAmount(IOUAmount const &iou, Asset const &asset)
STAmount lpTokensOut(STAmount const &asset1Balance, STAmount const &asset1Deposit, STAmount const &lptAMMBalance, std::uint16_t tfee)
Calculate LP Tokens given asset's deposit amount.
Represents a pair of input and output currencies.
Definition Quality.h:29