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)
80 if ((*offerSLE)[sfAmount].negative() &&
89 auto const [bo, err1] = checkOffer(ctx.
tx[~sfNFTokenBuyOffer]);
92 auto const [so, err2] = checkOffer(ctx.
tx[~sfNFTokenSellOffer]);
100 if ((*bo)[sfNFTokenID] != (*so)[sfNFTokenID])
104 if ((*bo)[sfAmount].issue() != (*so)[sfAmount].issue())
110 ((*bo)[sfOwner] == (*so)[sfOwner]))
115 if ((*so)[sfAmount] > (*bo)[sfAmount])
119 if (
auto const dest = bo->at(~sfDestination))
125 if (*dest != ctx.
tx[sfAccount])
128 else if (*dest != so->at(sfOwner) && *dest != ctx.
tx[sfAccount])
133 if (
auto const dest = so->at(~sfDestination))
139 if (*dest != ctx.
tx[sfAccount])
142 else if (*dest != bo->at(sfOwner) && *dest != ctx.
tx[sfAccount])
150 if (
auto const brokerFee = ctx.
tx[~sfNFTokenBrokerFee])
152 if (brokerFee->issue() != (*bo)[sfAmount].issue())
155 if (brokerFee >= (*bo)[sfAmount])
158 if ((*so)[sfAmount] > (*bo)[sfAmount] - *brokerFee)
162 if (!brokerFee->native() &&
169 brokerFee->asset().get<
Issue>());
177 brokerFee->asset().get<
Issue>());
190 if ((*bo)[sfOwner] == ctx.
tx[sfAccount])
203 if (
auto const dest = bo->at(~sfDestination);
204 dest.has_value() && *dest != ctx.
tx[sfAccount])
212 auto const needed = bo->at(sfAmount);
224 needed.getCurrency(),
238 ctx.
view, bo->at(sfOwner), ctx.
j, needed.asset().get<
Issue>());
248 needed.asset().get<
Issue>());
256 needed.asset().get<
Issue>());
269 if ((*so)[sfOwner] == ctx.
tx[sfAccount])
281 if (
auto const dest = so->at(~sfDestination);
282 dest.has_value() && *dest != ctx.
tx[sfAccount])
287 auto const needed = so->at(sfAmount);
293 needed.getCurrency(),
323 if (!needed.native())
331 needed.asset().get<
Issue>());
341 needed.asset().get<
Issue>());
348 ctx.
view, (*so)[sfOwner], ctx.
j, needed.asset().get<
Issue>());
356 auto const& offer = bo ? bo : so;
361 auto const& tokenID = offer->at(sfNFTokenID);
362 auto const& amount = offer->at(sfAmount);
372 nftMinter != amount.getIssuer() &&
380 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
385 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
401 if (amount < beast::zero)
411 if (!
view().rules().enabled(fixNonFungibleTokensV1_2))
434 view(), seller, nftokenID, std::move(tokenAndPage->page));
443 sleBuyer->getFieldU32(sfOwnerCount);
445 auto const insertRet =
454 if (
view().rules().enabled(fixNFTokenReserve))
461 auto const buyerBalance = sleBuyer->getFieldAmount(sfBalance);
463 auto const buyerOwnerCountAfter = sleBuyer->getFieldU32(sfOwnerCount);
464 if (buyerOwnerCountAfter > buyerOwnerCountBefore)
466 if (
auto const reserve =
467 view().fees().accountReserve(buyerOwnerCountAfter);
468 buyerBalance < reserve)
480 AccountID const owner = (*offer)[sfOwner];
484 auto const nftokenID = (*offer)[sfNFTokenID];
486 if (
auto amount = offer->getFieldAmount(sfAmount); amount != beast::zero)
494 cut != beast::zero && seller != issuer && buyer != issuer)
521 auto bo = loadToken(
ctx_.
tx[~sfNFTokenBuyOffer]);
522 auto so = loadToken(
ctx_.
tx[~sfNFTokenSellOffer]);
527 JLOG(
j_.
fatal()) <<
"Unable to delete buy offer '"
528 <<
to_string(bo->key()) <<
"': ignoring";
536 JLOG(
j_.
fatal()) <<
"Unable to delete sell offer '"
537 <<
to_string(so->key()) <<
"': ignoring";
548 auto const nftokenID = (*so)[sfNFTokenID];
567 if (
auto const cut =
ctx_.
tx[~sfNFTokenBrokerFee];
568 cut && cut.value() != beast::zero)
570 if (
auto const r =
pay(buyer,
account_, cut.value());
574 amount -= cut.
value();
579 amount != beast::zero && fee != 0)
584 seller != issuer && buyer != issuer)
594 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
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
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.