20#include <xrpld/app/misc/AMMUtils.h>
21#include <xrpld/app/tx/detail/AMMVote.h>
22#include <xrpld/ledger/Sandbox.h>
24#include <xrpl/protocol/AMMCore.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/TxFlags.h>
42 JLOG(ctx.
j.
debug()) <<
"AMM Vote: invalid asset pair.";
48 JLOG(ctx.
j.
debug()) <<
"AMM Vote: invalid flags.";
54 JLOG(ctx.
j.
debug()) <<
"AMM Vote: invalid trading fee.";
64 if (
auto const ammSle =
68 JLOG(ctx.
j.
debug()) <<
"AMM Vote: Invalid asset pair.";
71 else if (ammSle->getFieldAmount(sfLPTokenBalance) == beast::zero)
73 else if (
auto const lpTokensNew =
75 lpTokensNew == beast::zero)
77 JLOG(ctx.
j.
debug()) <<
"AMM Vote: account is not LP.";
91 auto const feeNew = ctx_.
tx[sfTradingFee];
95 STAmount const lptAMMBalance = (*ammSle)[sfLPTokenBalance];
105 bool foundAccount =
false;
111 for (
auto const& entry : ammSle->getFieldArray(sfVoteSlots))
113 auto const account = entry[sfAccount];
115 if (lpTokens == beast::zero)
118 <<
"AMMVote::applyVote, account " << account <<
" is not LP";
121 auto feeVal = entry[sfTradingFee];
124 if (account == account_)
126 lpTokens = lpTokensNew;
131 num += feeVal * lpTokens;
144 (lpTokens < *minTokens ||
145 (lpTokens == *minTokens &&
146 (feeVal < minFee || (feeVal == minFee && account < minAccount)))))
148 minTokens = lpTokens;
149 minPos = updatedVoteSlots.
size();
150 minAccount = account;
153 updatedVoteSlots.
push_back(std::move(newEntry));
170 num += feeNew * lpTokensNew;
173 *(updatedVoteSlots.
begin() + *minPos) = std::move(newEntry);
175 updatedVoteSlots.
push_back(std::move(newEntry));
184 lpTokensNew > *minTokens ||
185 (lpTokensNew == *minTokens && feeNew > minFee))
187 auto const entry = updatedVoteSlots.
begin() + minPos;
189 num -=
Number((*entry)[~sfTradingFee].value_or(0)) * *minTokens;
197 JLOG(j_.
debug()) <<
"AMMVote::applyVote, insufficient tokens to "
198 "override other votes";
204 ammSle->isFieldPresent(sfAuctionSlot),
205 "ripple::applyVote : has auction slot");
208 ammSle->setFieldArray(sfVoteSlots, updatedVoteSlots);
209 if (
auto const fee =
static_cast<std::int64_t>(num / den))
211 ammSle->setFieldU16(sfTradingFee, fee);
212 if (ammSle->isFieldPresent(sfAuctionSlot))
214 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
215 if (
auto const discountedFee =
217 auctionSlot.setFieldU16(sfDiscountedFee, discountedFee);
218 else if (auctionSlot.isFieldPresent(sfDiscountedFee))
219 auctionSlot.makeFieldAbsent(sfDiscountedFee);
224 if (ammSle->isFieldPresent(sfTradingFee))
225 ammSle->makeFieldAbsent(sfTradingFee);
226 if (ammSle->isFieldPresent(sfAuctionSlot))
228 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
229 if (auctionSlot.isFieldPresent(sfDiscountedFee))
230 auctionSlot.makeFieldAbsent(sfDiscountedFee);
A generic endpoint for log messages.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
State information when applying a tx.
beast::Journal const journal
A currency issued by an account.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
void push_back(STObject const &object)
void setFieldU16(SField const &field, std::uint16_t)
static STObject makeInnerObject(SField const &name)
void setAccountID(SField const &field, AccountID const &)
void setFieldU32(SField const &field, std::uint32_t)
std::uint32_t getFlags() const
Discardable, editable view to a ledger.
void update(std::shared_ptr< SLE > const &sle) override
Indicate changes to a peeked SLE.
std::shared_ptr< SLE > peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static std::pair< TER, bool > applyVote(ApplyContext &ctx_, Sandbox &sb, AccountID const &account_, beast::Journal j_)
bool ammEnabled(Rules const &)
Return true if required AMM amendments are enabled.
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
STAmount ammLPHolds(ReadView const &view, Currency const &cur1, Currency const &cur2, AccountID const &ammAccount, AccountID const &lpAccount, beast::Journal const j)
Get the balance of LP tokens.
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
std::uint32_t constexpr VOTE_WEIGHT_SCALE_FACTOR
NotTEC invalidAMMAssetPair(Issue const &issue1, Issue const &issue2, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt)
bool isTesSuccess(TER x) noexcept
constexpr std::uint32_t tfUniversalMask
std::uint16_t constexpr VOTE_MAX_SLOTS
TERSubset< CanCvtToTER > TER
std::uint16_t constexpr TRADING_FEE_THRESHOLD
TERSubset< CanCvtToNotTEC > NotTEC
std::uint32_t constexpr AUCTION_SLOT_DISCOUNTED_FEE_FRACTION
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.