19#include <xrpld/app/misc/AMMUtils.h>
20#include <xrpld/ledger/Sandbox.h>
21#include <xrpl/basics/Log.h>
22#include <xrpl/protocol/AMMCore.h>
23#include <xrpl/protocol/STAccount.h>
24#include <xrpl/protocol/STObject.h>
37 auto const assetInBalance =
39 auto const assetOutBalance =
44Expected<std::tuple<STAmount, STAmount, STAmount>,
TER>
54 auto const issue1 = ammSle[sfAsset].get<
Issue>();
55 auto const issue2 = ammSle[sfAsset2].get<
Issue>();
56 if (optIssue1 && optIssue2)
65 JLOG(j.
debug()) <<
"ammHolds: Invalid optIssue1 or optIssue2 "
66 << *optIssue1 <<
" " << *optIssue2;
72 auto const singleIssue =
73 [&issue1, &issue2, &j](
76 if (checkIssue == issue1)
78 else if (checkIssue == issue2)
83 <<
"ammHolds: Invalid " << label <<
" " << checkIssue;
89 return singleIssue(*optIssue1,
"optIssue1");
94 return singleIssue(*optIssue2,
"optIssue2");
131 JLOG(j.
trace()) <<
"ammLPHolds: no SLE "
135 else if (
isFrozen(view, lpAccount, currency, ammAccount))
138 JLOG(j.
trace()) <<
"ammLPHolds: frozen currency "
144 amount = sle->getFieldAmount(sfBalance);
145 if (lpAccount > ammAccount)
152 JLOG(j.
trace()) <<
"ammLPHolds:"
157 return view.
balanceHook(lpAccount, ammAccount, amount);
169 ammSle[sfAsset].get<Issue>().currency,
170 ammSle[sfAsset2].get<Issue>().currency,
183 "ripple::getTradingFee : auction present");
186 auto const& auctionSlot =
189 if (
auto const expiration = auctionSlot[~sfExpiration];
190 duration_cast<seconds>(
192 .count() < expiration)
194 if (auctionSlot[~sfAccount] == account)
195 return auctionSlot[sfDiscountedFee];
196 if (auctionSlot.isFieldPresent(sfAuthAccounts))
198 for (
auto const& acct :
199 auctionSlot.getFieldArray(sfAuthAccounts))
200 if (acct[~sfAccount] == account)
201 return auctionSlot[sfDiscountedFee];
205 return ammSle[sfTradingFee];
217 return (*sle)[sfBalance];
219 else if (
auto const sle = view.
read(
224 auto amount = (*sle)[sfBalance];
227 amount.setIssuer(issue.
account);
248 if (nodeType == LedgerEntryType::ltAMM)
251 if (nodeType != LedgerEntryType::ltRIPPLE_STATE)
255 <<
"deleteAMMTrustLines: deleting non-trustline "
262 if (sleItem->getFieldAmount(sfBalance) != beast::zero)
266 <<
"deleteAMMTrustLines: deleting trustline with "
277 maxTrustlinesToDelete);
291 JLOG(j.
error()) <<
"deleteAMMAccount: AMM object does not exist "
292 << asset <<
" " << asset2;
302 JLOG(j.
error()) <<
"deleteAMMAccount: AMM account does not exist "
315 ownerDirKeylet, (*ammSle)[sfOwnerNode], ammSle->key(),
false))
318 JLOG(j.
error()) <<
"deleteAMMAccount: failed to remove dir link";
325 JLOG(j.
error()) <<
"deleteAMMAccount: cannot delete root dir node of "
332 sb.
erase(sleAMMRoot);
342 Issue const& lptIssue,
345 auto const& rules = view.
rules();
354 ammSle->setFieldArray(sfVoteSlots, voteSlots);
358 if (rules.enabled(fixInnerObjTemplate) &&
359 !ammSle->isFieldPresent(sfAuctionSlot))
362 ammSle->set(std::move(auctionSlot));
364 STObject& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
367 auto const expiration = std::chrono::duration_cast<std::chrono::seconds>(
375 ammSle->setFieldU16(sfTradingFee, tfee);
376 else if (ammSle->isFieldPresent(sfTradingFee))
377 ammSle->makeFieldAbsent(sfTradingFee);
387 Issue const& ammIssue,
405 auto currentIndex =
root;
410 auto const ownerDir = view.
read(currentIndex);
413 for (
auto const& key : ownerDir->getFieldV256(sfIndexes))
419 if (sle->getFieldU16(sfLedgerEntryType) == ltAMM)
426 if (sle->getFieldU16(sfLedgerEntryType) != ltRIPPLE_STATE)
428 auto const lowLimit = sle->getFieldAmount(sfLowLimit);
429 auto const highLimit = sle->getFieldAmount(sfHighLimit);
430 auto const isLPTrustline = lowLimit.getIssuer() == lpAccount ||
431 highLimit.getIssuer() == lpAccount;
432 auto const isLPTokenTrustline =
433 lowLimit.issue() == ammIssue || highLimit.issue() == ammIssue;
439 if (isLPTokenTrustline)
441 if (++nLPTokenTrustLines > 1)
444 else if (++nIOUTrustLines > 2)
448 else if (isLPTokenTrustline)
450 else if (++nIOUTrustLines > 2)
453 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
456 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 &)
const STBase & peekAtField(SField const &field) 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)
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)
AccountID ammAccountID(std::uint16_t prefix, uint256 const &parentHash, uint256 const &ammID)
Calculate AMM account ID.
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)