20#include <xrpld/app/paths/AMMLiquidity.h>
21#include <xrpld/app/paths/AMMOffer.h>
23#include <xrpl/protocol/QualityFunction.h>
27template <
typename TIn,
typename TOut>
30 TAmounts<TIn, TOut>
const& amounts,
31 TAmounts<TIn, TOut>
const& balances,
32 Quality
const& quality)
33 : ammLiquidity_(ammLiquidity)
41template <
typename TIn,
typename TOut>
45 return ammLiquidity_.issueIn();
48template <
typename TIn,
typename TOut>
52 return ammLiquidity_.ammAccount();
55template <
typename TIn,
typename TOut>
56TAmounts<TIn, TOut>
const&
62template <
typename TIn,
typename TOut>
66 TAmounts<TIn, TOut>
const& consumed)
69 if (consumed.in > amounts_.in || consumed.out > amounts_.out)
70 Throw<std::logic_error>(
"Invalid consumed AMM offer.");
77 ammLiquidity_.context().setAMMUsed();
80template <
typename TIn,
typename TOut>
83 TAmounts<TIn, TOut>
const& offrAmt,
93 if (ammLiquidity_.multiPath())
96 rules && rules->enabled(fixReducedOffersV1))
101 return quality().ceil_out_strict(offrAmt, limit, roundUp);
102 return quality().ceil_out(offrAmt, limit);
107 return {
swapAssetOut(balances_, limit, ammLiquidity_.tradingFee()), limit};
110template <
typename TIn,
typename TOut>
113 TAmounts<TIn, TOut>
const& offrAmt,
118 if (ammLiquidity_.multiPath())
121 rules && rules->enabled(fixReducedOffersV2))
122 return quality().ceil_in_strict(offrAmt, limit, roundUp);
124 return quality().ceil_in(offrAmt, limit);
126 return {limit,
swapAssetIn(balances_, limit, ammLiquidity_.tradingFee())};
129template <
typename TIn,
typename TOut>
133 if (ammLiquidity_.multiPath())
139template <
typename TIn,
typename TOut>
142 TAmounts<TIn, TOut>
const& consumed,
145 if (consumed.in > amounts_.in || consumed.out > amounts_.out)
147 JLOG(j.
error()) <<
"AMMOffer::checkInvariant failed: consumed "
149 <<
to_string(consumed.out) <<
" amounts "
156 Number const product = balances_.in * balances_.out;
157 auto const newBalances = TAmounts<TIn, TOut>{
158 balances_.in + consumed.in, balances_.out - consumed.out};
159 Number const newProduct = newBalances.in * newBalances.out;
161 if (newProduct >= product ||
165 JLOG(j.
error()) <<
"AMMOffer::checkInvariant failed: balances "
167 <<
to_string(balances_.out) <<
" new balances "
169 <<
to_string(newBalances.out) <<
" product/newProduct "
170 << product <<
" " << newProduct <<
" diff "
172 ?
to_string((product - newProduct) / product)
A generic endpoint for log messages.
AMMLiquidity class provides AMM offers to BookStep class.
Represents synthetic AMM offer in BookStep.
AMMOffer(AMMLiquidity< TIn, TOut > const &ammLiquidity, TAmounts< TIn, TOut > const &amounts, TAmounts< TIn, TOut > const &balances, Quality const &quality)
AccountID const & owner() const
TAmounts< TIn, TOut > limitOut(TAmounts< TIn, TOut > const &offrAmt, TOut const &limit, bool roundUp) const
Limit out of the provided offer.
bool checkInvariant(TAmounts< TIn, TOut > const &consumed, beast::Journal j) const
Check the new pool product is greater or equal to the old pool product or if decreases then within so...
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
TAmounts< TIn, TOut > limitIn(TAmounts< TIn, TOut > const &offrAmt, TIn const &limit, bool roundUp) const
Limit in of the provided offer.
Issue const & issueIn() const
TAmounts< TIn, TOut > const & amount() const
QualityFunction getQualityFunc() const
Writeable view to a ledger, for applying a transaction.
A currency issued by an account.
Average quality of a path as a function of out: q(out) = m * out + b, where m = -1 / poolGets,...
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
TOut swapAssetIn(TAmounts< TIn, TOut > const &pool, TIn const &assetIn, std::uint16_t tfee)
AMM pool invariant - the product (A * B) after swap in/out has to remain at least the same: (A + in) ...
std::string to_string(base_uint< Bits, Tag > const &a)
std::optional< Rules > const & getCurrentTransactionRules()
bool withinRelativeDistance(Quality const &calcQuality, Quality const &reqQuality, Number const &dist)
Check if the relative distance between the qualities is within the requested distance.
TIn swapAssetOut(TAmounts< TIn, TOut > const &pool, TOut const &assetOut, std::uint16_t tfee)
Swap assetOut out of the pool and swap in a proportional amount of the other asset.