20#include <xrpld/app/misc/AMMHelpers.h>
21#include <xrpld/app/misc/AMMUtils.h>
22#include <xrpld/app/tx/detail/AMMBid.h>
23#include <xrpld/ledger/Sandbox.h>
24#include <xrpld/ledger/View.h>
26#include <xrpl/protocol/AMMCore.h>
27#include <xrpl/protocol/Feature.h>
28#include <xrpl/protocol/TER.h>
29#include <xrpl/protocol/TxFlags.h>
44 JLOG(ctx.
j.
debug()) <<
"AMM Bid: invalid flags.";
51 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid asset pair.";
55 if (
auto const bidMin = ctx.
tx[~sfBidMin])
59 JLOG(ctx.
j.
debug()) <<
"AMM Bid: invalid min slot price.";
64 if (
auto const bidMax = ctx.
tx[~sfBidMax])
68 JLOG(ctx.
j.
debug()) <<
"AMM Bid: invalid max slot price.";
78 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid number of AuthAccounts.";
93 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid asset pair.";
97 auto const lpTokensBalance = (*ammSle)[sfLPTokenBalance];
98 if (lpTokensBalance == beast::zero)
107 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid Account.";
113 auto const lpTokens =
116 if (lpTokens == beast::zero)
118 JLOG(ctx.
j.
debug()) <<
"AMM Bid: account is not LP.";
122 auto const bidMin = ctx.
tx[~sfBidMin];
126 if (bidMin->issue() != lpTokens.issue())
128 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid LPToken.";
131 if (*bidMin > lpTokens || *bidMin >= lpTokensBalance)
133 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid Tokens.";
138 auto const bidMax = ctx.
tx[~sfBidMax];
141 if (bidMax->issue() != lpTokens.issue())
143 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid LPToken.";
146 if (*bidMax > lpTokens || *bidMax >= lpTokensBalance)
148 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid Tokens.";
153 if (bidMin && bidMax && bidMin > bidMax)
155 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid Max/MinSlotPrice.";
174 STAmount const lptAMMBalance = (*ammSle)[sfLPTokenBalance];
177 if (!rules.enabled(fixInnerObjTemplate))
179 if (!ammSle->isFieldPresent(sfAuctionSlot))
180 ammSle->makeFieldPresent(sfAuctionSlot);
185 ammSle->isFieldPresent(sfAuctionSlot),
186 "ripple::applyBid : has auction slot");
187 if (!ammSle->isFieldPresent(sfAuctionSlot))
190 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
192 duration_cast<seconds>(
196 auto const discountedFee =
198 auto const tradingFee =
getFee((*ammSle)[sfTradingFee]);
200 auto const minSlotPrice =
210 auto validOwner = [&](
AccountID const& account) {
213 return timeSlot && *timeSlot < tailingSlot &&
220 auctionSlot.setAccountID(sfAccount, account_);
223 auctionSlot.setFieldU16(sfDiscountedFee, fee);
224 else if (auctionSlot.isFieldPresent(sfDiscountedFee))
225 auctionSlot.makeFieldAbsent(sfDiscountedFee);
226 auctionSlot.setFieldAmount(
227 sfPrice,
toSTAmount(lpTokens.issue(), minPrice));
229 auctionSlot.setFieldArray(
232 auctionSlot.makeFieldAbsent(sfAuthAccounts);
236 if (saBurn >= lptAMMBalance)
240 <<
"AMM Bid: LP Token burn exceeds AMM balance " << burn <<
" "
248 JLOG(ctx_.
journal.
debug()) <<
"AMM Bid: failed to redeem.";
251 ammSle->setFieldAmount(sfLPTokenBalance, lptAMMBalance - saBurn);
258 auto const bidMin = ctx_.
tx[~sfBidMin];
259 auto const bidMax = ctx_.
tx[~sfBidMax];
265 if (bidMin && bidMax)
267 if (computedPrice <= *bidMax)
270 <<
"AMM Bid: not in range " << computedPrice <<
" "
271 << *bidMin <<
" " << *bidMax;
281 if (computedPrice <= *bidMax)
282 return computedPrice;
284 << computedPrice <<
" " << *bidMax;
288 return computedPrice;
292 else if (payPrice > lpTokens)
298 if (
auto const acct = auctionSlot[~sfAccount]; !acct || !validOwner(*acct))
300 if (
auto const payPrice = getPayPrice(minSlotPrice); !payPrice)
301 return {payPrice.error(),
false};
303 res = updateSlot(discountedFee, *payPrice, *payPrice);
308 STAmount const pricePurchased = auctionSlot[sfPrice];
309 XRPL_ASSERT(timeSlot,
"ripple::applyBid : timeSlot is set");
310 auto const fractionUsed =
312 auto const fractionRemaining =
Number(1) - fractionUsed;
313 auto const computedPrice = [&]() ->
Number {
314 auto const p1_05 =
Number(105, -2);
317 return pricePurchased * p1_05 + minSlotPrice;
319 return pricePurchased * p1_05 * (1 -
power(fractionUsed, 60)) +
323 auto const payPrice = getPayPrice(computedPrice);
326 return {payPrice.error(),
false};
330 auto const refund = fractionRemaining * pricePurchased;
331 if (refund > *payPrice)
334 JLOG(ctx_.
journal.
fatal()) <<
"AMM Bid: refund exceeds payPrice "
335 << refund <<
" " << *payPrice;
341 auctionSlot[sfAccount],
346 JLOG(ctx_.
journal.
debug()) <<
"AMM Bid: failed to refund.";
350 auto const burn = *payPrice - refund;
351 res = updateSlot(discountedFee, *payPrice, burn);
A generic endpoint for log messages.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(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 LedgerInfo const & info() const =0
Returns information about the ledger.
virtual Rules const & rules() const =0
Returns the tx processing rules.
Issue const & issue() const
const STArray & getFieldArray(SField const &field) const
bool isFieldPresent(SField const &field) const
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 const > read(Keylet const &k) const override
Return the state item associated with a key.
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.
Keylet account(AccountID const &id) noexcept
AccountID root.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
NotTEC invalidAMMAmount(STAmount const &amount, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt, bool validZero=false)
Validate the amount.
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, bool isDeposit)
Adjust LP tokens to deposit/withdraw.
std::uint32_t constexpr TOTAL_TIME_SLOT_SECS
std::uint16_t constexpr AUCTION_SLOT_TIME_INTERVALS
std::optional< std::uint8_t > ammAuctionTimeSlot(std::uint64_t current, STObject const &auctionSlot)
Get time slot of the auction slot.
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
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.
@ current
This was a new validation and was added.
std::uint32_t constexpr AUCTION_SLOT_MIN_FEE_FRACTION
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.
Number getFee(std::uint16_t tfee)
Convert to the fee from the basis points.
Number power(Number const &f, unsigned n)
NotTEC invalidAMMAssetPair(Issue const &issue1, Issue const &issue2, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt)
constexpr std::uint32_t tfUniversalMask
static std::pair< TER, bool > applyBid(ApplyContext &ctx_, Sandbox &sb, AccountID const &account_, beast::Journal j_)
TERSubset< CanCvtToTER > TER
std::uint16_t constexpr AUCTION_SLOT_MAX_AUTH_ACCOUNTS
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
Calls static accountSendIOU if saAmount represents Issue.
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.
T time_since_epoch(T... args)