20#include <xrpld/app/misc/AMMUtils.h>
21#include <xrpld/app/tx/detail/AMMVote.h>
23#include <xrpl/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 trading fee.";
58 if (
auto const ammSle =
62 JLOG(ctx.
j.
debug()) <<
"AMM Vote: Invalid asset pair.";
65 else if (ammSle->getFieldAmount(sfLPTokenBalance) == beast::zero)
67 else if (
auto const lpTokensNew =
69 lpTokensNew == beast::zero)
71 JLOG(ctx.
j.
debug()) <<
"AMM Vote: account is not LP.";
85 auto const feeNew = ctx_.
tx[sfTradingFee];
89 STAmount const lptAMMBalance = (*ammSle)[sfLPTokenBalance];
99 bool foundAccount =
false;
105 for (
auto const& entry : ammSle->getFieldArray(sfVoteSlots))
107 auto const account = entry[sfAccount];
109 if (lpTokens == beast::zero)
112 <<
"AMMVote::applyVote, account " << account <<
" is not LP";
115 auto feeVal = entry[sfTradingFee];
118 if (account == account_)
120 lpTokens = lpTokensNew;
125 num += feeVal * lpTokens;
138 (lpTokens < *minTokens ||
139 (lpTokens == *minTokens &&
140 (feeVal < minFee || (feeVal == minFee && account < minAccount)))))
142 minTokens = lpTokens;
143 minPos = updatedVoteSlots.
size();
144 minAccount = account;
147 updatedVoteSlots.
push_back(std::move(newEntry));
164 num += feeNew * lpTokensNew;
167 *(updatedVoteSlots.
begin() + *minPos) = std::move(newEntry);
169 updatedVoteSlots.
push_back(std::move(newEntry));
178 lpTokensNew > *minTokens ||
179 (lpTokensNew == *minTokens && feeNew > minFee))
181 auto const entry = updatedVoteSlots.
begin() + minPos;
183 num -=
Number((*entry)[~sfTradingFee].value_or(0)) * *minTokens;
191 JLOG(j_.
debug()) <<
"AMMVote::applyVote, insufficient tokens to "
192 "override other votes";
198 ammSle->isFieldPresent(sfAuctionSlot),
199 "ripple::applyVote : has auction slot");
202 ammSle->setFieldArray(sfVoteSlots, updatedVoteSlots);
203 if (
auto const fee =
static_cast<std::int64_t>(num / den))
205 ammSle->setFieldU16(sfTradingFee, fee);
206 if (ammSle->isFieldPresent(sfAuctionSlot))
208 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
209 if (
auto const discountedFee =
211 auctionSlot.setFieldU16(sfDiscountedFee, discountedFee);
212 else if (auctionSlot.isFieldPresent(sfDiscountedFee))
213 auctionSlot.makeFieldAbsent(sfDiscountedFee);
218 if (ammSle->isFieldPresent(sfTradingFee))
219 ammSle->makeFieldAbsent(sfTradingFee);
220 if (ammSle->isFieldPresent(sfAuctionSlot))
222 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
223 if (auctionSlot.isFieldPresent(sfDiscountedFee))
224 auctionSlot.makeFieldAbsent(sfDiscountedFee);
A generic endpoint for log messages.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static bool checkExtraFeatures(PreflightContext 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)
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.
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.
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)
std::uint16_t constexpr VOTE_MAX_SLOTS
TERSubset< CanCvtToTER > TER
std::uint16_t constexpr TRADING_FEE_THRESHOLD
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.