20#include <xrpld/app/tx/detail/NFTokenAcceptOffer.h>
21#include <xrpld/app/tx/detail/NFTokenUtils.h>
23#include <xrpl/ledger/View.h>
24#include <xrpl/protocol/Feature.h>
25#include <xrpl/protocol/Rate.h>
26#include <xrpl/protocol/TxFlags.h>
42 auto const bo = ctx.
tx[~sfNFTokenBuyOffer];
43 auto const so = ctx.
tx[~sfNFTokenSellOffer];
51 if (
auto const bf = ctx.
tx[~sfNFTokenBrokerFee])
56 if (*bf <= beast::zero)
83 if ((*offerSLE)[sfAmount].negative() &&
92 auto const [bo, err1] = checkOffer(ctx.
tx[~sfNFTokenBuyOffer]);
95 auto const [so, err2] = checkOffer(ctx.
tx[~sfNFTokenSellOffer]);
103 if ((*bo)[sfNFTokenID] != (*so)[sfNFTokenID])
107 if ((*bo)[sfAmount].issue() != (*so)[sfAmount].issue())
113 ((*bo)[sfOwner] == (*so)[sfOwner]))
118 if ((*so)[sfAmount] > (*bo)[sfAmount])
122 if (
auto const dest = bo->at(~sfDestination))
128 if (*dest != ctx.
tx[sfAccount])
131 else if (*dest != so->at(sfOwner) && *dest != ctx.
tx[sfAccount])
136 if (
auto const dest = so->at(~sfDestination))
142 if (*dest != ctx.
tx[sfAccount])
145 else if (*dest != bo->at(sfOwner) && *dest != ctx.
tx[sfAccount])
153 if (
auto const brokerFee = ctx.
tx[~sfNFTokenBrokerFee])
155 if (brokerFee->issue() != (*bo)[sfAmount].issue())
158 if (brokerFee >= (*bo)[sfAmount])
161 if ((*so)[sfAmount] > (*bo)[sfAmount] - *brokerFee)
165 if (!brokerFee->native() &&
172 brokerFee->asset().get<
Issue>());
180 brokerFee->asset().get<
Issue>());
193 if ((*bo)[sfOwner] == ctx.
tx[sfAccount])
206 if (
auto const dest = bo->at(~sfDestination);
207 dest.has_value() && *dest != ctx.
tx[sfAccount])
215 auto const needed = bo->at(sfAmount);
227 needed.getCurrency(),
241 ctx.
view, bo->at(sfOwner), ctx.
j, needed.asset().get<
Issue>());
251 needed.asset().get<
Issue>());
259 needed.asset().get<
Issue>());
272 if ((*so)[sfOwner] == ctx.
tx[sfAccount])
284 if (
auto const dest = so->at(~sfDestination);
285 dest.has_value() && *dest != ctx.
tx[sfAccount])
290 auto const needed = so->at(sfAmount);
296 needed.getCurrency(),
326 if (!needed.native())
334 needed.asset().get<
Issue>());
344 needed.asset().get<
Issue>());
351 ctx.
view, (*so)[sfOwner], ctx.
j, needed.asset().get<
Issue>());
359 auto const& offer = bo ? bo : so;
364 auto const& tokenID = offer->at(sfNFTokenID);
365 auto const& amount = offer->at(sfAmount);
375 nftMinter != amount.getIssuer() &&
383 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
388 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
404 if (amount < beast::zero)
414 if (!
view().rules().enabled(fixNonFungibleTokensV1_2))
437 view(), seller, nftokenID, std::move(tokenAndPage->page));
446 sleBuyer->getFieldU32(sfOwnerCount);
448 auto const insertRet =
457 if (
view().rules().enabled(fixNFTokenReserve))
464 auto const buyerBalance = sleBuyer->getFieldAmount(sfBalance);
466 auto const buyerOwnerCountAfter = sleBuyer->getFieldU32(sfOwnerCount);
467 if (buyerOwnerCountAfter > buyerOwnerCountBefore)
469 if (
auto const reserve =
470 view().fees().accountReserve(buyerOwnerCountAfter);
471 buyerBalance < reserve)
483 AccountID const owner = (*offer)[sfOwner];
487 auto const nftokenID = (*offer)[sfNFTokenID];
489 if (
auto amount = offer->getFieldAmount(sfAmount); amount != beast::zero)
497 cut != beast::zero && seller != issuer && buyer != issuer)
524 auto bo = loadToken(
ctx_.
tx[~sfNFTokenBuyOffer]);
525 auto so = loadToken(
ctx_.
tx[~sfNFTokenSellOffer]);
529 JLOG(
j_.
fatal()) <<
"Unable to delete buy offer '"
530 <<
to_string(bo->key()) <<
"': ignoring";
536 JLOG(
j_.
fatal()) <<
"Unable to delete sell offer '"
537 <<
to_string(so->key()) <<
"': ignoring";
547 auto const nftokenID = (*so)[sfNFTokenID];
566 if (
auto const cut =
ctx_.
tx[~sfNFTokenBrokerFee];
567 cut && cut.value() != beast::zero)
569 if (
auto const r =
pay(buyer,
account_, cut.value());
573 amount -= cut.
value();
578 amount != beast::zero && fee != 0)
583 seller != issuer && buyer != issuer)
593 if (amount > beast::zero)
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
A currency issued by an account.
TER acceptOffer(std::shared_ptr< SLE > const &offer)
static TER preclaim(PreclaimContext const &ctx)
TER pay(AccountID const &from, AccountID const &to, STAmount const &amount)
TER transferNFToken(AccountID const &buyer, AccountID const &seller, uint256 const &nfTokenID)
static NotTEC preflight(PreflightContext const &ctx)
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.
STAmount const & value() const noexcept
std::uint32_t getFlags() const
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet nftoffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
std::uint16_t getTransferFee(uint256 const &id)
std::uint16_t getFlags(uint256 const &id)
TER removeToken(ApplyView &view, AccountID const &owner, uint256 const &nftokenID)
Remove the token from the owner's token directory.
AccountID getIssuer(uint256 const &id)
TER checkTrustlineDeepFrozen(ReadView const &view, AccountID const id, beast::Journal const j, Issue const &issue)
bool deleteTokenOffer(ApplyView &view, std::shared_ptr< SLE > const &offer)
Deletes the given token offer.
std::optional< TokenAndPage > findTokenAndPage(ApplyView &view, AccountID const &owner, uint256 const &nftokenID)
TER insertToken(ApplyView &view, AccountID owner, STObject &&nft)
Insert the token in the owner's token directory.
std::optional< STObject > findToken(ReadView const &view, AccountID const &owner, uint256 const &nftokenID)
Finds the specified token in the owner's token directory.
constexpr std::uint16_t const flagCreateTrustLines
TER checkTrustlineAuthorized(ReadView const &view, AccountID const id, beast::Journal const j, Issue const &issue)
Rate transferFeeAsRate(std::uint16_t fee)
Given a transfer fee (in basis points) convert it to a transfer rate.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
STAmount multiply(STAmount const &amount, Rate const &rate)
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
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.
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
@ tecNFTOKEN_OFFER_TYPE_MISMATCH
@ tecNFTOKEN_BUY_SELL_MISMATCH
@ tecINSUFFICIENT_PAYMENT
@ tecINSUFFICIENT_RESERVE
@ tecCANT_ACCEPT_OWN_NFTOKEN_OFFER
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
bool isTesSuccess(TER x) noexcept
std::string to_string(base_uint< Bits, Tag > const &a)
constexpr std::uint32_t const tfNFTokenAcceptOfferMask
TERSubset< CanCvtToNotTEC > NotTEC
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.