1#include <xrpl/tx/transactors/dex/AMMWithdraw.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/core/ServiceRegistry.h>
8#include <xrpl/ledger/Sandbox.h>
9#include <xrpl/ledger/helpers/AMMHelpers.h>
10#include <xrpl/ledger/helpers/AccountRootHelpers.h>
11#include <xrpl/ledger/helpers/MPTokenHelpers.h>
12#include <xrpl/ledger/helpers/RippleStateHelpers.h>
13#include <xrpl/ledger/helpers/TokenHelpers.h>
14#include <xrpl/protocol/AMMCore.h>
15#include <xrpl/protocol/AccountID.h>
16#include <xrpl/protocol/Asset.h>
17#include <xrpl/protocol/Feature.h>
18#include <xrpl/protocol/IOUAmount.h>
19#include <xrpl/protocol/Indexes.h>
20#include <xrpl/protocol/Issue.h>
21#include <xrpl/protocol/Keylet.h>
22#include <xrpl/protocol/LedgerFormats.h>
23#include <xrpl/protocol/MPTIssue.h>
24#include <xrpl/protocol/SField.h>
25#include <xrpl/protocol/STAmount.h>
26#include <xrpl/protocol/STLedgerEntry.h>
27#include <xrpl/protocol/STTx.h>
28#include <xrpl/protocol/TER.h>
29#include <xrpl/protocol/TxFlags.h>
30#include <xrpl/protocol/XRPAmount.h>
31#include <xrpl/tx/Transactor.h>
50 auto const amount = ctx.
tx[~sfAmount];
51 auto const amount2 = ctx.
tx[~sfAmount2];
55 !(amount && amount->holds<
MPTIssue>()) && !(amount2 && amount2->holds<
MPTIssue>()));
61 return tfAMMWithdrawMask;
69 auto const amount = ctx.
tx[~sfAmount];
70 auto const amount2 = ctx.
tx[~sfAmount2];
71 auto const ePrice = ctx.
tx[~sfEPrice];
72 auto const lpTokens = ctx.
tx[~sfLPTokenIn];
83 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid flags.";
88 if (!lpTokens || amount || amount2 || ePrice)
91 else if (ctx.
tx.
isFlag(tfWithdrawAll))
93 if (lpTokens || amount || amount2 || ePrice)
96 else if (ctx.
tx.
isFlag(tfOneAssetWithdrawAll) || ctx.
tx.
isFlag(tfSingleAsset))
98 if (!amount || lpTokens || amount2 || ePrice)
103 if (!amount || !amount2 || lpTokens || ePrice)
106 else if (ctx.
tx.
isFlag(tfOneAssetLPToken))
108 if (!amount || !lpTokens || amount2 || ePrice)
111 else if (ctx.
tx.
isFlag(tfLimitLPToken))
113 if (!amount || !ePrice || lpTokens || amount2)
117 auto const asset = ctx.
tx[sfAsset];
118 auto const asset2 = ctx.
tx[sfAsset2];
121 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: Invalid asset pair.";
125 if (amount && amount2 && amount->asset() == amount2->asset())
127 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid tokens, same issue." << amount->asset() <<
" "
134 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid tokens.";
143 ((flags & (tfOneAssetWithdrawAll | tfOneAssetLPToken)) != 0u) || ePrice))
145 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid Asset1Out";
155 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid Asset2OutAmount";
164 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid EPrice";
178 if ((flags & (tfWithdrawAll | tfOneAssetWithdrawAll)) != 0u)
186 auto const accountID = ctx.
tx[sfAccount];
191 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: Invalid asset pair.";
195 auto const amount = ctx.
tx[~sfAmount];
196 auto const amount2 = ctx.
tx[~sfAmount2];
207 return expected.error();
208 auto const [amountBalance, amount2Balance, lptAMMBalance] = *expected;
215 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: reserves or tokens balance is zero.";
220 auto const ammAccountID = ammSle->getAccountID(sfAccount);
225 if (amount > balance)
228 <<
"AMM Withdraw: withdrawing more than the balance, " << *amount;
236 <<
"AMM Withdraw: account is not authorized, " << amount->asset();
242 ctx.
view, ammAccountID, accountID, accountID, amount->asset()))
244 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: frozen, " <<
to_string(accountID) <<
" "
252 if (
auto const ter =
checkFrozen(ctx.
view, ammAccountID, amount->asset());
256 <<
"AMM Withdraw: AMM account or currency is frozen or locked, "
265 <<
"AMM Withdraw: account is frozen or locked, " <<
to_string(accountID)
274 if (
auto const ter = checkAmount(amount, amountBalance))
277 if (
auto const ter = checkAmount(amount2, amount2Balance))
285 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: tokens balance is zero.";
289 if (lpTokensWithdraw && lpTokensWithdraw->asset() != lpTokens.asset())
291 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid LPTokens.";
295 if (lpTokensWithdraw && *lpTokensWithdraw > lpTokens)
297 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid tokens.";
301 if (
auto const ePrice = ctx.
tx[~sfEPrice]; ePrice && ePrice->asset() != lpTokens.asset())
303 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid EPrice.";
307 if ((ctx.
tx.
getFlags() & (tfLPToken | tfWithdrawAll)) != 0u)
309 if (
auto const ter = checkAmount(amountBalance, amountBalance))
311 if (
auto const ter = checkAmount(amount2Balance, amount2Balance))
324 if (!
ctx_.view().rules().enabled(fixCleanup3_3_0))
327 auto const asset1 =
Asset{
ctx_.tx[sfAsset]};
328 auto const asset2 =
Asset{
ctx_.tx[sfAsset2]};
329 if (!asset1.native() &&
accountID_ == asset1.getIssuer())
331 if (!asset2.native() &&
accountID_ == asset2.getIssuer())
340 auto const amount =
ctx_.tx[~sfAmount];
341 auto const amount2 =
ctx_.tx[~sfAmount2];
342 auto const ePrice =
ctx_.tx[~sfEPrice];
346 auto const ammAccountID = (*ammSle)[sfAccount];
351 auto const lpTokensWithdraw =
359 return {res.error(),
false};
375 return {expected.error(),
false};
376 auto const [amountBalance, amount2Balance, lptAMMBalance] = *expected;
379 auto dispatchToWithdraw = [&,
380 &amountBalance = amountBalance,
381 &amount2Balance = amount2Balance,
383 if (subTxType & tfTwoAsset)
396 if (subTxType & tfOneAssetLPToken || subTxType & tfOneAssetWithdrawAll)
408 if (subTxType & tfLimitLPToken)
411 sb, *ammSle, ammAccountID, amountBalance, lptAMMBalance, *amount, *ePrice, tfee);
413 if (subTxType & tfSingleAsset)
416 sb, *ammSle, ammAccountID, amountBalance, lptAMMBalance, *amount, tfee);
418 if (subTxType & tfLPToken || subTxType & tfWithdrawAll)
433 JLOG(
j_.error()) <<
"AMM Withdraw: invalid options.";
441 return dispatchToWithdraw();
455 JLOG(
j_.error()) <<
"AMMWithdraw: amount out of range " << e.
what();
462 return {result,
false};
467 sb, ammAccountID,
ctx_.tx[sfAsset],
ctx_.tx[sfAsset2], newLPTokenBalance,
j_);
475 sb, ammSle, newLPTokenBalance,
ctx_.tx[sfAsset],
ctx_.tx[sfAsset2],
j_);
478 return {res.first,
false};
481 JLOG(
ctx_.journal.trace()) <<
"AMM Withdraw: tokens " <<
to_string(newLPTokenBalance.iou())
482 <<
" " <<
to_string(lpTokens.iou()) <<
" "
533 return {ter, newLPTokenBalance};
557 view, ammSle, amountWithdraw.
asset(), std::nullopt, freezeHandling, authHandling, journal);
562 auto const [curBalance, curBalance2, _] = *expected;
565 auto const [amountWithdrawActual, amount2WithdrawActual, lpTokensWithdrawActual] =
578 return std::make_tuple(amountWithdraw, amount2Withdraw, lpTokensWithdraw);
581 if (lpTokensWithdrawActual <= beast::kZero || lpTokensWithdrawActual > lpTokens)
583 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw, invalid LP tokens: "
584 << lpTokensWithdrawActual <<
" " << lpTokens <<
" "
585 << lpTokensAMMBalance;
591 if (
view.rules().enabled(fixAMMv1_1) && lpTokensWithdrawActual > lpTokensAMMBalance)
594 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw, unexpected LP tokens: "
595 << lpTokensWithdrawActual <<
" " << lpTokens <<
" "
596 << lpTokensAMMBalance;
602 if ((amountWithdrawActual == curBalance && amount2WithdrawActual != curBalance2) ||
603 (amount2WithdrawActual == curBalance2 && amountWithdrawActual != curBalance))
605 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw one side of the pool "
606 <<
" curBalance: " << curBalance <<
" " << amountWithdrawActual
607 <<
" lpTokensBalance: " << lpTokensWithdraw <<
" lptBalance "
608 << lpTokensAMMBalance;
613 if (lpTokensWithdrawActual == lpTokensAMMBalance &&
614 (amountWithdrawActual != curBalance || amount2WithdrawActual != curBalance2))
616 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw all tokens "
617 <<
" curBalance: " << curBalance <<
" " << amountWithdrawActual
618 <<
" curBalance2: " << amount2WithdrawActual.value_or(
STAmount{0})
619 <<
" lpTokensBalance: " << lpTokensWithdraw <<
" lptBalance "
620 << lpTokensAMMBalance;
625 if (amountWithdrawActual > curBalance || amount2WithdrawActual > curBalance2)
627 JLOG(journal.
debug()) <<
"AMM Withdraw: withdrawing more than the pool's balance "
628 <<
" curBalance: " << curBalance <<
" " << amountWithdrawActual
629 <<
" curBalance2: " << curBalance2 <<
" "
630 << (amount2WithdrawActual ? *amount2WithdrawActual :
STAmount{})
631 <<
" lpTokensBalance: " << lpTokensWithdraw <<
" lptBalance "
632 << lpTokensAMMBalance;
638 if (
view.rules().enabled(featureMPTokensV2))
640 bool const newBalanceZero = (curBalance - amountWithdrawActual) ==
beast::kZero;
641 bool const newBalance2Zero =
642 (curBalance2 - amount2WithdrawActual.value_or(curBalance2.asset())) ==
beast::kZero;
643 bool const newLPTokensZero = (lpTokensAMMBalance - lpTokensWithdrawActual) ==
beast::kZero;
646 bool const valid = [&]() {
647 if (!amount2WithdrawActual)
648 return newBalanceZero == newLPTokensZero;
649 return newBalanceZero == newBalance2Zero && newBalance2Zero == newLPTokensZero;
653 JLOG(journal.
debug()) <<
"AMM Withdraw: some balances are zero"
654 <<
" curBalance: " << curBalance <<
" " << amountWithdrawActual
655 <<
" curBalance2: " << curBalance2 <<
" "
656 << (amount2WithdrawActual ? *amount2WithdrawActual :
STAmount{})
657 <<
" lpTokensBalance: " << lpTokensWithdraw <<
" lptBalance "
658 << lpTokensAMMBalance;
664 bool const enabledFixAmMv12 =
view.rules().enabled(fixAMMv1_2);
668 auto sufficientReserve = [&](
Asset const& asset) ->
TER {
669 mptokenKey = std::nullopt;
670 if (!enabledFixAmMv12 ||
isXRP(asset))
672 bool const isIssue = asset.holds<
Issue>();
673 bool const assetNotExists = [&] {
678 if (!
view.exists(*mptokenKey))
680 mptokenKey = std::nullopt;
689 auto const balance = (*sleAccount)[sfBalance]->xrp();
696 auto const balanceAdj = isIssue ?
std::max(priorBalance, balance) : priorBalance;
697 if (balanceAdj < reserve)
706 if (mptokenKey && account != asset.getIssuer())
708 auto const& mptIssue = asset.get<
MPTIssue>();
739 if (clawbackIssuer && asset.getIssuer() == *clawbackIssuer)
740 createFlags = lsfMPTAuthorized;
743 if (
auto const err =
checkCreateMPT(
view, mptIssue, account, {}, createFlags, journal);
756 if (
auto const err = sufficientReserve(amountWithdrawActual.asset()))
768 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw " << amountWithdrawActual;
774 if (amount2WithdrawActual)
776 if (
auto const err = sufficientReserve(amount2WithdrawActual->asset()); !
isTesSuccess(err))
787 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw " << *amount2WithdrawActual;
795 view, account, lpTokensWithdrawActual, lpTokensWithdrawActual.get<
Issue>(), journal);
799 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw LPTokens";
806 lpTokensAMMBalance - lpTokensWithdrawActual,
807 amountWithdrawActual,
808 amount2WithdrawActual);
819 return lpTokensWithdraw;
857 return {ter, newLPTokenBalance};
870 bool updateBalance =
true;
882 ammSle->setFieldAmount(sfLPTokenBalance, lpTokenBalance);
914 if (lpTokensWithdraw == lptAMMBalance)
935 auto const tokensAdj =
940 auto const frac =
divide(tokensAdj, lptAMMBalance,
noIssue());
941 auto const amountWithdraw =
943 auto const amount2Withdraw =
973 JLOG(journal.
error()) <<
"AMMWithdraw::equalWithdrawTokens exception " << e.
what();
1017 auto frac =
Number{amount} / amountBalance;
1024 if (amount2Withdraw <= amount2)
1038 frac =
Number{amount2} / amount2Balance;
1046 if (!
view.rules().enabled(fixAMMv1_3))
1050 amountWithdraw <= amount,
1051 "xrpl::AMMWithdraw::equalWithdrawLimit : maximum amountWithdraw");
1054 else if (amountWithdraw > amount)
1089 lpTokensIn(amountBalance, amount, lptAMMBalance, tfee),
1093 if (!
view.rules().enabled(fixAMMv1_3))
1101 auto const [tokensAdj, amountWithdrawAdj] =
1139 auto const tokensAdj =
1144 auto const amountWithdraw =
ammAssetOut(amountBalance, lptAMMBalance, tokensAdj, tfee);
1145 if (amount ==
beast::kZero || amountWithdraw >= amount)
1203 Number const ae = amountBalance * ePrice;
1204 auto const f =
getFee(tfee);
1205 auto const denom = lptAMMBalance * f - ae;
1210 auto tokNoRoundCb = [&] {
return lptAMMBalance * (lptAMMBalance + ae * (f - 2)) / denom; };
1211 auto tokProdCb = [&] {
return (lptAMMBalance + ae * (f - 2)) / denom; };
1212 auto const tokensAdj =
1216 if (!
view.rules().enabled(fixAMMv1_3))
1223 auto amtNoRoundCb = [&] {
return tokensAdj / ePrice; };
1224 auto amtProdCb = [&] {
return tokensAdj / ePrice; };
1226 auto const amountWithdraw =
1228 if (amount ==
beast::kZero || amountWithdraw >= amount)
1248 if ((tx[sfFlags] & (tfWithdrawAll | tfOneAssetWithdrawAll)) != 0u)
A generic endpoint for log messages.
FreezeHandling issuerFreezeHandling() const
Returns IgnoreFreeze when the withdrawer is the issuer of a pool asset (post-fixCleanup3_3_0),...
static std::tuple< TER, STAmount, STAmount, std::optional< STAmount > > equalWithdrawTokens(Sandbox &view, SLE const &ammSle, AccountID const account, std::optional< AccountID > const &clawbackIssuer, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &amount2Balance, STAmount const &lptAMMBalance, STAmount const &lpTokens, STAmount const &lpTokensWithdraw, std::uint16_t tfee, FreezeHandling freezeHandling, AuthHandling authHandling, WithdrawAll withdrawAll, XRPAmount const &priorBalance, beast::Journal const &journal)
Equal-asset withdrawal (LPTokens) of some AMM instance pools shares represented by the number of LPTo...
static std::pair< TER, bool > deleteAMMAccountIfEmpty(Sandbox &sb, SLE::pointer const ammSle, STAmount const &lpTokenBalance, Asset const &asset1, Asset const &asset2, beast::Journal const &journal)
static WithdrawAll isWithdrawAll(STTx const &tx)
Check from the flags if it's withdraw all.
static NotTEC preflight(PreflightContext const &ctx)
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
static std::tuple< TER, STAmount, STAmount, std::optional< STAmount > > withdraw(Sandbox &view, SLE const &ammSle, AccountID const &ammAccount, std::optional< AccountID > const &clawbackIssuer, AccountID const &account, STAmount const &amountBalance, STAmount const &amountWithdraw, std::optional< STAmount > const &amount2Withdraw, STAmount const &lpTokensAMMBalance, STAmount const &lpTokensWithdraw, std::uint16_t tfee, FreezeHandling freezeHandling, AuthHandling authHandling, WithdrawAll withdrawAll, XRPAmount const &priorBalance, beast::Journal const &journal)
Withdraw requested assets and token from AMM into LP account.
std::pair< TER, STAmount > equalWithdrawLimit(Sandbox &view, SLE const &ammSle, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &amount2Balance, STAmount const &lptAMMBalance, STAmount const &amount, STAmount const &amount2, std::uint16_t tfee)
Withdraw both assets (Asset1Out, Asset2Out) with the constraints on the maximum amount of each asset ...
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
std::pair< TER, STAmount > singleWithdrawTokens(Sandbox &view, SLE const &ammSle, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &lptAMMBalance, STAmount const &amount, STAmount const &lpTokensWithdraw, std::uint16_t tfee)
Single asset withdrawal (Asset1Out, LPTokens) proportional to the share specified by tokens.
std::pair< TER, STAmount > singleWithdraw(Sandbox &view, SLE const &ammSle, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &lptAMMBalance, STAmount const &amount, std::uint16_t tfee)
Single asset withdrawal (Asset1Out) equivalent to the amount specified in Asset1Out.
static bool checkExtraFeatures(PreflightContext const &ctx)
std::pair< TER, bool > applyGuts(Sandbox &view)
std::pair< TER, STAmount > singleWithdrawEPrice(Sandbox &view, SLE const &ammSle, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &lptAMMBalance, STAmount const &amount, STAmount const &ePrice, std::uint16_t tfee)
Withdraw single asset (Asset1Out, EPrice) with two constraints.
A currency issued by an account.
Number is a floating point type that can represent a wide range of values.
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
Rules controlling protocol behavior.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Asset const & asset() const
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry const > const & const_ref
bool isFlag(std::uint32_t) const
std::uint32_t getFlags() const
Discardable, editable view to a ledger.
AccountID const accountID_
SLE::pointer peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
void update(SLE::ref sle) override
Indicate changes to a peeked SLE.
SLE::const_pointer read(Keylet const &k) const override
Return the state item associated with a key.
Rules const & rules() const override
Returns the tx processing rules.
T make_optional(T... args)
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
NotTEC invalidAMMAmount(STAmount const &amount, std::optional< std::pair< Asset, Asset > > const &pair=std::nullopt, bool validZero=false)
Validate the amount.
STAmount divide(STAmount const &amount, Rate const &rate)
TER createMPToken(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, SLE::ref sponsorSle, std::uint32_t const flags)
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.
WithdrawAll
AMMWithdraw implements AMM withdraw Transactor.
TER checkCreateMPT(xrpl::ApplyView &view, xrpl::MPTIssue const &mptIssue, xrpl::AccountID const &holder, SLE::ref sponsorSle, std::uint32_t flags, beast::Journal j)
TER checkIndividualFrozen(ReadView const &view, AccountID const &account, Asset const &asset)
bool ammEnabled(Rules const &)
Return true if required AMM amendment is enabled.
bool isXRP(AccountID const &c)
Number adjustFracByTokens(Rules const &rules, STAmount const &lptAMMBalance, STAmount const &tokens, Number const &frac)
Find a fraction of tokens after the tokens are adjusted.
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, SLE::ref sponsorSle={}, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
std::expected< bool, TER > verifyAndAdjustLPTokenBalance(Sandbox &sb, STAmount const &lpTokens, SLE::pointer &ammSle, AccountID const &account)
Due to rounding, the LPTokenBalance of the last LP might not match the LP's trustline balance.
NotTEC invalidAMMAssetPair(Asset const &asset1, Asset const &asset2, std::optional< std::pair< Asset, Asset > > const &pair=std::nullopt)
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
static std::optional< STAmount > tokensWithdraw(STAmount const &lpTokens, std::optional< STAmount > const &tokensIn, std::uint32_t flags)
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, IsDeposit isDeposit)
Adjust LP tokens to deposit/withdraw.
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)
static STAmount adjustLPTokensIn(Rules const &rules, STAmount const &lptAMMBalance, STAmount const &lpTokensWithdraw, WithdrawAll withdrawAll)
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.
TERSubset< CanCvtToNotTEC > NotTEC
STAmount getRoundedAsset(Rules const &rules, STAmount const &balance, A const &frac, IsDeposit isDeposit)
Round AMM equal deposit/withdrawal amount.
AuthHandling
Controls the treatment of unauthorized MPT balances.
TER checkAMMPrecisionLoss(Number const &poolProductMean, STAmount const &newLPTokenBalance)
Check AMM pool product invariant after an AMM operation that changes LP tokens (deposit/withdraw/claw...
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
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...
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
bool isTesSuccess(TER x) noexcept
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.
TERSubset< CanCvtToTER > TER
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
@ tecINSUFFICIENT_RESERVE
std::uint32_t ownerCount(SLE::const_ref sle, beast::Journal j, std::int32_t ownerCountAdj=0)
Return number of the objects which reserve is covered by the account(sle) (so called "ownercount").
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.
constexpr FlagValue tfWithdrawSubTx
TER checkWithdrawFreeze(ReadView const &view, AccountID const &pseudoAcct, AccountID const &submitterAcct, AccountID const &dstAcct, Asset const &asset)
Checks freeze compliance for withdrawing an asset from a pseudo-account (e.g.
XRPAmount accountReserve(ReadView const &view, SLE::const_ref sle, beast::Journal j, Adjustment adj={})
Returns the account reserve, in drops.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.