20#include <xrpld/app/paths/AMMLiquidity.h>
21#include <xrpld/app/paths/AMMOffer.h>
25template <
typename TIn,
typename TOut>
34 : ammContext_(ammContext)
35 , ammAccountID_(ammAccountID)
36 , tradingFee_(tradingFee)
39 , initialBalances_{fetchBalances(view)}
44template <
typename TIn,
typename TOut>
51 if (assetIn < beast::zero || assetOut < beast::zero)
52 Throw<std::runtime_error>(
"AMMLiquidity: invalid balances");
54 return TAmounts{get<TIn>(assetIn), get<TOut>(assetOut)};
57template <
typename TIn,
typename TOut>
60 TAmounts<TIn, TOut>
const& balances)
const
62 TAmounts<TIn, TOut> cur{};
64 cur.in = toAmount<TIn>(
66 InitialFibSeqPct * initialBalances_.in,
68 cur.out =
swapAssetIn(initialBalances_, cur.in, tradingFee_);
70 if (ammContext_.curIters() == 0)
75 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
76 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393,
77 196418, 317811, 514229, 832040, 1346269};
81 !ammContext_.maxItersReached(),
82 "ripple::AMMLiquidity::generateFibSeqOffer : maximum iterations");
84 cur.out = toAmount<TOut>(
86 cur.out * fib[ammContext_.curIters() - 1],
89 if (cur.out >= balances.out)
90 Throw<std::overflow_error>(
91 "AMMLiquidity: generateFibSeqOffer exceeds the balance");
113maxOut(T
const&
out, Issue
const& iss)
115 Number
const res =
out * Number{99, -2};
120template <
typename TIn,
typename TOut>
123 TAmounts<TIn, TOut>
const& balances,
124 Rules const& rules)
const
126 if (!rules.
enabled(fixAMMOverflowOffer))
131 swapAssetIn(balances, maxAmount<TIn>(), tradingFee_)},
137 auto const out = maxOut<TOut>(balances.out, issueOut());
138 if (
out <= TOut{0} ||
out >= balances.out)
148template <
typename TIn,
typename TOut>
155 if (ammContext_.maxItersReached())
158 auto const balances = fetchBalances(view);
161 if (balances.in == beast::zero || balances.out == beast::zero)
163 JLOG(j_.
debug()) <<
"AMMLiquidity::getOffer, frozen accounts";
167 JLOG(j_.
trace()) <<
"AMMLiquidity::getOffer balances "
169 <<
to_string(initialBalances_.out) <<
" new balances "
181 if (
auto const spotPriceQ = Quality{balances}; clobQuality &&
182 (spotPriceQ <= clobQuality ||
185 JLOG(j_.
trace()) <<
"AMMLiquidity::getOffer, higher clob quality";
192 if (ammContext_.multiPath())
194 auto const amounts = generateFibSeqOffer(balances);
195 if (clobQuality && Quality{amounts} < clobQuality)
198 *
this, amounts, balances, Quality{amounts});
200 else if (!clobQuality)
207 return maxOffer(balances, view.
rules());
211 balances, *clobQuality, tradingFee_, view.
rules(), j_))
214 *
this, *amounts, balances, Quality{*amounts});
218 if (
auto const maxAMMOffer = maxOffer(balances, view.
rules());
220 Quality{maxAMMOffer->amount()} > *clobQuality)
226 JLOG(j_.
error()) <<
"AMMLiquidity::getOffer overflow " << e.
what();
228 return maxOffer(balances, view.
rules());
234 JLOG(j_.
error()) <<
"AMMLiquidity::getOffer exception " << e.
what();
241 if (offer->amount().in > beast::zero &&
242 offer->amount().out > beast::zero)
245 <<
"AMMLiquidity::getOffer, created "
246 <<
to_string(offer->amount().in) <<
"/" << issueIn_ <<
" "
247 <<
to_string(offer->amount().out) <<
"/" << issueOut_;
251 JLOG(j_.
debug()) <<
"AMMLiquidity::getOffer, no valid offer "
252 << ammContext_.multiPath() <<
" "
253 << ammContext_.curIters() <<
" "
254 << (clobQuality ? clobQuality->rate() :
STAmount{})
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Maintains AMM info per overall payment engine execution and individual iteration.
static constexpr std::uint8_t MaxIterations
AMMLiquidity class provides AMM offers to BookStep class.
std::optional< AMMOffer< TIn, TOut > > getOffer(ReadView const &view, std::optional< Quality > const &clobQuality) const
Generate AMM offer.
TAmounts< TIn, TOut > fetchBalances(ReadView const &view) const
Fetches current AMM balances.
AMMLiquidity(ReadView const &view, AccountID const &ammAccountID, std::uint32_t tradingFee, Issue const &in, Issue const &out, AMMContext &ammContext, beast::Journal j)
std::optional< AMMOffer< TIn, TOut > > maxOffer(TAmounts< TIn, TOut > const &balances, Rules const &rules) const
Generate max offer.
TAmounts< TIn, TOut > generateFibSeqOffer(TAmounts< TIn, TOut > const &balances) const
Generate AMM offers with the offer size based on Fibonacci sequence.
Represents synthetic AMM offer in BookStep.
Floating point representation of amounts with high dynamic range.
A currency issued by an account.
virtual Rules const & rules() const =0
Returns the tx processing rules.
Rules controlling protocol behavior.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
static int const cMaxOffset
static std::uint64_t const cMaxValue
static std::uint64_t const cMaxNative
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Issue getIssue(T const &amt)
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::optional< TAmounts< TIn, TOut > > changeSpotPriceQuality(TAmounts< TIn, TOut > const &pool, Quality const &quality, std::uint16_t tfee, Rules const &rules, beast::Journal j)
Generate AMM offer so that either updated Spot Price Quality (SPQ) is equal to LOB quality (in this c...
std::string to_string(base_uint< Bits, Tag > const &a)
STAmount ammAccountHolds(ReadView const &view, AccountID const &ammAccountID, Issue const &issue)
Returns total amount held by AMM for the given token.
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.