1#include <xrpld/app/misc/PermissionedDEXHelpers.h>
2#include <xrpld/app/tx/detail/OfferStream.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/ledger/View.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/LedgerFormats.h>
13checkIssuers(ReadView
const& view, Book
const& book)
15 auto issuerExists = [](ReadView
const& view, Issue
const& iss) ->
bool {
18 return issuerExists(view, book.in) && issuerExists(view, book.out);
22template <
class TIn,
class TOut>
32 , cancelView_(cancelView)
34 , validBook_(checkIssuers(view, book))
40 validBook_,
"ripple::TOfferStreamBase::TOfferStreamBase : valid book");
45template <
class TIn,
class TOut>
57 JLOG(j_.
error()) <<
"Missing directory " << tip_.dir() <<
" for offer "
62 auto v(p->getFieldV256(sfIndexes));
63 auto it(
std::find(v.begin(), v.end(), tip_.index()));
67 JLOG(j_.
error()) <<
"Missing offer " << tip_.index()
68 <<
" for directory " << tip_.dir();
73 p->setFieldV256(sfIndexes, v);
76 JLOG(j_.
trace()) <<
"Missing offer " << tip_.index()
77 <<
" removed from directory " << tip_.dir();
89 return accountFunds(view,
id, saDefault, freezeHandling, j);
122template <
class TIn,
class TOut>
123template <
class TTakerPays,
class TTakerGets>
130 "STAmount is not supported");
135 "STAmount is not supported");
140 "Cannot have XRP/XRP offers");
148 if constexpr (outIsXRP)
157 TAmounts<TTakerPays, TTakerGets>
const ofrAmts{
158 toAmount<TTakerPays>(offer_.amount().in),
159 toAmount<TTakerGets>(offer_.amount().out)};
161 if constexpr (!inIsXRP && !outIsXRP)
163 if (ofrAmts.in >= ofrAmts.out)
167 TTakerGets
const ownerFunds = toAmount<TTakerGets>(*ownerFunds_);
169 auto const effectiveAmounts = [&] {
170 if (offer_.owner() != offer_.issueOut().account &&
171 ownerFunds < ofrAmts.out)
177 return offer_.quality().ceil_out_strict(
178 ofrAmts, ownerFunds,
false);
184 if (effectiveAmounts.in.signum() <= 0 || effectiveAmounts.out.signum() <= 0)
187 if (effectiveAmounts.in > TTakerPays::minPositiveAmount())
190 Quality
const effectiveQuality{effectiveAmounts};
191 return effectiveQuality < offer_.quality();
194template <
class TIn,
class TOut>
215 if (!counter_.step())
229 if (entry->isFieldPresent(sfExpiration) &&
230 tp{d{(*entry)[sfExpiration]}} <= expire_)
232 JLOG(j_.
trace()) <<
"Removing expired offer " << entry->key();
233 permRmOffer(entry->key());
239 auto const amount(offer_.amount());
244 JLOG(j_.
warn()) <<
"Removing bad offer " << entry->key();
245 permRmOffer(entry->key());
253 offer_.issueIn().currency,
254 offer_.issueIn().account);
258 <<
"Removing deep frozen unfunded offer " << entry->key();
259 permRmOffer(entry->key());
264 if (entry->isFieldPresent(sfDomainID) &&
266 view_, entry->key(), entry->getFieldH256(sfDomainID), j_))
269 <<
"Removing offer no longer in domain " << entry->key();
270 permRmOffer(entry->key());
285 if (*ownerFunds_ <= beast::zero)
298 if (original_funds == *ownerFunds_)
300 permRmOffer(entry->key());
301 JLOG(j_.
trace()) <<
"Removing unfunded offer " << entry->key();
306 <<
"Removing became unfunded offer " << entry->key();
313 bool const rmSmallIncreasedQOffer = [&] {
314 bool const inIsXRP =
isXRP(offer_.issueIn());
315 bool const outIsXRP =
isXRP(offer_.issueOut());
316 if (inIsXRP && !outIsXRP)
326 return shouldRmSmallIncreasedQOffer<XRPAmount, IOUAmount>();
328 if (!inIsXRP && outIsXRP)
333 return shouldRmSmallIncreasedQOffer<IOUAmount, XRPAmount>();
335 if (!inIsXRP && !outIsXRP)
340 return shouldRmSmallIncreasedQOffer<IOUAmount, IOUAmount>();
344 "rippls::TOfferStreamBase::step::rmSmallIncreasedQOffer : XRP "
350 if (rmSmallIncreasedQOffer)
360 if (original_funds == *ownerFunds_)
362 permRmOffer(entry->key());
364 <<
"Removing tiny offer due to reduced quality "
369 JLOG(j_.
trace()) <<
"Removing tiny offer that became tiny due "
370 "to reduced quality "
385OfferStream::permRmOffer(
uint256 const& offerIndex)
387 offerDelete(cancelView_, cancelView_.peek(keylet::offer(offerIndex)), j_);
390template <
class TIn,
class TOut>
394 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.