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>
47 auto const amount = ctx.
tx[~sfAmount];
48 auto const amount2 = ctx.
tx[~sfAmount2];
52 !(amount && amount->holds<
MPTIssue>()) && !(amount2 && amount2->holds<
MPTIssue>()));
58 return tfAMMWithdrawMask;
66 auto const amount = ctx.
tx[~sfAmount];
67 auto const amount2 = ctx.
tx[~sfAmount2];
68 auto const ePrice = ctx.
tx[~sfEPrice];
69 auto const lpTokens = ctx.
tx[~sfLPTokenIn];
80 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid flags.";
85 if (!lpTokens || amount || amount2 || ePrice)
88 else if (ctx.
tx.
isFlag(tfWithdrawAll))
90 if (lpTokens || amount || amount2 || ePrice)
93 else if (ctx.
tx.
isFlag(tfOneAssetWithdrawAll) || ctx.
tx.
isFlag(tfSingleAsset))
95 if (!amount || lpTokens || amount2 || ePrice)
100 if (!amount || !amount2 || lpTokens || ePrice)
103 else if (ctx.
tx.
isFlag(tfOneAssetLPToken))
105 if (!amount || !lpTokens || amount2 || ePrice)
108 else if (ctx.
tx.
isFlag(tfLimitLPToken))
110 if (!amount || !ePrice || lpTokens || amount2)
114 auto const asset = ctx.
tx[sfAsset];
115 auto const asset2 = ctx.
tx[sfAsset2];
118 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: Invalid asset pair.";
122 if (amount && amount2 && amount->asset() == amount2->asset())
124 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid tokens, same issue." << amount->asset() <<
" "
131 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid tokens.";
140 ((flags & (tfOneAssetWithdrawAll | tfOneAssetLPToken)) != 0u) || ePrice))
142 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid Asset1Out";
152 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid Asset2OutAmount";
161 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid EPrice";
175 if ((flags & (tfWithdrawAll | tfOneAssetWithdrawAll)) != 0u)
183 auto const accountID = ctx.
tx[sfAccount];
188 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: Invalid asset pair.";
192 auto const amount = ctx.
tx[~sfAmount];
193 auto const amount2 = ctx.
tx[~sfAmount2];
204 return expected.error();
205 auto const [amountBalance, amount2Balance, lptAMMBalance] = *expected;
212 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: reserves or tokens balance is zero.";
217 auto const ammAccountID = ammSle->getAccountID(sfAccount);
220 auto const& balance) ->
TER {
223 if (amount > balance)
226 <<
"AMM Withdraw: withdrawing more than the balance, " << *amount;
234 <<
"AMM Withdraw: account is not authorized, " << amount->asset();
240 ctx.
view, ammAccountID, accountID, accountID, amount->asset()))
242 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: frozen, " <<
to_string(accountID) <<
" "
250 if (
auto const ter =
checkFrozen(ctx.
view, ammAccountID, amount->asset());
254 <<
"AMM Withdraw: AMM account or currency is frozen or locked, "
263 <<
"AMM Withdraw: account is frozen or locked, " <<
to_string(accountID)
272 if (
auto const ter = checkAmount(amount, amountBalance))
275 if (
auto const ter = checkAmount(amount2, amount2Balance))
283 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: tokens balance is zero.";
287 if (lpTokensWithdraw && lpTokensWithdraw->asset() != lpTokens.asset())
289 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid LPTokens.";
293 if (lpTokensWithdraw && *lpTokensWithdraw > lpTokens)
295 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid tokens.";
299 if (
auto const ePrice = ctx.
tx[~sfEPrice]; ePrice && ePrice->asset() != lpTokens.asset())
301 JLOG(ctx.
j.
debug()) <<
"AMM Withdraw: invalid EPrice.";
305 if ((ctx.
tx.
getFlags() & (tfLPToken | tfWithdrawAll)) != 0u)
307 if (
auto const ter = checkAmount(amountBalance, amountBalance))
309 if (
auto const ter = checkAmount(amount2Balance, amount2Balance))
322 if (!
ctx_.view().rules().enabled(fixCleanup3_3_0))
325 auto const asset1 =
Asset{
ctx_.tx[sfAsset]};
326 auto const asset2 =
Asset{
ctx_.tx[sfAsset2]};
327 if (!asset1.native() &&
accountID_ == asset1.getIssuer())
329 if (!asset2.native() &&
accountID_ == asset2.getIssuer())
338 auto const amount =
ctx_.tx[~sfAmount];
339 auto const amount2 =
ctx_.tx[~sfAmount2];
340 auto const ePrice =
ctx_.tx[~sfEPrice];
344 auto const ammAccountID = (*ammSle)[sfAccount];
349 auto const lpTokensWithdraw =
355 return {res.error(),
false};
370 return {expected.error(),
false};
371 auto const [amountBalance, amount2Balance, lptAMMBalance] = *expected;
374 auto dispatchToWithdraw = [&,
375 &amountBalance = amountBalance,
376 &amount2Balance = amount2Balance,
378 if (subTxType & tfTwoAsset)
391 if (subTxType & tfOneAssetLPToken || subTxType & tfOneAssetWithdrawAll)
403 if (subTxType & tfLimitLPToken)
406 sb, *ammSle, ammAccountID, amountBalance, lptAMMBalance, *amount, *ePrice, tfee);
408 if (subTxType & tfSingleAsset)
411 sb, *ammSle, ammAccountID, amountBalance, lptAMMBalance, *amount, tfee);
413 if (subTxType & tfLPToken || subTxType & tfWithdrawAll)
428 JLOG(
j_.error()) <<
"AMM Withdraw: invalid options.";
436 return dispatchToWithdraw();
450 JLOG(
j_.error()) <<
"AMMWithdraw: amount out of range " << e.
what();
457 return {result,
false};
462 sb, ammAccountID,
ctx_.tx[sfAsset],
ctx_.tx[sfAsset2], newLPTokenBalance,
j_);
470 sb, ammSle, newLPTokenBalance,
ctx_.tx[sfAsset],
ctx_.tx[sfAsset2],
j_);
473 return {res.first,
false};
476 JLOG(
ctx_.journal.trace()) <<
"AMM Withdraw: tokens " <<
to_string(newLPTokenBalance.iou())
477 <<
" " <<
to_string(lpTokens.iou()) <<
" "
529 return {ter, newLPTokenBalance};
554 view, ammSle, amountWithdraw.
asset(), std::nullopt, freezeHandling, authHandling, journal);
559 auto const [curBalance, curBalance2, _] = *expected;
562 auto const [amountWithdrawActual, amount2WithdrawActual, lpTokensWithdrawActual] =
575 return std::make_tuple(amountWithdraw, amount2Withdraw, lpTokensWithdraw);
578 if (lpTokensWithdrawActual <= beast::kZero || lpTokensWithdrawActual > lpTokens)
580 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw, invalid LP tokens: "
581 << lpTokensWithdrawActual <<
" " << lpTokens <<
" "
582 << lpTokensAMMBalance;
588 if (lpTokensWithdrawActual > lpTokensAMMBalance)
591 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw, unexpected LP tokens: "
592 << lpTokensWithdrawActual <<
" " << lpTokens <<
" "
593 << lpTokensAMMBalance;
599 if ((amountWithdrawActual == curBalance && amount2WithdrawActual != curBalance2) ||
600 (amount2WithdrawActual == curBalance2 && amountWithdrawActual != curBalance))
602 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw one side of the pool "
603 <<
" curBalance: " << curBalance <<
" " << amountWithdrawActual
604 <<
" lpTokensBalance: " << lpTokensWithdraw <<
" lptBalance "
605 << lpTokensAMMBalance;
610 if (lpTokensWithdrawActual == lpTokensAMMBalance &&
611 (amountWithdrawActual != curBalance || amount2WithdrawActual != curBalance2))
613 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw all tokens "
614 <<
" curBalance: " << curBalance <<
" " << amountWithdrawActual
615 <<
" curBalance2: " << amount2WithdrawActual.value_or(
STAmount{0})
616 <<
" lpTokensBalance: " << lpTokensWithdraw <<
" lptBalance "
617 << lpTokensAMMBalance;
622 if (amountWithdrawActual > curBalance || amount2WithdrawActual > curBalance2)
624 JLOG(journal.
debug()) <<
"AMM Withdraw: withdrawing more than the pool's balance "
625 <<
" curBalance: " << curBalance <<
" " << amountWithdrawActual
626 <<
" curBalance2: " << curBalance2 <<
" "
627 << (amount2WithdrawActual ? *amount2WithdrawActual :
STAmount{})
628 <<
" lpTokensBalance: " << lpTokensWithdraw <<
" lptBalance "
629 << lpTokensAMMBalance;
635 if (
view.rules().enabled(featureMPTokensV2))
637 bool const newBalanceZero = (curBalance - amountWithdrawActual) ==
beast::kZero;
638 bool const newBalance2Zero =
639 (curBalance2 - amount2WithdrawActual.value_or(curBalance2.asset())) ==
beast::kZero;
640 bool const newLPTokensZero = (lpTokensAMMBalance - lpTokensWithdrawActual) ==
beast::kZero;
643 bool const valid = [&] {
644 if (!amount2WithdrawActual)
645 return newBalanceZero == newLPTokensZero;
646 return newBalanceZero == newBalance2Zero && newBalance2Zero == newLPTokensZero;
650 JLOG(journal.
debug()) <<
"AMM Withdraw: some balances are zero"
651 <<
" curBalance: " << curBalance <<
" " << amountWithdrawActual
652 <<
" curBalance2: " << curBalance2 <<
" "
653 << (amount2WithdrawActual ? *amount2WithdrawActual :
STAmount{})
654 <<
" lpTokensBalance: " << lpTokensWithdraw <<
" lptBalance "
655 << lpTokensAMMBalance;
661 bool const enabledFixAmMv12 =
view.rules().enabled(fixAMMv1_2);
665 auto const sufficientReserve = [&](
Asset const& asset) ->
TER {
666 mptokenKey = std::nullopt;
667 if (!enabledFixAmMv12 ||
isXRP(asset))
669 bool const assetNotExists = asset.visit(
674 if (!
view.exists(*mptokenKey))
676 mptokenKey = std::nullopt;
685 if (
view.rules().enabled(fixCleanup3_4_0) &&
693 auto const balance = (*sleAccount)[sfBalance]->xrp();
700 auto const balanceAdj =
std::max(priorBalance, balance);
701 if (balanceAdj < reserve)
710 if (mptokenKey && account != asset.getIssuer())
712 auto const& mptIssue = asset.get<
MPTIssue>();
743 if (clawbackIssuer && asset.getIssuer() == *clawbackIssuer)
744 createFlags = lsfMPTAuthorized;
747 if (
auto const err =
checkCreateMPT(
view, mptIssue, account, {}, createFlags, journal);
760 if (
auto const err = sufficientReserve(amountWithdrawActual.asset()))
772 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw " << amountWithdrawActual;
778 if (amount2WithdrawActual)
780 if (
auto const err = sufficientReserve(amount2WithdrawActual->asset()); !
isTesSuccess(err))
791 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw " << *amount2WithdrawActual;
799 view, account, lpTokensWithdrawActual, lpTokensWithdrawActual.get<
Issue>(), journal);
803 JLOG(journal.
debug()) <<
"AMM Withdraw: failed to withdraw LPTokens";
810 lpTokensAMMBalance - lpTokensWithdrawActual,
811 amountWithdrawActual,
812 amount2WithdrawActual);
823 return lpTokensWithdraw;
862 return {ter, newLPTokenBalance};
875 bool updateBalance =
true;
887 ammSle->setFieldAmount(sfLPTokenBalance, lpTokenBalance);
920 if (lpTokensWithdraw == lptAMMBalance)
942 auto const tokensAdj =
947 auto const frac =
divide(tokensAdj, lptAMMBalance,
noIssue());
948 auto const amountWithdraw =
950 auto const amount2Withdraw =
981 JLOG(journal.
error()) <<
"AMMWithdraw::equalWithdrawTokens exception " << e.
what();
1025 auto frac =
Number{amount} / amountBalance;
1032 if (amount2Withdraw <= amount2)
1046 frac =
Number{amount2} / amount2Balance;
1054 if (!
view.rules().enabled(fixAMMv1_3))
1058 amountWithdraw <= amount,
1059 "xrpl::AMMWithdraw::equalWithdrawLimit : maximum amountWithdraw");
1062 else if (amountWithdraw > amount)
1097 lpTokensIn(amountBalance, amount, lptAMMBalance, tfee),
1101 if (!
view.rules().enabled(fixAMMv1_3))
1109 auto const [tokensAdj, amountWithdrawAdj] =
1147 auto const tokensAdj =
1152 auto const amountWithdraw =
ammAssetOut(amountBalance, lptAMMBalance, tokensAdj, tfee);
1153 if (amount ==
beast::kZero || amountWithdraw >= amount)
1211 Number const ae = amountBalance * ePrice;
1212 auto const f =
getFee(tfee);
1213 auto const denom = lptAMMBalance * f - ae;
1218 auto const tokNoRoundCb = [&] {
1219 return lptAMMBalance * (lptAMMBalance + ae * (f - 2)) / denom;
1221 auto const tokProdCb = [&] {
return (lptAMMBalance + ae * (f - 2)) / denom; };
1222 auto const tokensAdj =
1226 if (!
view.rules().enabled(fixAMMv1_3))
1233 auto const amtNoRoundCb = [&] {
return tokensAdj / ePrice; };
1234 auto const amtProdCb = [&] {
return tokensAdj / ePrice; };
1236 auto const amountWithdraw =
1238 if (amount ==
beast::kZero || amountWithdraw >= amount)
1258 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 > > 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, ReserveHandling reserveHandling, WithdrawAll withdrawAll, XRPAmount const &priorBalance, beast::Journal const &journal)
Withdraw requested assets and token from AMM into LP account.
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)
static TER preclaim(PreclaimContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
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)
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, ReserveHandling reserveHandling, 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...
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 & ConstRef
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.
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.
void update(SLE::Ref sle) override
Indicate changes to a peeked SLE.
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)
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 checkIndividualFrozen(ReadView const &view, AccountID const &account, Asset const &asset)
std::uint32_t ownerCount(SLE::ConstRef 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").
TER checkCreateMPT(xrpl::ApplyView &view, xrpl::MPTIssue const &mptIssue, xrpl::AccountID const &holder, SLE::Ref sponsorSle, std::uint32_t flags, beast::Journal j)
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.
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)
ReserveHandling
Controls whether the recipient owner-reserve check is enforced when auto-creating a trustline or MPTo...
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
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.
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...
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.
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
XRPAmount accountReserve(ReadView const &view, SLE::ConstRef sle, beast::Journal j, Adjustment adj={})
Returns the account reserve, in drops.
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
TER createMPToken(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, SLE::Ref sponsorSle, std::uint32_t const flags)
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.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.