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");
137 ammSle[sfAsset].get<Issue>().currency,
138 ammSle[sfAsset2].get<Issue>().currency,
151 "ripple::getTradingFee : auction present");
154 auto const& auctionSlot =
157 if (
auto const expiration = auctionSlot[~sfExpiration];
158 duration_cast<seconds>(
160 .count() < expiration)
162 if (auctionSlot[~sfAccount] == account)
163 return auctionSlot[sfDiscountedFee];
164 if (auctionSlot.isFieldPresent(sfAuthAccounts))
166 for (
auto const& acct :
167 auctionSlot.getFieldArray(sfAuthAccounts))
168 if (acct[~sfAccount] == account)
169 return auctionSlot[sfDiscountedFee];
173 return ammSle[sfTradingFee];
185 return (*sle)[sfBalance];
187 else if (
auto const sle = view.
read(
192 auto amount = (*sle)[sfBalance];
195 amount.setIssuer(issue.
account);
216 if (nodeType == LedgerEntryType::ltAMM)
219 if (nodeType != LedgerEntryType::ltRIPPLE_STATE)
223 <<
"deleteAMMTrustLines: deleting non-trustline "
230 if (sleItem->getFieldAmount(sfBalance) != beast::zero)
234 <<
"deleteAMMTrustLines: deleting trustline with "
245 maxTrustlinesToDelete);
259 JLOG(j.
error()) <<
"deleteAMMAccount: AMM object does not exist "
260 << asset <<
" " << asset2;
270 JLOG(j.
error()) <<
"deleteAMMAccount: AMM account does not exist "
283 ownerDirKeylet, (*ammSle)[sfOwnerNode], ammSle->key(),
false))
286 JLOG(j.
error()) <<
"deleteAMMAccount: failed to remove dir link";
293 JLOG(j.
error()) <<
"deleteAMMAccount: cannot delete root dir node of "
300 sb.
erase(sleAMMRoot);
310 Issue const& lptIssue,
313 auto const& rules = view.
rules();
322 ammSle->setFieldArray(sfVoteSlots, voteSlots);
326 if (rules.enabled(fixInnerObjTemplate) &&
327 !ammSle->isFieldPresent(sfAuctionSlot))
330 ammSle->set(std::move(auctionSlot));
332 STObject& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
335 auto const expiration = std::chrono::duration_cast<std::chrono::seconds>(
343 ammSle->setFieldU16(sfTradingFee, tfee);
344 else if (ammSle->isFieldPresent(sfTradingFee))
345 ammSle->makeFieldAbsent(sfTradingFee);
355 Issue const& ammIssue,
373 auto currentIndex =
root;
378 auto const ownerDir = view.
read(currentIndex);
381 for (
auto const& key : ownerDir->getFieldV256(sfIndexes))
387 if (sle->getFieldU16(sfLedgerEntryType) == ltAMM)
394 if (sle->getFieldU16(sfLedgerEntryType) != ltRIPPLE_STATE)
396 auto const lowLimit = sle->getFieldAmount(sfLowLimit);
397 auto const highLimit = sle->getFieldAmount(sfHighLimit);
398 auto const isLPTrustline = lowLimit.getIssuer() == lpAccount ||
399 highLimit.getIssuer() == lpAccount;
400 auto const isLPTokenTrustline =
401 lowLimit.issue() == ammIssue || highLimit.issue() == ammIssue;
407 if (isLPTokenTrustline)
409 if (++nLPTokenTrustLines > 1)
412 else if (++nIOUTrustLines > 2)
416 else if (isLPTokenTrustline)
418 else if (++nIOUTrustLines > 2)
421 auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext);
424 if (nLPTokenTrustLines != 1 || nIOUTrustLines == 0 ||
A generic endpoint for log messages.
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 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 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)