1#include <xrpl/protocol/QualityFunction.h>
2#include <xrpl/tx/paths/AMMLiquidity.h>
3#include <xrpl/tx/paths/AMMOffer.h>
7template <
typename TIn,
typename TOut>
10 TAmounts<TIn, TOut>
const& amounts,
11 TAmounts<TIn, TOut>
const& balances,
12 Quality
const& quality)
13 : ammLiquidity_(ammLiquidity), amounts_(amounts), balances_(balances), quality_(quality)
18template <
typename TIn,
typename TOut>
22 return ammLiquidity_.issueIn();
25template <
typename TIn,
typename TOut>
29 return ammLiquidity_.ammAccount();
32template <
typename TIn,
typename TOut>
33TAmounts<TIn, TOut>
const&
39template <
typename TIn,
typename TOut>
44 if (consumed.in > amounts_.in || consumed.out > amounts_.out)
45 Throw<std::logic_error>(
"Invalid consumed AMM offer.");
52 ammLiquidity_.context().setAMMUsed();
55template <
typename TIn,
typename TOut>
58 TAmounts<TIn, TOut>
const& offerAmount,
68 if (ammLiquidity_.multiPath())
72 return quality().ceil_out_strict(offerAmount, limit, roundUp);
77 return {
swapAssetOut(balances_, limit, ammLiquidity_.tradingFee()), limit};
80template <
typename TIn,
typename TOut>
86 if (ammLiquidity_.multiPath())
89 rules && rules->enabled(fixReducedOffersV2))
90 return quality().ceil_in_strict(offerAmount, limit, roundUp);
92 return quality().ceil_in(offerAmount, limit);
94 return {limit,
swapAssetIn(balances_, limit, ammLiquidity_.tradingFee())};
97template <
typename TIn,
typename TOut>
101 if (ammLiquidity_.multiPath())
106template <
typename TIn,
typename TOut>
110 if (consumed.in > amounts_.in || consumed.out > amounts_.out)
112 JLOG(j.
error()) <<
"AMMOffer::checkInvariant failed: consumed " <<
to_string(consumed.in)
119 Number const product = balances_.in * balances_.out;
120 auto const newBalances =
121 TAmounts<TIn, TOut>{balances_.in + consumed.in, balances_.out - consumed.out};
122 Number const newProduct = newBalances.in * newBalances.out;
127 JLOG(j.
error()) <<
"AMMOffer::checkInvariant failed: balances " <<
to_string(balances_.in)
128 <<
" " <<
to_string(balances_.out) <<
" new balances "
130 <<
" product/newProduct " << product <<
" " << newProduct <<
" diff "
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)
void consume(ApplyView &view, TAmounts< TIn, TOut > const &consumed)
TAmounts< TIn, TOut > const & amount() const
QualityFunction getQualityFunc() const
TAmounts< TIn, TOut > limitOut(TAmounts< TIn, TOut > const &offerAmount, TOut const &limit, bool roundUp) const
Limit out of the provided offer.
Issue const & issueIn() const
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...
AccountID const & owner() const
TAmounts< TIn, TOut > limitIn(TAmounts< TIn, TOut > const &offerAmount, TIn const &limit, bool roundUp) const
Limit in of the provided offer.
Writeable view to a ledger, for applying a transaction.
A currency issued by an account.
Number is a floating point type that can represent a wide range of values.
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.
std::string to_string(base_uint< Bits, Tag > const &a)
std::optional< Rules > const & getCurrentTransactionRules()
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) ...
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.
bool withinRelativeDistance(Quality const &calcQuality, Quality const &reqQuality, Number const &dist)
Check if the relative distance between the qualities is within the requested distance.