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>
22 JLOG(ctx.
j.
debug()) <<
"AMM Vote: invalid asset pair.";
28 JLOG(ctx.
j.
debug()) <<
"AMM Vote: invalid trading fee.";
40 JLOG(ctx.
j.
debug()) <<
"AMM Vote: Invalid asset pair.";
43 else if (ammSle->getFieldAmount(sfLPTokenBalance) == beast::zero)
45 else if (
auto const lpTokensNew =
ammLPHolds(ctx.
view, *ammSle, ctx.
tx[sfAccount], ctx.
j);
46 lpTokensNew == beast::zero)
48 JLOG(ctx.
j.
debug()) <<
"AMM Vote: account is not LP.";
58 auto const feeNew = ctx_.
tx[sfTradingFee];
62 STAmount const lptAMMBalance = (*ammSle)[sfLPTokenBalance];
72 bool foundAccount =
false;
78 for (
auto const& entry : ammSle->getFieldArray(sfVoteSlots))
80 auto const account = entry[sfAccount];
82 if (lpTokens == beast::zero)
84 JLOG(j_.
debug()) <<
"AMMVote::applyVote, account " << account <<
" is not LP";
87 auto feeVal = entry[sfTradingFee];
90 if (account == account_)
92 lpTokens = lpTokensNew;
97 num += feeVal * lpTokens;
108 (lpTokens < *minTokens ||
109 (lpTokens == *minTokens && (feeVal < minFee || (feeVal == minFee && account < minAccount)))))
111 minTokens = lpTokens;
112 minPos = updatedVoteSlots.
size();
113 minAccount = account;
116 updatedVoteSlots.
push_back(std::move(newEntry));
130 num += feeNew * lpTokensNew;
133 *(updatedVoteSlots.
begin() + *minPos) = std::move(newEntry);
135 updatedVoteSlots.
push_back(std::move(newEntry));
143 else if (lpTokensNew > *minTokens || (lpTokensNew == *minTokens && feeNew > minFee))
145 auto const entry = updatedVoteSlots.
begin() + minPos;
147 num -=
Number((*entry)[~sfTradingFee].value_or(0)) * *minTokens;
155 JLOG(j_.
debug()) <<
"AMMVote::applyVote, insufficient tokens to "
156 "override other votes";
161 !ctx_.
view().
rules().
enabled(fixInnerObjTemplate) || ammSle->isFieldPresent(sfAuctionSlot),
162 "xrpl::applyVote : has auction slot");
165 ammSle->setFieldArray(sfVoteSlots, updatedVoteSlots);
166 if (
auto const fee =
static_cast<std::int64_t>(num / den))
168 ammSle->setFieldU16(sfTradingFee, fee);
169 if (ammSle->isFieldPresent(sfAuctionSlot))
171 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
173 auctionSlot.setFieldU16(sfDiscountedFee, discountedFee);
174 else if (auctionSlot.isFieldPresent(sfDiscountedFee))
175 auctionSlot.makeFieldAbsent(sfDiscountedFee);
180 if (ammSle->isFieldPresent(sfTradingFee))
181 ammSle->makeFieldAbsent(sfTradingFee);
182 if (ammSle->isFieldPresent(sfAuctionSlot))
184 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
185 if (auctionSlot.isFieldPresent(sfDiscountedFee))
186 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.