1#include <xrpld/app/misc/AMMUtils.h>
2#include <xrpld/app/tx/detail/AMMVote.h>
4#include <xrpl/ledger/Sandbox.h>
5#include <xrpl/protocol/AMMCore.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/TxFlags.h>
23 JLOG(ctx.
j.
debug()) <<
"AMM Vote: invalid asset pair.";
29 JLOG(ctx.
j.
debug()) <<
"AMM Vote: invalid trading fee.";
39 if (
auto const ammSle =
43 JLOG(ctx.
j.
debug()) <<
"AMM Vote: Invalid asset pair.";
46 else if (ammSle->getFieldAmount(sfLPTokenBalance) == beast::zero)
48 else if (
auto const lpTokensNew =
50 lpTokensNew == beast::zero)
52 JLOG(ctx.
j.
debug()) <<
"AMM Vote: account is not LP.";
66 auto const feeNew = ctx_.
tx[sfTradingFee];
70 STAmount const lptAMMBalance = (*ammSle)[sfLPTokenBalance];
80 bool foundAccount =
false;
86 for (
auto const& entry : ammSle->getFieldArray(sfVoteSlots))
88 auto const account = entry[sfAccount];
90 if (lpTokens == beast::zero)
93 <<
"AMMVote::applyVote, account " << account <<
" is not LP";
96 auto feeVal = entry[sfTradingFee];
99 if (account == account_)
101 lpTokens = lpTokensNew;
106 num += feeVal * lpTokens;
119 (lpTokens < *minTokens ||
120 (lpTokens == *minTokens &&
121 (feeVal < minFee || (feeVal == minFee && account < minAccount)))))
123 minTokens = lpTokens;
124 minPos = updatedVoteSlots.
size();
125 minAccount = account;
128 updatedVoteSlots.
push_back(std::move(newEntry));
145 num += feeNew * lpTokensNew;
148 *(updatedVoteSlots.
begin() + *minPos) = std::move(newEntry);
150 updatedVoteSlots.
push_back(std::move(newEntry));
159 lpTokensNew > *minTokens ||
160 (lpTokensNew == *minTokens && feeNew > minFee))
162 auto const entry = updatedVoteSlots.
begin() + minPos;
164 num -=
Number((*entry)[~sfTradingFee].value_or(0)) * *minTokens;
172 JLOG(j_.
debug()) <<
"AMMVote::applyVote, insufficient tokens to "
173 "override other votes";
179 ammSle->isFieldPresent(sfAuctionSlot),
180 "ripple::applyVote : has auction slot");
183 ammSle->setFieldArray(sfVoteSlots, updatedVoteSlots);
184 if (
auto const fee =
static_cast<std::int64_t>(num / den))
186 ammSle->setFieldU16(sfTradingFee, fee);
187 if (ammSle->isFieldPresent(sfAuctionSlot))
189 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
190 if (
auto const discountedFee =
192 auctionSlot.setFieldU16(sfDiscountedFee, discountedFee);
193 else if (auctionSlot.isFieldPresent(sfDiscountedFee))
194 auctionSlot.makeFieldAbsent(sfDiscountedFee);
199 if (ammSle->isFieldPresent(sfTradingFee))
200 ammSle->makeFieldAbsent(sfTradingFee);
201 if (ammSle->isFieldPresent(sfAuctionSlot))
203 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
204 if (auctionSlot.isFieldPresent(sfDiscountedFee))
205 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.