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))
39 XRPL_ASSERT(
validBook_,
"xrpl::TOfferStreamBase::TOfferStreamBase : valid book");
44template <
class TIn,
class TOut>
56 JLOG(j_.error()) <<
"Missing directory " << tip_.dir() <<
" for offer " << tip_.index();
60 auto v(p->getFieldV256(sfIndexes));
61 auto it(
std::find(v.begin(), v.end(), tip_.index()));
65 JLOG(j_.error()) <<
"Missing offer " << tip_.index() <<
" for directory " << tip_.dir();
70 p->setFieldV256(sfIndexes, v);
73 JLOG(j_.trace()) <<
"Missing offer " << tip_.index() <<
" removed from directory " << tip_.dir();
85 return accountFunds(view,
id, saDefault, freezeHandling, j);
116template <
class TIn,
class TOut>
117template <
class TTakerPays,
class TTakerGets>
136 if constexpr (outIsXRP)
145 TAmounts<TTakerPays, TTakerGets>
const ofrAmts{
146 toAmount<TTakerPays>(offer_.amount().in), toAmount<TTakerGets>(offer_.amount().out)};
148 if constexpr (!inIsXRP && !outIsXRP)
150 if (ofrAmts.in >= ofrAmts.out)
154 TTakerGets
const ownerFunds = toAmount<TTakerGets>(*ownerFunds_);
156 auto const effectiveAmounts = [&] {
157 if (offer_.owner() != offer_.issueOut().account && ownerFunds < ofrAmts.out)
163 return offer_.quality().ceil_out_strict(ofrAmts, ownerFunds,
false);
169 if (effectiveAmounts.in.signum() <= 0 || effectiveAmounts.out.signum() <= 0)
172 if (effectiveAmounts.in > TTakerPays::minPositiveAmount())
175 Quality
const effectiveQuality{effectiveAmounts};
176 return effectiveQuality < offer_.quality();
179template <
class TIn,
class TOut>
200 if (!counter_.step())
214 if (entry->isFieldPresent(sfExpiration) && tp{d{(*entry)[sfExpiration]}} <= expire_)
216 JLOG(j_.trace()) <<
"Removing expired offer " << entry->key();
217 permRmOffer(entry->key());
223 auto const amount(offer_.amount());
228 JLOG(j_.warn()) <<
"Removing bad offer " << entry->key();
229 permRmOffer(entry->key());
234 bool const deepFrozen =
235 isDeepFrozen(view_, offer_.owner(), offer_.issueIn().currency, offer_.issueIn().account);
238 JLOG(j_.trace()) <<
"Removing deep frozen unfunded offer " << entry->key();
239 permRmOffer(entry->key());
244 if (entry->isFieldPresent(sfDomainID) &&
247 JLOG(j_.trace()) <<
"Removing offer no longer in domain " << entry->key();
248 permRmOffer(entry->key());
257 if (*ownerFunds_ <= beast::zero)
262 auto const original_funds =
265 if (original_funds == *ownerFunds_)
267 permRmOffer(entry->key());
268 JLOG(j_.trace()) <<
"Removing unfunded offer " << entry->key();
272 JLOG(j_.trace()) <<
"Removing became unfunded offer " << entry->key();
279 bool const rmSmallIncreasedQOffer = [&] {
280 bool const inIsXRP =
isXRP(offer_.issueIn());
281 bool const outIsXRP =
isXRP(offer_.issueOut());
282 if (inIsXRP && !outIsXRP)
291 return shouldRmSmallIncreasedQOffer<XRPAmount, IOUAmount>();
293 if (!inIsXRP && outIsXRP)
297 return shouldRmSmallIncreasedQOffer<IOUAmount, XRPAmount>();
299 if (!inIsXRP && !outIsXRP)
303 return shouldRmSmallIncreasedQOffer<IOUAmount, IOUAmount>();
307 "xrpl::TOfferStreamBase::step::rmSmallIncreasedQOffer : XRP "
313 if (rmSmallIncreasedQOffer)
315 auto const original_funds =
318 if (original_funds == *ownerFunds_)
320 permRmOffer(entry->key());
321 JLOG(j_.trace()) <<
"Removing tiny offer due to reduced quality " << entry->key();
325 JLOG(j_.trace()) <<
"Removing tiny offer that became tiny due "
326 "to reduced quality "
341OfferStream::permRmOffer(
uint256 const& offerIndex)
343 offerDelete(cancelView_, cancelView_.peek(keylet::offer(offerIndex)), j_);
346template <
class TIn,
class TOut>
350 permToRemove_.insert(offerIndex);
A generic endpoint for log messages.
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.