20#include <xrpld/app/misc/AMMUtils.h>
21#include <xrpld/ledger/Sandbox.h>
23#include <xrpl/basics/Log.h>
24#include <xrpl/protocol/AMMCore.h>
25#include <xrpl/protocol/STObject.h>
38 auto const assetInBalance =
39 accountHolds(view, ammAccountID, issue1, freezeHandling, j);
40 auto const assetOutBalance =
41 accountHolds(view, ammAccountID, issue2, freezeHandling, j);
45Expected<std::tuple<STAmount, STAmount, STAmount>,
TER>
55 auto const issue1 = ammSle[sfAsset].get<
Issue>();
56 auto const issue2 = ammSle[sfAsset2].get<
Issue>();
57 if (optIssue1 && optIssue2)
66 JLOG(j.
debug()) <<
"ammHolds: Invalid optIssue1 or optIssue2 "
67 << *optIssue1 <<
" " << *optIssue2;
73 auto const singleIssue =
74 [&issue1, &issue2, &j](
77 if (checkIssue == issue1)
79 else if (checkIssue == issue2)
84 <<
"ammHolds: Invalid " << label <<
" " << checkIssue;
90 return singleIssue(*optIssue1,
"optIssue1");
95 return singleIssue(*optIssue2,
"optIssue2");
132 JLOG(j.
trace()) <<
"ammLPHolds: no SLE "
136 else if (
isFrozen(view, lpAccount, currency, ammAccount))
139 JLOG(j.
trace()) <<
"ammLPHolds: frozen currency "
145 amount = sle->getFieldAmount(sfBalance);
146 if (lpAccount > ammAccount)
153 JLOG(j.
trace()) <<
"ammLPHolds:"
158 return view.
balanceHook(lpAccount, ammAccount, amount);
170 ammSle[sfAsset].get<Issue>().currency,
171 ammSle[sfAsset2].get<Issue>().currency,
184 "ripple::getTradingFee : auction present");
187 auto const& auctionSlot =
190 if (
auto const expiration = auctionSlot[~sfExpiration];
191 duration_cast<seconds>(
193 .count() < expiration)
195 if (auctionSlot[~sfAccount] == account)
196 return auctionSlot[sfDiscountedFee];
197 if (auctionSlot.isFieldPresent(sfAuthAccounts))
199 for (
auto const& acct :
200 auctionSlot.getFieldArray(sfAuthAccounts))
201 if (acct[~sfAccount] == account)
202 return auctionSlot[sfDiscountedFee];
206 return ammSle[sfTradingFee];
218 return (*sle)[sfBalance];
220 else if (
auto const sle = view.
read(
225 auto amount = (*sle)[sfBalance];
226 if (ammAccountID > issue.
account)
228 amount.setIssuer(issue.
account);
249 if (nodeType == LedgerEntryType::ltAMM)
252 if (nodeType != LedgerEntryType::ltRIPPLE_STATE)
256 <<
"deleteAMMTrustLines: deleting non-trustline "
263 if (sleItem->getFieldAmount(sfBalance) != beast::zero)
267 <<
"deleteAMMTrustLines: deleting trustline with "
278 maxTrustlinesToDelete);
292 JLOG(j.
error()) <<
"deleteAMMAccount: AMM object does not exist "
293 << asset <<
" " << asset2;
298 auto const ammAccountID = (*ammSle)[sfAccount];
303 JLOG(j.
error()) <<
"deleteAMMAccount: AMM account does not exist "
316 ownerDirKeylet, (*ammSle)[sfOwnerNode], ammSle->key(),
false))
319 JLOG(j.
error()) <<
"deleteAMMAccount: failed to remove dir link";
326 JLOG(j.
error()) <<
"deleteAMMAccount: cannot delete root dir node of "
333 sb.
erase(sleAMMRoot);
343 Issue const& lptIssue,
346 auto const& rules = view.
rules();
355 ammSle->setFieldArray(sfVoteSlots, voteSlots);
359 if (rules.enabled(fixInnerObjTemplate) &&
360 !ammSle->isFieldPresent(sfAuctionSlot))
363 ammSle->set(std::move(auctionSlot));
365 STObject& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
368 auto const expiration = std::chrono::duration_cast<std::chrono::seconds>(
376 ammSle->setFieldU16(sfTradingFee, tfee);
377 else if (ammSle->isFieldPresent(sfTradingFee))
378 ammSle->makeFieldAbsent(sfTradingFee);
388 Issue const& ammIssue,
406 auto currentIndex =
root;
411 auto const ownerDir = view.
read(currentIndex);
414 for (
auto const& key : ownerDir->getFieldV256(sfIndexes))
420 if (sle->getFieldU16(sfLedgerEntryType) == ltAMM)
427 if (sle->getFieldU16(sfLedgerEntryType) != ltRIPPLE_STATE)
429 auto const lowLimit = sle->getFieldAmount(sfLowLimit);
430 auto const highLimit = sle->getFieldAmount(sfHighLimit);
431 auto const isLPTrustline = lowLimit.getIssuer() == lpAccount ||
432 highLimit.getIssuer() == lpAccount;
433 auto const isLPTokenTrustline =
434 lowLimit.issue() == ammIssue || highLimit.issue() == ammIssue;
440 if (isLPTokenTrustline)
442 if (++nLPTokenTrustLines > 1)
445 else if (++nIOUTrustLines > 2)
449 else if (isLPTokenTrustline)
451 else if (++nIOUTrustLines > 2)
454 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
457 if (nLPTokenTrustLines != 1 || nIOUTrustLines == 0 ||
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.
A currency issued by an account.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual STAmount balanceHook(AccountID const &account, AccountID const &issuer, STAmount const &amount) const
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
void setIssuer(AccountID const &uIssuer)
std::string getFullText() const override
void push_back(STObject const &object)
AccountID getAccountID(SField const &field) const
void makeFieldAbsent(SField const &field)
void setFieldU16(SField const &field, std::uint16_t)
void setFieldAmount(SField const &field, STAmount const &)
bool isFieldPresent(SField const &field) const
static STObject makeInnerObject(SField const &name)
void setAccountID(SField const &field, AccountID const &)
void setFieldU32(SField const &field, std::uint32_t)
STBase const & peekAtField(SField const &field) const
Discardable, editable view to a ledger.
void erase(std::shared_ptr< SLE > const &sle) override
Remove a peeked SLE.
bool exists(Keylet const &k) const override
Determine if a state item exists.
std::shared_ptr< SLE > peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
T make_optional(T... args)
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
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 ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
FreezeHandling
Controls the treatment of frozen account balances.
std::uint32_t constexpr TOTAL_TIME_SLOT_SECS
bool isXRP(AccountID const &c)
TER deleteAMMTrustLine(ApplyView &view, std::shared_ptr< SLE > sleState, std::optional< AccountID > const &ammAccountID, beast::Journal j)
Delete trustline to AMM.
Currency ammLPTCurrency(Currency const &cur1, Currency const &cur2)
Calculate Liquidity Provider Token (LPT) Currency.
std::uint16_t getTradingFee(ReadView const &view, SLE const &ammSle, AccountID const &account)
Get AMM trading fee for the given account.
TER deleteAMMAccount(Sandbox &view, Issue const &asset, Issue const &asset2, beast::Journal j)
Delete trustlines to AMM.
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.
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
void initializeFeeAuctionVote(ApplyView &view, std::shared_ptr< SLE > &ammSle, AccountID const &account, Issue const &lptIssue, std::uint16_t tfee)
Initialize Auction and Voting slots and set the trading/discounted fee.
STAmount ammLPHolds(ReadView const &view, Currency const &cur1, Currency const &cur2, AccountID const &ammAccount, AccountID const &lpAccount, beast::Journal const j)
Get the balance of LP tokens.
std::pair< STAmount, STAmount > ammPoolHolds(ReadView const &view, AccountID const &ammAccountID, Issue const &issue1, Issue const &issue2, FreezeHandling freezeHandling, beast::Journal const j)
Get AMM pool balances.
static TER deleteAMMTrustLines(Sandbox &sb, AccountID const &ammAccountID, std::uint16_t maxTrustlinesToDelete, beast::Journal j)
std::uint32_t constexpr VOTE_WEIGHT_SCALE_FACTOR
NotTEC invalidAMMAssetPair(Issue const &issue1, Issue const &issue2, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
Expected< std::tuple< STAmount, STAmount, STAmount >, TER > ammHolds(ReadView const &view, SLE const &ammSle, std::optional< Issue > const &optIssue1, std::optional< Issue > const &optIssue2, FreezeHandling freezeHandling, beast::Journal const j)
Get AMM pool and LP token balances.
std::string to_string(base_uint< Bits, Tag > const &a)
LedgerEntryType
Identifiers for on-ledger objects.
TER cleanupOnAccountDelete(ApplyView &view, Keylet const &ownerDirKeylet, EntryDeleter const &deleter, beast::Journal j, std::optional< uint16_t > maxNodesToDelete)
STAmount ammAccountHolds(ReadView const &view, AccountID const &ammAccountID, Issue const &issue)
Returns total amount held by AMM for the given token.
Number root(Number f, unsigned d)
std::uint16_t constexpr maxDeletableAMMTrustLines
The maximum number of trustlines to delete as part of AMM account deletion cleanup.
TERSubset< CanCvtToTER > TER
std::uint32_t constexpr AUCTION_SLOT_DISCOUNTED_FEE_FRACTION
T time_since_epoch(T... args)