1#include <xrpl/basics/Log.h>
2#include <xrpl/ledger/View.h>
3#include <xrpl/protocol/Feature.h>
4#include <xrpl/protocol/LedgerFormats.h>
5#include <xrpl/tx/paths/OfferStream.h>
6#include <xrpl/tx/transactors/PermissionedDEXHelpers.h>
12checkIssuers(ReadView
const& view, Book
const& book)
14 auto issuerExists = [](ReadView
const& view, Issue
const& iss) ->
bool {
17 return issuerExists(view, book.in) && issuerExists(view, book.out);
21template <
class TIn,
class TOut>
31 , cancelView_(cancelView)
33 , validBook_(checkIssuers(view, book))
38 XRPL_ASSERT(
validBook_,
"xrpl::TOfferStreamBase::TOfferStreamBase : valid book");
43template <
class TIn,
class TOut>
55 JLOG(j_.
error()) <<
"Missing directory " << tip_.dir() <<
" for offer " << tip_.index();
59 auto v(p->getFieldV256(sfIndexes));
60 auto it(
std::find(v.begin(), v.end(), tip_.index()));
64 JLOG(j_.
error()) <<
"Missing offer " << tip_.index() <<
" for directory " << tip_.dir();
69 p->setFieldV256(sfIndexes, v);
72 JLOG(j_.
trace()) <<
"Missing offer " << tip_.index() <<
" removed from directory "
85 return accountFunds(view,
id, saDefault, freezeHandling, j);
118template <
class TIn,
class TOut>
119template <
class TTakerPays,
class TTakerGets>
125 "STAmount is not supported");
129 "STAmount is not supported");
133 "Cannot have XRP/XRP offers");
141 if constexpr (outIsXRP)
150 TAmounts<TTakerPays, TTakerGets>
const ofrAmts{
151 toAmount<TTakerPays>(offer_.amount().in), toAmount<TTakerGets>(offer_.amount().out)};
153 if constexpr (!inIsXRP && !outIsXRP)
155 if (ofrAmts.in >= ofrAmts.out)
159 TTakerGets
const ownerFunds = toAmount<TTakerGets>(*ownerFunds_);
161 auto const effectiveAmounts = [&] {
162 if (offer_.owner() != offer_.issueOut().account && ownerFunds < ofrAmts.out)
168 return offer_.quality().ceil_out_strict(ofrAmts, ownerFunds,
false);
174 if (effectiveAmounts.in.signum() <= 0 || effectiveAmounts.out.signum() <= 0)
177 if (effectiveAmounts.in > TTakerPays::minPositiveAmount())
180 Quality
const effectiveQuality{effectiveAmounts};
181 return effectiveQuality < offer_.quality();
184template <
class TIn,
class TOut>
205 if (!counter_.step())
219 if (entry->isFieldPresent(sfExpiration) && tp{d{(*entry)[sfExpiration]}} <= expire_)
221 JLOG(j_.
trace()) <<
"Removing expired offer " << entry->key();
222 permRmOffer(entry->key());
228 auto const amount(offer_.amount());
233 JLOG(j_.
warn()) <<
"Removing bad offer " << entry->key();
234 permRmOffer(entry->key());
240 view_, offer_.owner(), offer_.issueIn().currency, offer_.issueIn().account);
243 JLOG(j_.
trace()) <<
"Removing deep frozen unfunded offer " << entry->key();
244 permRmOffer(entry->key());
249 if (entry->isFieldPresent(sfDomainID) &&
251 view_, entry->key(), entry->getFieldH256(sfDomainID), j_))
253 JLOG(j_.
trace()) <<
"Removing offer no longer in domain " << entry->key();
254 permRmOffer(entry->key());
261 view_, offer_.owner(), amount.out, offer_.issueOut(),
fhZERO_IF_FROZEN, j_);
264 if (*ownerFunds_ <= beast::zero)
270 cancelView_, offer_.owner(), amount.out, offer_.issueOut(),
fhZERO_IF_FROZEN, j_);
272 if (original_funds == *ownerFunds_)
274 permRmOffer(entry->key());
275 JLOG(j_.
trace()) <<
"Removing unfunded offer " << entry->key();
279 JLOG(j_.
trace()) <<
"Removing became unfunded offer " << entry->key();
286 bool const rmSmallIncreasedQOffer = [&] {
287 bool const inIsXRP =
isXRP(offer_.issueIn());
288 bool const outIsXRP =
isXRP(offer_.issueOut());
289 if (inIsXRP && !outIsXRP)
298 return shouldRmSmallIncreasedQOffer<XRPAmount, IOUAmount>();
300 if (!inIsXRP && outIsXRP)
304 return shouldRmSmallIncreasedQOffer<IOUAmount, XRPAmount>();
306 if (!inIsXRP && !outIsXRP)
310 return shouldRmSmallIncreasedQOffer<IOUAmount, IOUAmount>();
314 "xrpl::TOfferStreamBase::step::rmSmallIncreasedQOffer : XRP "
320 if (rmSmallIncreasedQOffer)
323 cancelView_, offer_.owner(), amount.out, offer_.issueOut(),
fhZERO_IF_FROZEN, j_);
325 if (original_funds == *ownerFunds_)
327 permRmOffer(entry->key());
328 JLOG(j_.
trace()) <<
"Removing tiny offer due to reduced quality " << entry->key();
332 JLOG(j_.
trace()) <<
"Removing tiny offer that became tiny due "
333 "to reduced quality "
348OfferStream::permRmOffer(
uint256 const& offerIndex)
350 offerDelete(cancelView_, cancelView_.peek(keylet::offer(offerIndex)), j_);
353template <
class TIn,
class TOut>
357 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
TOfferStreamBase(ApplyView &view, ApplyView &cancelView, Book const &book, NetClock::time_point when, StepCounter &counter, beast::Journal journal)
bool shouldRmSmallIncreasedQOffer() const
bool step()
Advance to the next valid offer.
void erase(ApplyView &view)
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.
FreezeHandling
Controls the treatment of frozen account balances.
bool isXRP(AccountID const &c)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
XRPAmount toAmount< XRPAmount >(STAmount const &amt)
static STAmount accountFundsHelper(ReadView const &view, AccountID const &id, STAmount const &saDefault, Issue const &, FreezeHandling freezeHandling, beast::Journal j)
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.