1#ifndef XRPL_APP_BOOK_OFFER_H_INCLUDED
2#define XRPL_APP_BOOK_OFFER_H_INCLUDED
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/ledger/View.h>
7#include <xrpl/protocol/Quality.h>
8#include <xrpl/protocol/Rules.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STLedgerEntry.h>
16template <
class TIn,
class TOut>
31template <
class TIn = STAmount,
class TOut = STAmount>
73 TAmounts<TIn, TOut>
const&
95 Throw<std::logic_error>(
"can't consume more than is available.");
98 Throw<std::logic_error>(
"can't produce more than is available.");
124 TAmounts<TIn, TOut>
const& offerAmount,
130 TAmounts<TIn, TOut>
const& offerAmount,
134 template <
typename... Args>
149 return {ofrInRate, ofrOutRate};
165 <<
"AMMOffer::checkInvariant failed: consumed "
180template <
class TIn,
class TOut>
184 , m_account(m_entry->getAccountID(sfAccount))
186 auto const tp =
m_entry->getFieldAmount(sfTakerPays);
187 auto const tg =
m_entry->getFieldAmount(sfTakerGets);
190 this->
issIn_ = tp.issue();
200 , m_account(m_entry->getAccountID(sfAccount))
202 m_entry->getFieldAmount(sfTakerPays),
203 m_entry->getFieldAmount(sfTakerGets))
207template <
class TIn,
class TOut>
213 UNREACHABLE(
"xrpl::TOffer::setFieldAmounts : must be specialized");
215 static_assert(
sizeof(TOut) == -1,
"Must be specialized");
220template <
class TIn,
class TOut>
223 TAmounts<TIn, TOut>
const& offerAmount,
229 return quality().ceil_out_strict(offerAmount, limit, roundUp);
232template <
class TIn,
class TOut>
235 TAmounts<TIn, TOut>
const& offerAmount,
240 rules && rules->enabled(fixReducedOffersV2))
245 return quality().ceil_in_strict(offerAmount, limit, roundUp);
246 return m_quality.ceil_in(offerAmount, limit);
249template <
class TIn,
class TOut>
250template <
typename... Args>
261 m_entry->setFieldAmount(sfTakerPays, m_amounts.in);
262 m_entry->setFieldAmount(sfTakerGets, m_amounts.out);
269 m_entry->setFieldAmount(sfTakerPays,
toSTAmount(m_amounts.in, issIn_));
270 m_entry->setFieldAmount(sfTakerGets,
toSTAmount(m_amounts.out, issOut_));
277 m_entry->setFieldAmount(sfTakerPays,
toSTAmount(m_amounts.in, issIn_));
278 m_entry->setFieldAmount(sfTakerGets,
toSTAmount(m_amounts.out));
285 m_entry->setFieldAmount(sfTakerPays,
toSTAmount(m_amounts.in));
286 m_entry->setFieldAmount(sfTakerGets,
toSTAmount(m_amounts.out, issOut_));
289template <
class TIn,
class TOut>
300 return m_amounts.in.issue();
303template <
class TIn,
class TOut>
307 return this->issOut_;
314 return m_amounts.out.issue();
317template <
class TIn,
class TOut>
321 return os << offer.id();
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.
A currency issued by an account.
Issue const & issueOut() const
static TER send(Args &&... args)
bool fully_consumed() const
Returns true if no more funds can flow through this offer.
AccountID const & owner() const
Returns the account id of the offer's owner.
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
Adjusts the offer to indicate that we consumed some (or all) of it.
static std::pair< std::uint32_t, std::uint32_t > adjustRates(std::uint32_t ofrInRate, std::uint32_t ofrOutRate)
std::optional< uint256 > key() const
Quality quality() const noexcept
Returns the quality of the offer.
TAmounts< TIn, TOut > m_amounts
TOffer(SLE::pointer const &entry, Quality quality)
TAmounts< TIn, TOut > limitOut(TAmounts< TIn, TOut > const &offerAmount, TOut const &limit, bool roundUp) const
bool checkInvariant(TAmounts< TIn, TOut > const &consumed, beast::Journal j) const
Check any required invariant.
TAmounts< TIn, TOut > const & amount() const
Returns the in and out amounts.
Issue const & issueIn() const
TAmounts< TIn, TOut > limitIn(TAmounts< TIn, TOut > const &offerAmount, TIn const &limit, bool roundUp) const
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string to_string(base_uint< Bits, Tag > const &a)
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
std::optional< Rules > const & getCurrentTransactionRules()
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
bool isFeatureEnabled(uint256 const &feature)