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)
68 if ((*offerSLE)[sfAmount].negative())
76 auto const [bo, err1] = checkOffer(ctx.
tx[~sfNFTokenBuyOffer]);
79 auto const [so, err2] = checkOffer(ctx.
tx[~sfNFTokenSellOffer]);
87 if ((*bo)[sfNFTokenID] != (*so)[sfNFTokenID])
91 if ((*bo)[sfAmount].issue() != (*so)[sfAmount].issue())
96 if (((*bo)[sfOwner] == (*so)[sfOwner]))
101 if ((*so)[sfAmount] > (*bo)[sfAmount])
106 if (
auto const dest = bo->at(~sfDestination); dest && *dest != ctx.
tx[sfAccount])
113 if (
auto const dest = so->at(~sfDestination); dest && *dest != ctx.
tx[sfAccount])
122 if (
auto const brokerFee = ctx.
tx[~sfNFTokenBrokerFee])
124 if (brokerFee->issue() != (*bo)[sfAmount].issue())
127 if (brokerFee >= (*bo)[sfAmount])
130 if ((*so)[sfAmount] > (*bo)[sfAmount] - *brokerFee)
134 if (!brokerFee->native() && ctx.
view.
rules().
enabled(fixEnforceNFTokenTrustlineV2))
155 if ((*bo)[sfOwner] == ctx.
tx[sfAccount])
167 if (
auto const dest = bo->at(~sfDestination); dest.has_value() && *dest != ctx.
tx[sfAccount])
175 auto const needed = bo->at(sfAmount);
184 if (ctx.
view.
rules().
enabled(fixEnforceNFTokenTrustlineV2) && !needed.native())
209 if ((*so)[sfOwner] == ctx.
tx[sfAccount])
221 if (
auto const dest = so->at(~sfDestination); dest.has_value() && *dest != ctx.
tx[sfAccount])
226 auto const needed = so->at(sfAmount);
246 if (!needed.native())
272 auto const& offer = bo ? bo : so;
277 auto const& tokenID = offer->at(sfNFTokenID);
278 auto const& amount = offer->at(sfAmount);
311 if (amount < beast::zero)
345 std::uint32_t const buyerOwnerCountBefore = sleBuyer->getFieldU32(sfOwnerCount);
355 if (
view().rules().enabled(fixNFTokenReserve))
362 auto const buyerBalance = sleBuyer->getFieldAmount(sfBalance);
364 auto const buyerOwnerCountAfter = sleBuyer->getFieldU32(sfOwnerCount);
365 if (buyerOwnerCountAfter > buyerOwnerCountBefore)
367 if (
auto const reserve =
view().fees().accountReserve(buyerOwnerCountAfter); buyerBalance < reserve)
379 AccountID const owner = (*offer)[sfOwner];
383 auto const nftokenID = (*offer)[sfNFTokenID];
385 if (
auto amount = offer->getFieldAmount(sfAmount); amount != beast::zero)
393 cut != beast::zero && seller != issuer && buyer != issuer)
420 auto bo = loadToken(
ctx_.
tx[~sfNFTokenBuyOffer]);
421 auto so = loadToken(
ctx_.
tx[~sfNFTokenSellOffer]);
426 if (
view().rules().enabled(fixExpiredNFTokenOfferRemoval))
428 bool foundExpired =
false;
433 JLOG(
j_.
trace()) <<
"Offer is expired, deleting: " << offer->
key();
437 JLOG(
j_.
fatal()) <<
"Unable to delete expired offer '" << offer->
key() <<
"': ignoring";
441 JLOG(
j_.
trace()) <<
"Deleted offer " << offer->
key();
447 if (
auto const r = deleteOfferIfExpired(bo); !
isTesSuccess(r))
449 if (
auto const r = deleteOfferIfExpired(so); !
isTesSuccess(r))
459 JLOG(
j_.
fatal()) <<
"Unable to delete buy offer '" <<
to_string(bo->key()) <<
"': ignoring";
467 JLOG(
j_.
fatal()) <<
"Unable to delete sell offer '" <<
to_string(so->key()) <<
"': ignoring";
478 auto const nftokenID = (*so)[sfNFTokenID];
497 if (
auto const cut =
ctx_.
tx[~sfNFTokenBrokerFee]; cut && cut.value() != beast::zero)
502 amount -= cut.value();
510 if (
auto const issuer =
nft::getIssuer(nftokenID); seller != issuer && buyer != issuer)
520 if (amount > beast::zero)
Stream trace() const
Severity stream access functions.
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)
TER transferNFToken(AccountID const &buyer, AccountID const &seller, uint256 const &nfTokenID)
TER pay(AccountID const &from, AccountID const &to, STAmount const &amount)
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Keylet nftoffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
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.
TER insertToken(ApplyView &view, AccountID owner, STObject &&nft)
Insert the token in the owner's token directory.
constexpr std::uint16_t const flagCreateTrustLines
std::optional< STObject > findToken(ReadView const &view, AccountID const &owner, uint256 const &nftokenID)
Finds the specified token in the owner's token directory.
TER removeToken(ApplyView &view, AccountID const &owner, uint256 const &nftokenID)
Remove the token from the owner's token directory.
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.
TER checkTrustlineAuthorized(ReadView const &view, AccountID const id, beast::Journal const j, Issue const &issue)
AccountID getIssuer(uint256 const &id)
std::uint16_t getTransferFee(uint256 const &id)
std::optional< TokenAndPage > findTokenAndPage(ApplyView &view, AccountID const &owner, uint256 const &nftokenID)
Rate transferFeeAsRate(std::uint16_t fee)
Given a transfer fee (in basis points) convert it to a transfer rate.
std::uint16_t getFlags(uint256 const &id)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string to_string(base_uint< Bits, Tag > const &a)
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
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.
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
constexpr std::uint32_t const tfNFTokenAcceptOfferMask
bool isTesSuccess(TER x) noexcept
@ tecCANT_ACCEPT_OWN_NFTOKEN_OFFER
@ tecNFTOKEN_BUY_SELL_MISMATCH
@ tecNFTOKEN_OFFER_TYPE_MISMATCH
@ tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_PAYMENT
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.