1#include <xrpl/tx/invariants/AMMInvariant.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/protocol/TxFormats.h>
6#include <xrpl/tx/transactors/dex/AMMHelpers.h>
7#include <xrpl/tx/transactors/dex/AMMUtils.h>
22 auto const type =
after->getType();
31 (type == ltRIPPLE_STATE && ((
after->getFlags() & lsfAMMNode) != 0u)) ||
32 (type == ltACCOUNT_ROOT &&
after->isFieldPresent(sfAMMID)))
41 if (before->getType() == ltAMM)
56 amount > beast::zero && amount2 > beast::zero && lptAMMBalance > beast::zero;
60 (amount == beast::zero && amount2 == beast::zero && lptAMMBalance == beast::zero);
90 JLOG(j.
error()) <<
"AMMBid invariant failed: pool changed";
121 JLOG(j.
error()) <<
"AMMCreate invariant failed: AMM object is not created";
131 tx[sfAmount].get<Issue>(),
132 tx[sfAmount2].get<Issue>(),
141 JLOG(j.
error()) <<
"AMMCreate invariant failed: " << amount <<
" " << amount2 <<
" "
158 :
"AMM object is changed on tecINCOMPLETE";
159 JLOG(j.
error()) <<
"AMMDelete invariant failed: " << msg;
174 JLOG(j.
error()) <<
"AMM swap invariant failed: AMM object changed";
193 tx[sfAsset].get<Issue>(),
194 tx[sfAsset2].get<Issue>(),
201 auto const poolProductMean =
root2(amount * amount2);
202 bool const nonNegativeBalances =
206 auto weakInvariantCheck = [&]() {
210 if (!nonNegativeBalances || (!strongInvariantCheck && !weakInvariantCheck()))
235 JLOG(j.
error()) <<
"AMMDeposit invariant failed: AMM object is deleted";
A generic endpoint for log messages.
Number is a floating point type that can represent a wide range of values.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
uint256 getHash(HashPrefix prefix) const
TxType getTxnType() const
std::optional< AccountID > ammAccount_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
bool finalizeWithdraw(STTx const &, ReadView const &, bool enforce, beast::Journal const &) const
bool finalizeBid(bool enforce, beast::Journal const &) const
bool finalizeCreate(STTx const &, ReadView const &, bool enforce, beast::Journal const &) const
bool finalizeDEX(bool enforce, beast::Journal const &) const
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalizeDeposit(STTx const &, ReadView const &, bool enforce, beast::Journal const &) const
bool generalInvariant(STTx const &, ReadView const &, ZeroAllowed zeroAllowed, beast::Journal const &) const
std::optional< STAmount > lptAMMBalanceAfter_
bool finalizeVote(bool enforce, beast::Journal const &) const
bool finalizeDelete(bool enforce, TER res, beast::Journal const &) const
std::optional< STAmount > lptAMMBalanceBefore_
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
static bool validBalances(STAmount const &amount, STAmount const &amount2, STAmount const &lptAMMBalance, ValidAMM::ZeroAllowed zeroAllowed)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
std::pair< STAmount, STAmount > ammPoolHolds(ReadView const &view, AccountID const &ammAccountID, Issue const &issue1, Issue const &issue2, FreezeHandling freezeHandling, beast::Journal const j)
Get AMM pool balances.
@ transactionID
transaction plus signature to give transaction ID
bool isTesSuccess(TER x) noexcept
bool withinRelativeDistance(Quality const &calcQuality, Quality const &reqQuality, Number const &dist)
Check if the relative distance between the qualities is within the requested distance.