1#include <xrpld/app/misc/AMMHelpers.h>
2#include <xrpld/app/misc/AMMUtils.h>
3#include <xrpld/app/tx/detail/AMMBid.h>
5#include <xrpl/ledger/Sandbox.h>
6#include <xrpl/ledger/View.h>
7#include <xrpl/protocol/AMMCore.h>
8#include <xrpl/protocol/Feature.h>
9#include <xrpl/protocol/TER.h>
10#include <xrpl/protocol/TxFlags.h>
26 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid asset pair.";
30 if (
auto const bidMin = ctx.
tx[~sfBidMin])
34 JLOG(ctx.
j.
debug()) <<
"AMM Bid: invalid min slot price.";
39 if (
auto const bidMax = ctx.
tx[~sfBidMax])
43 JLOG(ctx.
j.
debug()) <<
"AMM Bid: invalid max slot price.";
53 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid number of AuthAccounts.";
60 for (
auto const& obj : authAccounts)
62 auto authAccount = obj[sfAccount];
63 if (authAccount == account || unique.contains(authAccount))
65 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid auth.account.";
68 unique.insert(authAccount);
83 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid asset pair.";
87 auto const lpTokensBalance = (*ammSle)[sfLPTokenBalance];
88 if (lpTokensBalance == beast::zero)
97 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid Account.";
103 auto const lpTokens =
106 if (lpTokens == beast::zero)
108 JLOG(ctx.
j.
debug()) <<
"AMM Bid: account is not LP.";
112 auto const bidMin = ctx.
tx[~sfBidMin];
116 if (bidMin->issue() != lpTokens.issue())
118 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid LPToken.";
121 if (*bidMin > lpTokens || *bidMin >= lpTokensBalance)
123 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid Tokens.";
128 auto const bidMax = ctx.
tx[~sfBidMax];
131 if (bidMax->issue() != lpTokens.issue())
133 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid LPToken.";
136 if (*bidMax > lpTokens || *bidMax >= lpTokensBalance)
138 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid Tokens.";
143 if (bidMin && bidMax && bidMin > bidMax)
145 JLOG(ctx.
j.
debug()) <<
"AMM Bid: Invalid Max/MinSlotPrice.";
164 STAmount const lptAMMBalance = (*ammSle)[sfLPTokenBalance];
167 if (!rules.enabled(fixInnerObjTemplate))
169 if (!ammSle->isFieldPresent(sfAuctionSlot))
170 ammSle->makeFieldPresent(sfAuctionSlot);
175 ammSle->isFieldPresent(sfAuctionSlot),
176 "ripple::applyBid : has auction slot");
177 if (!ammSle->isFieldPresent(sfAuctionSlot))
180 auto& auctionSlot = ammSle->peekFieldObject(sfAuctionSlot);
182 duration_cast<seconds>(
186 auto const discountedFee =
188 auto const tradingFee =
getFee((*ammSle)[sfTradingFee]);
190 auto const minSlotPrice =
200 auto validOwner = [&](
AccountID const& account) {
203 return timeSlot && *timeSlot < tailingSlot &&
210 auctionSlot.setAccountID(sfAccount, account_);
213 auctionSlot.setFieldU16(sfDiscountedFee, fee);
214 else if (auctionSlot.isFieldPresent(sfDiscountedFee))
215 auctionSlot.makeFieldAbsent(sfDiscountedFee);
216 auctionSlot.setFieldAmount(
217 sfPrice,
toSTAmount(lpTokens.issue(), minPrice));
219 auctionSlot.setFieldArray(
222 auctionSlot.makeFieldAbsent(sfAuthAccounts);
228 if (saBurn >= lptAMMBalance)
233 <<
"AMM Bid: LP Token burn exceeds AMM balance " << burn <<
" "
242 JLOG(ctx_.
journal.
debug()) <<
"AMM Bid: failed to redeem.";
245 ammSle->setFieldAmount(sfLPTokenBalance, lptAMMBalance - saBurn);
252 auto const bidMin = ctx_.
tx[~sfBidMin];
253 auto const bidMax = ctx_.
tx[~sfBidMax];
259 if (bidMin && bidMax)
261 if (computedPrice <= *bidMax)
264 <<
"AMM Bid: not in range " << computedPrice <<
" "
265 << *bidMin <<
" " << *bidMax;
275 if (computedPrice <= *bidMax)
276 return computedPrice;
278 << computedPrice <<
" " << *bidMax;
282 return computedPrice;
286 else if (payPrice > lpTokens)
292 if (
auto const acct = auctionSlot[~sfAccount]; !acct || !validOwner(*acct))
294 if (
auto const payPrice = getPayPrice(minSlotPrice); !payPrice)
295 return {payPrice.error(),
false};
297 res = updateSlot(discountedFee, *payPrice, *payPrice);
302 STAmount const pricePurchased = auctionSlot[sfPrice];
303 XRPL_ASSERT(timeSlot,
"ripple::applyBid : timeSlot is set");
304 auto const fractionUsed =
306 auto const fractionRemaining =
Number(1) - fractionUsed;
307 auto const computedPrice = [&]() ->
Number {
308 auto const p1_05 =
Number(105, -2);
311 return pricePurchased * p1_05 + minSlotPrice;
313 return pricePurchased * p1_05 * (1 -
power(fractionUsed, 60)) +
317 auto const payPrice = getPayPrice(computedPrice);
320 return {payPrice.error(),
false};
324 auto const refund = fractionRemaining * pricePurchased;
325 if (refund > *payPrice)
328 JLOG(ctx_.
journal.
fatal()) <<
"AMM Bid: refund exceeds payPrice "
329 << refund <<
" " << *payPrice;
335 auctionSlot[sfAccount],
340 JLOG(ctx_.
journal.
debug()) <<
"AMM Bid: failed to refund.";
344 auto const burn = *payPrice - refund;
345 res = updateSlot(discountedFee, *payPrice, burn);
A generic endpoint for log messages.
static TER preclaim(PreclaimContext const &ctx)
static bool checkExtraFeatures(PreflightContext 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.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Issue const & issue() const
STArray const & getFieldArray(SField const &field) const
bool isFieldPresent(SField const &field) 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.
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.
@ current
This was a new validation and was added.
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
std::uint32_t constexpr AUCTION_SLOT_MIN_FEE_FRACTION
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, IsDeposit isDeposit)
Adjust LP tokens to deposit/withdraw.
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.
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)
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
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)