1#include <xrpl/tx/paths/AMMLiquidity.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/beast/utility/Zero.h>
8#include <xrpl/beast/utility/instrumentation.h>
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/ledger/helpers/AMMHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Asset.h>
13#include <xrpl/protocol/Feature.h>
14#include <xrpl/protocol/IOUAmount.h>
15#include <xrpl/protocol/MPTAmount.h>
16#include <xrpl/protocol/Protocol.h>
17#include <xrpl/protocol/Quality.h>
18#include <xrpl/protocol/Rules.h>
19#include <xrpl/protocol/STAmount.h>
20#include <xrpl/protocol/XRPAmount.h>
21#include <xrpl/tx/paths/AMMOffer.h>
22#include <xrpl/tx/transactors/dex/AMMContext.h>
31template <
typename TIn,
typename TOut>
50template <
typename TIn,
typename TOut>
57 if (amountIn < beast::kZero || amountOut < beast::kZero)
63template <
typename TIn,
typename TOut>
80 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
81 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393,
82 196418, 317811, 514229, 832040, 1346269};
87 "xrpl::AMMLiquidity::generateFibSeqOffer : maximum iterations");
94 if (cur.
out >= balances.
out)
127maxOut(T
const& out,
Asset const& asset)
134template <
typename TIn,
typename TOut>
135std::optional<AMMOffer<TIn, TOut>>
138 if (!rules.
enabled(fixAMMOverflowOffer))
147 auto const out = maxOut<TOut>(balances.
out,
assetOut());
148 if (out <= TOut{0} || out >= balances.
out)
154template <
typename TIn,
typename TOut>
166 if (balances.in == beast::kZero || balances.out == beast::kZero)
168 JLOG(
j_.debug()) <<
"AMMLiquidity::getOffer, frozen accounts";
184 if (
auto const spotPriceQ =
Quality{balances}; clobQuality &&
185 (spotPriceQ <= clobQuality ||
188 JLOG(
j_.trace()) <<
"AMMLiquidity::getOffer, higher clob quality";
198 if (clobQuality &&
Quality{amounts} < clobQuality)
211 if (
auto const amounts =
218 if (
auto const maxAMMOffer =
maxOffer(balances, view.
rules());
219 maxAMMOffer &&
Quality{maxAMMOffer->amount()} > *clobQuality)
225 JLOG(
j_.error()) <<
"AMMLiquidity::getOffer overflow " << e.
what();
235 JLOG(
j_.error()) <<
"AMMLiquidity::getOffer exception " << e.
what();
242 if (offer->amount().in > beast::kZero && offer->amount().out > beast::kZero)
244 JLOG(
j_.trace()) <<
"AMMLiquidity::getOffer, created " <<
to_string(offer->amount().in)
250 JLOG(
j_.debug()) <<
"AMMLiquidity::getOffer, no valid offer " <<
ammContext_.multiPath()
252 << (clobQuality ? clobQuality->rate() :
STAmount{}) <<
" "
A generic endpoint for log messages.
Maintains AMM info per overall payment engine execution and individual iteration.
static constexpr std::uint8_t kMaxIterations
AMMLiquidity class provides AMM offers to BookStep class.
TAmounts< TIn, TOut > generateFibSeqOffer(TAmounts< TIn, TOut > const &balances) const
Generate AMM offers with the offer size based on Fibonacci sequence.
Asset const & assetOut() const
TAmounts< TIn, TOut > const initialBalances_
AccountID const ammAccountID_
std::optional< AMMOffer< TIn, TOut > > maxOffer(TAmounts< TIn, TOut > const &balances, Rules const &rules) const
Generate max offer.
TAmounts< TIn, TOut > fetchBalances(ReadView const &view) const
Fetches current AMM balances.
std::optional< AMMOffer< TIn, TOut > > getOffer(ReadView const &view, std::optional< Quality > const &clobQuality) const
Generate AMM offer.
AMMLiquidity(ReadView const &view, AccountID const &ammAccountID, std::uint32_t tradingFee, Asset const &in, Asset const &out, AMMContext &ammContext, beast::Journal j)
static Number const kInitialFibSeqPct
std::uint32_t const tradingFee_
std::uint32_t tradingFee() const
Represents synthetic AMM offer in BookStep.
Number is a floating point type that can represent a wide range of values.
Represents the logical ratio of output currency to input currency.
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 constexpr std::uint64_t kMaxValue
static constexpr std::uint64_t kMaxNative
static constexpr int kMaxOffset
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
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(BaseUInt< Bits, Tag > const &a)
T toAmount(STAmount const &amt)=delete
STAmount ammAccountHolds(ReadView const &view, AccountID const &ammAccountID, Asset const &asset)
Returns total amount held by AMM for the given token.
Asset getAsset(T const &amt)
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...
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
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.
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Represents a pair of input and output currencies.