1#include <xrpl/ledger/Sandbox.h>
2#include <xrpl/protocol/AMMCore.h>
3#include <xrpl/protocol/Feature.h>
4#include <xrpl/protocol/TxFlags.h>
5#include <xrpl/tx/transactors/dex/AMMUtils.h>
6#include <xrpl/tx/transactors/dex/AMMVote.h>
22 JLOG(ctx.
j.
debug()) <<
"AMM Vote: invalid asset pair.";
28 JLOG(ctx.
j.
debug()) <<
"AMM Vote: invalid trading fee.";
41 JLOG(ctx.
j.
debug()) <<
"AMM Vote: Invalid asset pair.";
44 if (ammSle->getFieldAmount(sfLPTokenBalance) == beast::zero)
48 if (
auto const lpTokensNew =
ammLPHolds(ctx.
view, *ammSle, ctx.
tx[sfAccount], ctx.
j);
49 lpTokensNew == beast::zero)
51 JLOG(ctx.
j.
debug()) <<
"AMM Vote: account is not LP.";
61 auto const feeNew = ctx_.
tx[sfTradingFee];
65 STAmount const lptAMMBalance = (*ammSle)[sfLPTokenBalance];
75 bool foundAccount =
false;
81 for (
auto const& entry : ammSle->getFieldArray(sfVoteSlots))
83 auto const account = entry[sfAccount];
85 if (lpTokens == beast::zero)
87 JLOG(j_.
debug()) <<
"AMMVote::applyVote, account " << account <<
" is not LP";
90 auto feeVal = entry[sfTradingFee];
93 if (account == account_)
95 lpTokens = lpTokensNew;
100 num += feeVal * lpTokens;
112 (lpTokens < *minTokens ||
113 (lpTokens == *minTokens &&
114 (feeVal < minFee || (feeVal == minFee && account < minAccount)))))
116 minTokens = lpTokens;
117 minPos = updatedVoteSlots.
size();
118 minAccount = account;
121 updatedVoteSlots.
push_back(std::move(newEntry));
136 num += feeNew * lpTokensNew;
140 *(updatedVoteSlots.
begin() + *minPos) = std::move(newEntry);
144 updatedVoteSlots.
push_back(std::move(newEntry));
155 else if (lpTokensNew > *minTokens || (lpTokensNew == *minTokens && feeNew > minFee))
157 auto const entry = updatedVoteSlots.
begin() + minPos;
159 num -=
Number((*entry)[~sfTradingFee].value_or(0)) * *minTokens;
167 JLOG(j_.
debug()) <<
"AMMVote::applyVote, insufficient tokens to "
168 "override other votes";
173 !ctx_.
view().
rules().
enabled(fixInnerObjTemplate) || ammSle->isFieldPresent(sfAuctionSlot),
174 "xrpl::applyVote : has auction slot");
177 ammSle->setFieldArray(sfVoteSlots, updatedVoteSlots);
178 if (
auto const fee =
static_cast<std::int64_t>(num / den))
180 ammSle->setFieldU16(sfTradingFee, fee);
181 if (ammSle->isFieldPresent(sfAuctionSlot))
183 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
186 auctionSlot.setFieldU16(sfDiscountedFee, discountedFee);
188 else if (auctionSlot.isFieldPresent(sfDiscountedFee))
190 auctionSlot.makeFieldAbsent(sfDiscountedFee);
196 if (ammSle->isFieldPresent(sfTradingFee))
197 ammSle->makeFieldAbsent(sfTradingFee);
198 if (ammSle->isFieldPresent(sfAuctionSlot))
200 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
201 if (auctionSlot.isFieldPresent(sfDiscountedFee))
202 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.
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.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
void push_back(STObject const &object)
void setFieldU32(SField const &field, std::uint32_t)
void setFieldU16(SField const &field, std::uint16_t)
static STObject makeInnerObject(SField const &name)
void setAccountID(SField const &field, AccountID 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.
std::uint16_t constexpr TRADING_FEE_THRESHOLD
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.
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::uint32_t constexpr AUCTION_SLOT_DISCOUNTED_FEE_FRACTION
TERSubset< CanCvtToTER > TER
std::uint16_t constexpr VOTE_MAX_SLOTS
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.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.