1#include <xrpl/ledger/helpers/AMMHelpers.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/safe_cast.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/beast/utility/Zero.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/ledger/ApplyView.h>
11#include <xrpl/ledger/ReadView.h>
12#include <xrpl/ledger/Sandbox.h>
13#include <xrpl/ledger/View.h>
14#include <xrpl/ledger/helpers/RippleStateHelpers.h>
15#include <xrpl/ledger/helpers/TokenHelpers.h>
16#include <xrpl/protocol/AMMCore.h>
17#include <xrpl/protocol/AccountID.h>
18#include <xrpl/protocol/AmountConversions.h>
19#include <xrpl/protocol/Asset.h>
20#include <xrpl/protocol/Feature.h>
21#include <xrpl/protocol/Indexes.h>
22#include <xrpl/protocol/Issue.h>
23#include <xrpl/protocol/LedgerFormats.h>
24#include <xrpl/protocol/MPTIssue.h>
25#include <xrpl/protocol/Protocol.h>
26#include <xrpl/protocol/Rules.h>
27#include <xrpl/protocol/SField.h>
28#include <xrpl/protocol/STAmount.h>
29#include <xrpl/protocol/STArray.h>
30#include <xrpl/protocol/STLedgerEntry.h>
31#include <xrpl/protocol/TER.h>
51 auto const tokens =
root2(asset1 * asset2);
70 Number const r = asset1Deposit / asset1Balance;
71 auto const c =
root2(f2 * f2 + r / f1) - f2;
74 auto const t = lptAMMBalance * (r - c) / (1 + c);
79 auto const frac = (r - c) / (1 + c);
103 auto const t1 = lpTokens / lptAMMBalance;
104 auto const t2 = 1 + t1;
105 auto const d = f2 - t1 / t2;
106 auto const a = 1 / (t2 * t2);
107 auto const b = 2 * d / t2 - 1 / f1;
108 auto const c = d * d - f2 * f2;
130 Number const fr = asset1Withdraw / asset1Balance;
131 auto const f1 =
getFee(tfee);
132 auto const c = fr * f1 + 2 - f1;
135 auto const t = lptAMMBalance * (c -
root2(c * c - 4 * fr)) / 2;
140 auto const frac = (c -
root2(c * c - 4 * fr)) / 2;
161 auto const f =
getFee(tfee);
162 Number const t1 = lpTokens / lptAMMBalance;
165 auto const b = assetBalance * (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
170 auto const frac = (t1 * t1 - t1 * (2 - f)) / (t1 * f - 1);
187 return (lptAMMBalance + lpTokens) - lptAMMBalance;
188 return (lpTokens - lptAMMBalance) + lptAMMBalance;
205 auto const lpTokensActual =
adjustLPTokens(lptAMMBalance, lpTokens, isDeposit);
207 if (lpTokensActual == beast::kZero)
213 if (lpTokensActual < lpTokens)
215 bool const ammRoundingEnabled = [&]() {
217 rules && rules->enabled(fixAMMv1_1))
225 Number const fr = lpTokensActual / lpTokens;
227 auto const amount2Actual =
toSTAmount(amount2->asset(), fr * *amount2);
228 if (!ammRoundingEnabled)
231 amountActual < amount ? amountActual : amount,
232 amount2Actual < amount2 ? amount2Actual : amount2,
240 auto const amountActual = [&]() {
243 return ammAssetIn(amountBalance, lptAMMBalance, lpTokensActual, tfee);
245 if (!ammRoundingEnabled)
247 return ammAssetOut(amountBalance, lptAMMBalance, lpTokens, tfee);
250 return ammAssetOut(amountBalance, lptAMMBalance, lpTokensActual, tfee);
252 if (!ammRoundingEnabled)
254 return amountActual < amount
263 lpTokensActual == lpTokens,
"xrpl::adjustAmountsByLPTokens : LP tokens match actual");
265 return {amount, amount2, lpTokensActual};
271 return (-b +
root2(b * b - 4 * a * c)) / (2 * a);
278 auto const d = b * b - 4 * a * c;
285 return (2 * c) / (-b -
root2(d));
288 return (2 * c) / (-b +
root2(d));
295 auto const t = amount * frac;
307 if (!rules.
enabled(fixAMMv1_3))
312 return multiply(balance, productCb(), rm);
324 if (!rules.
enabled(fixAMMv1_3))
328 auto const tokens =
multiply(balance, frac, rm);
340 if (!rules.
enabled(fixAMMv1_3))
343 auto const tokens = [&] {
350 return multiply(lptAMMBalance, productCb(), rm);
364 if (!rules.
enabled(fixAMMv1_3))
365 return {tokens, amount};
366 auto assetAdj =
ammAssetIn(balance, lptAMMBalance, tokens, tfee);
367 auto tokensAdj = tokens;
371 if (assetAdj > amount)
373 auto const adjAmount = amount - (assetAdj - amount);
374 auto const t =
lpTokensOut(balance, adjAmount, lptAMMBalance, tfee);
376 assetAdj =
ammAssetIn(balance, lptAMMBalance, tokensAdj, tfee);
378 return {tokensAdj,
std::min(amount, assetAdj)};
390 if (!rules.
enabled(fixAMMv1_3))
391 return {tokens, amount};
392 auto assetAdj =
ammAssetOut(balance, lptAMMBalance, tokens, tfee);
393 auto tokensAdj = tokens;
397 if (assetAdj > amount)
399 auto const adjAmount = amount - (assetAdj - amount);
400 auto const t =
lpTokensIn(balance, adjAmount, lptAMMBalance, tfee);
402 assetAdj =
ammAssetOut(balance, lptAMMBalance, tokensAdj, tfee);
404 return {tokensAdj,
std::min(amount, assetAdj)};
414 if (!rules.
enabled(fixAMMv1_3))
416 return tokens / lptAMMBalance;
429 auto const assetInBalance =
430 accountHolds(view, ammAccountID, asset1, freezeHandling, authHandling, j);
431 auto const assetOutBalance =
432 accountHolds(view, ammAccountID, asset2, freezeHandling, authHandling, j);
439 if (newLPTokenBalance <= beast::kZero)
441 if (poolProductMean >= newLPTokenBalance)
460 if (newLPTokenBalance <= beast::kZero)
473std::expected<std::tuple<STAmount, STAmount, STAmount>,
TER>
484 auto const asset1 = ammSle[sfAsset];
485 auto const asset2 = ammSle[sfAsset2];
486 if (optAsset1 && optAsset2)
493 JLOG(j.
debug()) <<
"ammHolds: Invalid optAsset1 or optAsset2 " << *optAsset1 <<
" "
500 auto const singleAsset = [&asset1, &asset2, &j](
503 if (checkIssue == asset1)
507 if (checkIssue == asset2)
513 JLOG(j.
debug()) <<
"ammHolds: Invalid " << label <<
" " << checkIssue;
519 return singleAsset(*optAsset1,
"optAsset1");
524 return singleAsset(*optAsset2,
"optAsset2");
562 JLOG(j.
trace()) <<
"ammLPHolds: no SLE "
566 else if (
isFrozen(view, lpAccount, currency, ammAccount))
569 JLOG(j.
trace()) <<
"ammLPHolds: frozen currency "
575 amount = sle->getFieldAmount(sfBalance);
576 if (lpAccount > ammAccount)
581 amount.
get<
Issue>().account = ammAccount;
583 JLOG(j.
trace()) <<
"ammLPHolds:"
598 return ammLPHolds(view, ammSle[sfAsset], ammSle[sfAsset2], ammSle[sfAccount], lpAccount, j);
607 "xrpl::getTradingFee : auction present");
612 if (
auto const expiration = auctionSlot[~sfExpiration];
616 if (auctionSlot[~sfAccount] == account)
617 return auctionSlot[sfDiscountedFee];
618 if (auctionSlot.isFieldPresent(sfAuthAccounts))
620 for (
auto const& acct : auctionSlot.getFieldArray(sfAuthAccounts))
622 if (acct[~sfAccount] == account)
623 return auctionSlot[sfDiscountedFee];
628 return ammSle[sfTradingFee];
638 sle && !
isFrozen(view, ammAccountID, issue))
639 return STAmount{issue, (*sle)[sfMPTAmount]};
642 [&](
Issue const& issue) {
646 return (*sle)[sfBalance];
651 sle && !
isFrozen(view, ammAccountID, issue.currency, issue.account))
653 STAmount amount = (*sle)[sfBalance];
654 if (ammAccountID > issue.account)
656 amount.
get<
Issue>().account = issue.account;
677 if (nodeType == ltAMM || nodeType == ltMPTOKEN)
680 if (nodeType == ltRIPPLE_STATE)
686 JLOG(j.
error()) <<
"deleteAMMObjects: deleting trustline with "
695 JLOG(j.
error()) <<
"deleteAMMObjects: deleting non-trustline or non-MPT " << nodeType;
700 maxTrustlinesToDelete);
713 if (nodeType == ltAMM)
716 if (nodeType == ltMPTOKEN)
720 (*sleItem)[~sfLockedAmount].valueOr(0) != 0)
723 JLOG(j.
error()) <<
"deleteAMMObjects: deleting MPT with "
731 if (nodeType == ltRIPPLE_STATE)
735 JLOG(j.
error()) <<
"deleteAMMObjects: trustlines should have been deleted";
740 JLOG(j.
error()) <<
"deleteAMMObjects: deleting non-trustline or non-MPT " << nodeType;
755 JLOG(j.
error()) <<
"deleteAMMAccount: AMM object does not exist " << asset <<
" " << asset2;
760 auto const ammAccountID = (*ammSle)[sfAccount];
765 JLOG(j.
error()) <<
"deleteAMMAccount: AMM account does not exist "
782 if (!sb.
dirRemove(ownerDirKeylet, (*ammSle)[sfOwnerNode], ammSle->key(),
false))
785 JLOG(j.
error()) <<
"deleteAMMAccount: failed to remove dir link";
792 JLOG(j.
error()) <<
"deleteAMMAccount: cannot delete root dir node of "
799 sb.
erase(sleAMMRoot);
809 Asset const& lptAsset,
812 auto const& rules = view.
rules();
828 ammSle->
set(std::move(auctionSlot));
861std::expected<bool, TER>
885 auto currentIndex =
root;
890 auto const ownerDir = view.
read(currentIndex);
893 for (
auto const& key : ownerDir->getFieldV256(sfIndexes))
898 auto const entryType = sle->getFieldU16(sfLedgerEntryType);
900 if (entryType == ltAMM)
907 if (entryType == ltMPTOKEN)
912 if (entryType != ltRIPPLE_STATE)
914 auto const lowLimit = sle->getFieldAmount(sfLowLimit);
915 auto const highLimit = sle->getFieldAmount(sfHighLimit);
916 auto const isLPTrustline =
917 lowLimit.getIssuer() == lpAccount || highLimit.getIssuer() == lpAccount;
918 auto const isLPTokenTrustline =
919 lowLimit.asset() == ammIssue || highLimit.asset() == ammIssue;
925 if (isLPTokenTrustline)
928 if (++nLPTokenTrustLines > 1)
932 else if (++nIOUTrustLines > 2)
938 else if (isLPTokenTrustline)
943 else if (++nIOUTrustLines > 2)
948 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
951 if (nLPTokenTrustLines != 1 || (nIOUTrustLines == 0 && nMPT == 0) ||
952 (nIOUTrustLines > 2 || nMPT > 2) || (nIOUTrustLines + nMPT) > 2)
961std::expected<bool, TER>
969 if (!res.has_value())
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Writeable view to a ledger, for applying a transaction.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
bool emptyDirDelete(Keylet const &directory)
Remove the specified directory, if it is empty.
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
A currency issued by an account.
Number is a floating point type that can represent a wide range of values.
static RoundingMode setround(RoundingMode inMode)
static RoundingMode getround()
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.
virtual LedgerHeader const & header() const =0
Returns information about the ledger.
virtual STAmount balanceHookIOU(AccountID const &account, AccountID const &issuer, STAmount const &amount) const
Rules controlling protocol behavior.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
constexpr TIss const & get() const
std::string getFullText() const override
Asset const & asset() const
void pushBack(STObject const &object)
std::shared_ptr< STLedgerEntry > pointer
void setFieldU32(SField const &field, std::uint32_t)
void setFieldArray(SField const &field, STArray const &v)
STObject & peekFieldObject(SField const &field)
void setFieldAmount(SField const &field, STAmount const &)
bool isFieldPresent(SField const &field) const
void setFieldU16(SField const &field, std::uint16_t)
static STObject makeInnerObject(SField const &name)
STBase const & peekAtField(SField const &field) const
void set(SOTemplate const &)
std::uint64_t getFieldU64(SField const &field) const
void setAccountID(SField const &field, AccountID const &)
AccountID getAccountID(SField const &field) const
void makeFieldAbsent(SField const &field)
STAmount const & getFieldAmount(SField const &field) const
Discardable, editable view to a ledger.
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.
void erase(SLE::ref sle) override
Remove a peeked SLE.
bool exists(Keylet const &k) const override
Determine if a state item exists.
T duration_cast(T... args)
T make_optional(T... args)
Number::RoundingMode getLPTokenRounding(IsDeposit isDeposit)
Number::RoundingMode getAssetRounding(IsDeposit isDeposit)
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
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.
static TER deleteAMMTrustLines(Sandbox &sb, AccountID const &ammAccountID, std::uint16_t maxTrustlinesToDelete, beast::Journal j)
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.
constexpr std::uint32_t kTotalTimeSlotSecs
bool isFeatureEnabled(uint256 const &feature, bool resultIfNoRules)
Check whether a feature is enabled in the current ledger rules.
FreezeHandling
Controls the treatment of frozen account balances.
Number feeMultHalf(std::uint16_t tfee)
Get fee multiplier (1 - tfee / 2) @tfee trading fee in basis points.
Dest safeDowncast(Src *s) noexcept
constexpr std::uint32_t kAuctionSlotDiscountedFeeFraction
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.
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Asset const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
TER deleteAMMTrustLine(ApplyView &view, SLE::pointer sleState, std::optional< AccountID > const &ammAccountID, beast::Journal j)
Delete trustline to AMM.
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.
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)
void initializeFeeAuctionVote(ApplyView &view, SLE::pointer &ammSle, AccountID const &account, Asset const &lptAsset, std::uint16_t tfee)
Initialize Auction and Voting slots and set the trading/discounted fee.
TER deleteAMMMPToken(ApplyView &view, SLE::pointer sleMPT, AccountID const &ammAccountID, beast::Journal j)
Delete AMMs MPToken.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, IsDeposit isDeposit)
Adjust LP tokens to deposit/withdraw.
Number root(Number f, unsigned d)
std::optional< Rules > const & getCurrentTransactionRules()
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).
Currency ammLPTCurrency(Asset const &asset1, Asset const &asset2)
Calculate Liquidity Provider Token (LPT) Currency.
STAmount ammAssetOut(STAmount const &assetBalance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Calculate asset withdrawal by tokens.
constexpr std::uint16_t kMaxDeletableAmmTrustLines
The maximum number of trustlines to delete as part of AMM account deletion cleanup.
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)
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.
std::pair< STAmount, STAmount > adjustAssetInByTokens(Rules const &rules, STAmount const &balance, STAmount const &amount, STAmount const &lptAMMBalance, STAmount const &tokens, std::uint16_t tfee)
STAmount ammAccountHolds(ReadView const &view, AccountID const &ammAccountID, Asset const &asset)
Returns total amount held by AMM for the given token.
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.
static TER deleteAMMMPTokens(Sandbox &sb, AccountID const &ammAccountID, beast::Journal j)
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.
Number feeMult(std::uint16_t tfee)
Get fee multiplier (1 - tfee) @tfee trading fee in basis points.
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< Number > solveQuadraticEqSmallest(Number const &a, Number const &b, Number const &c)
Solve quadratic equation to find takerGets or takerPays.
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, std::uint8_t depth=0)
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.
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...
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
Number const kAMMInvariantRelativeTolerance
LedgerEntryType
Identifiers for on-ledger objects.
bool withinRelativeDistance(Quality const &calcQuality, Quality const &reqQuality, Number const &dist)
Check if the relative distance between the qualities is within the requested distance.
STAmount multiply(STAmount const &amount, Number const &frac, Number::RoundingMode rm)
constexpr std::uint32_t kVoteWeightScaleFactor
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 accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
STAmount toSTAmount(IOUAmount const &iou, Asset const &asset)
TER cleanupOnAccountDelete(ApplyView &view, Keylet const &ownerDirKeylet, EntryDeleter const &deleter, beast::Journal j, std::optional< std::uint16_t > maxNodesToDelete=std::nullopt)
Cleanup owner directory entries on account delete.
STAmount lpTokensOut(STAmount const &asset1Balance, STAmount const &asset1Deposit, STAmount const &lptAMMBalance, std::uint16_t tfee)
Calculate LP Tokens given asset's deposit amount.
T time_since_epoch(T... args)