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>
39 auto const bo = ctx.
tx[~sfNFTokenBuyOffer];
40 auto const so = ctx.
tx[~sfNFTokenSellOffer];
48 if (
auto const bf = ctx.
tx[~sfNFTokenBrokerFee])
53 if (*bf <= beast::zero)
78 if ((*offerSLE)[sfAmount].negative())
86 auto const [bo, err1] = checkOffer(ctx.
tx[~sfNFTokenBuyOffer]);
89 auto const [so, err2] = checkOffer(ctx.
tx[~sfNFTokenSellOffer]);
97 if ((*bo)[sfNFTokenID] != (*so)[sfNFTokenID])
101 if ((*bo)[sfAmount].issue() != (*so)[sfAmount].issue())
106 if (((*bo)[sfOwner] == (*so)[sfOwner]))
111 if ((*so)[sfAmount] > (*bo)[sfAmount])
116 if (
auto const dest = bo->at(~sfDestination);
117 dest && *dest != ctx.
tx[sfAccount])
124 if (
auto const dest = so->at(~sfDestination);
125 dest && *dest != ctx.
tx[sfAccount])
134 if (
auto const brokerFee = ctx.
tx[~sfNFTokenBrokerFee])
136 if (brokerFee->issue() != (*bo)[sfAmount].issue())
139 if (brokerFee >= (*bo)[sfAmount])
142 if ((*so)[sfAmount] > (*bo)[sfAmount] - *brokerFee)
146 if (!brokerFee->native() &&
153 brokerFee->asset().get<
Issue>());
161 brokerFee->asset().get<
Issue>());
174 if ((*bo)[sfOwner] == ctx.
tx[sfAccount])
187 if (
auto const dest = bo->at(~sfDestination);
188 dest.has_value() && *dest != ctx.
tx[sfAccount])
196 auto const needed = bo->at(sfAmount);
211 ctx.
view, bo->at(sfOwner), ctx.
j, needed.asset().get<
Issue>());
221 needed.asset().get<
Issue>());
229 needed.asset().get<
Issue>());
242 if ((*so)[sfOwner] == ctx.
tx[sfAccount])
254 if (
auto const dest = so->at(~sfDestination);
255 dest.has_value() && *dest != ctx.
tx[sfAccount])
260 auto const needed = so->at(sfAmount);
285 if (!needed.native())
293 needed.asset().get<
Issue>());
303 needed.asset().get<
Issue>());
310 ctx.
view, (*so)[sfOwner], ctx.
j, needed.asset().get<
Issue>());
318 auto const& offer = bo ? bo : so;
323 auto const& tokenID = offer->at(sfNFTokenID);
324 auto const& amount = offer->at(sfAmount);
334 nftMinter != amount.getIssuer() &&
342 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
347 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
363 if (amount < beast::zero)
394 view(), seller, nftokenID, std::move(tokenAndPage->page));
403 sleBuyer->getFieldU32(sfOwnerCount);
405 auto const insertRet =
414 if (
view().rules().enabled(fixNFTokenReserve))
421 auto const buyerBalance = sleBuyer->getFieldAmount(sfBalance);
423 auto const buyerOwnerCountAfter = sleBuyer->getFieldU32(sfOwnerCount);
424 if (buyerOwnerCountAfter > buyerOwnerCountBefore)
426 if (
auto const reserve =
427 view().fees().accountReserve(buyerOwnerCountAfter);
428 buyerBalance < reserve)
440 AccountID const owner = (*offer)[sfOwner];
444 auto const nftokenID = (*offer)[sfNFTokenID];
446 if (
auto amount = offer->getFieldAmount(sfAmount); amount != beast::zero)
454 cut != beast::zero && seller != issuer && buyer != issuer)
481 auto bo = loadToken(
ctx_.
tx[~sfNFTokenBuyOffer]);
482 auto so = loadToken(
ctx_.
tx[~sfNFTokenSellOffer]);
487 JLOG(
j_.
fatal()) <<
"Unable to delete buy offer '"
488 <<
to_string(bo->key()) <<
"': ignoring";
496 JLOG(
j_.
fatal()) <<
"Unable to delete sell offer '"
497 <<
to_string(so->key()) <<
"': ignoring";
508 auto const nftokenID = (*so)[sfNFTokenID];
527 if (
auto const cut =
ctx_.
tx[~sfNFTokenBrokerFee];
528 cut && cut.value() != beast::zero)
530 if (
auto const r =
pay(buyer,
account_, cut.value());
534 amount -= cut.
value();
539 amount != beast::zero && fee != 0)
544 seller != issuer && buyer != issuer)
554 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.