20#include <xrpld/app/misc/PermissionedDEXHelpers.h>
21#include <xrpld/app/tx/detail/OfferStream.h>
23#include <xrpl/basics/Log.h>
24#include <xrpl/ledger/View.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/LedgerFormats.h>
32checkIssuers(ReadView
const& view, Book
const& book)
34 auto issuerExists = [](ReadView
const& view, Issue
const& iss) ->
bool {
37 return issuerExists(view, book.in) && issuerExists(view, book.out);
41template <
class TIn,
class TOut>
51 , cancelView_(cancelView)
53 , validBook_(checkIssuers(view, book))
59 validBook_,
"ripple::TOfferStreamBase::TOfferStreamBase : valid book");
64template <
class TIn,
class TOut>
76 JLOG(j_.
error()) <<
"Missing directory " << tip_.dir() <<
" for offer "
81 auto v(p->getFieldV256(sfIndexes));
82 auto it(
std::find(v.begin(), v.end(), tip_.index()));
86 JLOG(j_.
error()) <<
"Missing offer " << tip_.index()
87 <<
" for directory " << tip_.dir();
92 p->setFieldV256(sfIndexes, v);
95 JLOG(j_.
trace()) <<
"Missing offer " << tip_.index()
96 <<
" removed from directory " << tip_.dir();
108 return accountFunds(view,
id, saDefault, freezeHandling, j);
141template <
class TIn,
class TOut>
142template <
class TTakerPays,
class TTakerGets>
149 "STAmount is not supported");
154 "STAmount is not supported");
159 "Cannot have XRP/XRP offers");
161 if (!view_.rules().enabled(fixRmSmallIncreasedQOffers))
170 if constexpr (outIsXRP)
179 TAmounts<TTakerPays, TTakerGets>
const ofrAmts{
180 toAmount<TTakerPays>(offer_.amount().in),
181 toAmount<TTakerGets>(offer_.amount().out)};
183 if constexpr (!inIsXRP && !outIsXRP)
185 if (ofrAmts.in >= ofrAmts.out)
189 TTakerGets
const ownerFunds = toAmount<TTakerGets>(*ownerFunds_);
190 bool const fixReduced = view_.rules().enabled(fixReducedOffersV1);
192 auto const effectiveAmounts = [&] {
193 if (offer_.owner() != offer_.issueOut().account &&
194 ownerFunds < ofrAmts.out)
202 return offer_.quality().ceil_out_strict(
203 ofrAmts, ownerFunds,
false);
205 return offer_.quality().ceil_out(ofrAmts, ownerFunds);
213 (effectiveAmounts.in.signum() <= 0 ||
214 effectiveAmounts.out.signum() <= 0))
217 if (effectiveAmounts.in > TTakerPays::minPositiveAmount())
220 Quality
const effectiveQuality{effectiveAmounts};
221 return effectiveQuality < offer_.quality();
224template <
class TIn,
class TOut>
245 if (!counter_.step())
259 if (entry->isFieldPresent(sfExpiration) &&
260 tp{d{(*entry)[sfExpiration]}} <= expire_)
262 JLOG(j_.
trace()) <<
"Removing expired offer " << entry->key();
263 permRmOffer(entry->key());
269 auto const amount(offer_.amount());
274 JLOG(j_.
warn()) <<
"Removing bad offer " << entry->key();
275 permRmOffer(entry->key());
283 offer_.issueIn().currency,
284 offer_.issueIn().account);
288 <<
"Removing deep frozen unfunded offer " << entry->key();
289 permRmOffer(entry->key());
294 if (entry->isFieldPresent(sfDomainID) &&
296 view_, entry->key(), entry->getFieldH256(sfDomainID), j_))
299 <<
"Removing offer no longer in domain " << entry->key();
300 permRmOffer(entry->key());
315 if (*ownerFunds_ <= beast::zero)
328 if (original_funds == *ownerFunds_)
330 permRmOffer(entry->key());
331 JLOG(j_.
trace()) <<
"Removing unfunded offer " << entry->key();
336 <<
"Removing became unfunded offer " << entry->key();
343 bool const rmSmallIncreasedQOffer = [&] {
344 bool const inIsXRP =
isXRP(offer_.issueIn());
345 bool const outIsXRP =
isXRP(offer_.issueOut());
346 if (inIsXRP && !outIsXRP)
356 return shouldRmSmallIncreasedQOffer<XRPAmount, IOUAmount>();
358 if (!inIsXRP && outIsXRP)
363 return shouldRmSmallIncreasedQOffer<IOUAmount, XRPAmount>();
365 if (!inIsXRP && !outIsXRP)
370 return shouldRmSmallIncreasedQOffer<IOUAmount, IOUAmount>();
373 "rippls::TOfferStreamBase::step::rmSmallIncreasedQOffer : XRP "
378 if (rmSmallIncreasedQOffer)
388 if (original_funds == *ownerFunds_)
390 permRmOffer(entry->key());
392 <<
"Removing tiny offer due to reduced quality "
397 JLOG(j_.
trace()) <<
"Removing tiny offer that became tiny due "
398 "to reduced quality "
413OfferStream::permRmOffer(
uint256 const& offerIndex)
415 offerDelete(cancelView_, cancelView_.peek(keylet::offer(offerIndex)), j_);
418template <
class TIn,
class TOut>
422 permToRemove_.insert(offerIndex);
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Writeable view to a ledger, for applying a transaction.
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
Presents and consumes the offers in an order book.
Floating point representation of amounts with high dynamic range.
A currency issued by an account.
std::chrono::time_point< NetClock > time_point
std::chrono::duration< rep, period > duration
void erase(ApplyView &view)
bool step()
Advance to the next valid offer.
TOfferStreamBase(ApplyView &view, ApplyView &cancelView, Book const &book, NetClock::time_point when, StepCounter &counter, beast::Journal journal)
bool shouldRmSmallIncreasedQOffer() const
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
bool offerInDomain(ReadView const &view, uint256 const &offerID, Domain const &domainID, beast::Journal j)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
FreezeHandling
Controls the treatment of frozen account balances.
bool isXRP(AccountID const &c)
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
static STAmount accountFundsHelper(ReadView const &view, AccountID const &id, STAmount const &saDefault, Issue const &, FreezeHandling freezeHandling, beast::Journal j)
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
XRPAmount toAmount< XRPAmount >(STAmount const &amt)
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.