1#include <xrpld/app/tx/detail/NFTokenAcceptOffer.h>
2#include <xrpld/app/tx/detail/NFTokenUtils.h>
4#include <xrpl/ledger/View.h>
5#include <xrpl/protocol/Feature.h>
6#include <xrpl/protocol/Rate.h>
7#include <xrpl/protocol/TxFlags.h>
20 auto const bo = ctx.
tx[~sfNFTokenBuyOffer];
21 auto const so = ctx.
tx[~sfNFTokenSellOffer];
29 if (
auto const bf = ctx.
tx[~sfNFTokenBrokerFee])
34 if (*bf <= beast::zero)
59 if ((*offerSLE)[sfAmount].negative())
67 auto const [bo, err1] = checkOffer(ctx.
tx[~sfNFTokenBuyOffer]);
70 auto const [so, err2] = checkOffer(ctx.
tx[~sfNFTokenSellOffer]);
78 if ((*bo)[sfNFTokenID] != (*so)[sfNFTokenID])
82 if ((*bo)[sfAmount].issue() != (*so)[sfAmount].issue())
87 if (((*bo)[sfOwner] == (*so)[sfOwner]))
92 if ((*so)[sfAmount] > (*bo)[sfAmount])
97 if (
auto const dest = bo->at(~sfDestination);
98 dest && *dest != ctx.
tx[sfAccount])
105 if (
auto const dest = so->at(~sfDestination);
106 dest && *dest != ctx.
tx[sfAccount])
115 if (
auto const brokerFee = ctx.
tx[~sfNFTokenBrokerFee])
117 if (brokerFee->issue() != (*bo)[sfAmount].issue())
120 if (brokerFee >= (*bo)[sfAmount])
123 if ((*so)[sfAmount] > (*bo)[sfAmount] - *brokerFee)
127 if (!brokerFee->native() &&
134 brokerFee->asset().get<
Issue>());
142 brokerFee->asset().get<
Issue>());
155 if ((*bo)[sfOwner] == ctx.
tx[sfAccount])
168 if (
auto const dest = bo->at(~sfDestination);
169 dest.has_value() && *dest != ctx.
tx[sfAccount])
177 auto const needed = bo->at(sfAmount);
192 ctx.
view, bo->at(sfOwner), ctx.
j, needed.asset().get<
Issue>());
202 needed.asset().get<
Issue>());
210 needed.asset().get<
Issue>());
223 if ((*so)[sfOwner] == ctx.
tx[sfAccount])
235 if (
auto const dest = so->at(~sfDestination);
236 dest.has_value() && *dest != ctx.
tx[sfAccount])
241 auto const needed = so->at(sfAmount);
266 if (!needed.native())
274 needed.asset().get<
Issue>());
284 needed.asset().get<
Issue>());
291 ctx.
view, (*so)[sfOwner], ctx.
j, needed.asset().get<
Issue>());
299 auto const& offer = bo ? bo : so;
304 auto const& tokenID = offer->at(sfNFTokenID);
305 auto const& amount = offer->at(sfAmount);
315 nftMinter != amount.getIssuer() &&
323 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
328 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
344 if (amount < beast::zero)
375 view(), seller, nftokenID, std::move(tokenAndPage->page));
384 sleBuyer->getFieldU32(sfOwnerCount);
386 auto const insertRet =
395 if (
view().rules().enabled(fixNFTokenReserve))
402 auto const buyerBalance = sleBuyer->getFieldAmount(sfBalance);
404 auto const buyerOwnerCountAfter = sleBuyer->getFieldU32(sfOwnerCount);
405 if (buyerOwnerCountAfter > buyerOwnerCountBefore)
407 if (
auto const reserve =
408 view().fees().accountReserve(buyerOwnerCountAfter);
409 buyerBalance < reserve)
421 AccountID const owner = (*offer)[sfOwner];
425 auto const nftokenID = (*offer)[sfNFTokenID];
427 if (
auto amount = offer->getFieldAmount(sfAmount); amount != beast::zero)
435 cut != beast::zero && seller != issuer && buyer != issuer)
462 auto bo = loadToken(
ctx_.
tx[~sfNFTokenBuyOffer]);
463 auto so = loadToken(
ctx_.
tx[~sfNFTokenSellOffer]);
468 JLOG(
j_.
fatal()) <<
"Unable to delete buy offer '"
469 <<
to_string(bo->key()) <<
"': ignoring";
477 JLOG(
j_.
fatal()) <<
"Unable to delete sell offer '"
478 <<
to_string(so->key()) <<
"': ignoring";
489 auto const nftokenID = (*so)[sfNFTokenID];
508 if (
auto const cut =
ctx_.
tx[~sfNFTokenBrokerFee];
509 cut && cut.value() != beast::zero)
511 if (
auto const r =
pay(buyer,
account_, cut.value());
515 amount -= cut.
value();
520 amount != beast::zero && fee != 0)
525 seller != issuer && buyer != issuer)
535 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 std::uint32_t getFlagsMask(PreflightContext const &ctx)
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
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)
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.
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
bool isTesSuccess(TER x) noexcept
std::string to_string(base_uint< Bits, Tag > const &a)
constexpr std::uint32_t const tfNFTokenAcceptOfferMask
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.