1#include <xrpl/tx/transactors/nft/NFTokenAcceptOffer.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/core/ServiceRegistry.h>
7#include <xrpl/ledger/View.h>
8#include <xrpl/ledger/helpers/NFTokenHelpers.h>
9#include <xrpl/ledger/helpers/TokenHelpers.h>
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/Indexes.h>
12#include <xrpl/protocol/Issue.h>
13#include <xrpl/protocol/LedgerFormats.h>
14#include <xrpl/protocol/Rate.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STLedgerEntry.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/TER.h>
19#include <xrpl/protocol/XRPAmount.h>
20#include <xrpl/protocol/nft.h>
21#include <xrpl/tx/Transactor.h>
32 auto const bo = ctx.
tx[~sfNFTokenBuyOffer];
33 auto const so = ctx.
tx[~sfNFTokenSellOffer];
41 if (
auto const bf = ctx.
tx[~sfNFTokenBrokerFee])
46 if (*bf <= beast::kZero)
56 auto const checkOffer =
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].asset() != (*so)[sfAmount].asset())
106 if (((*bo)[sfOwner] == (*so)[sfOwner]))
111 if ((*so)[sfAmount] > (*bo)[sfAmount])
116 if (
auto const dest = bo->at(~sfDestination); dest && *dest != ctx.
tx[sfAccount])
123 if (
auto const dest = so->at(~sfDestination); dest && *dest != ctx.
tx[sfAccount])
132 if (
auto const brokerFee = ctx.
tx[~sfNFTokenBrokerFee])
134 if (brokerFee->asset() != (*bo)[sfAmount].asset())
137 if (brokerFee >= (*bo)[sfAmount])
140 if ((*so)[sfAmount] > (*bo)[sfAmount] - *brokerFee)
144 if (!brokerFee->native() && ctx.
view.
rules().
enabled(fixEnforceNFTokenTrustlineV2))
147 ctx.
view, ctx.
tx[sfAccount], ctx.
j, brokerFee->asset().get<
Issue>());
152 ctx.
view, ctx.
tx[sfAccount], ctx.
j, brokerFee->asset().get<
Issue>());
161 if (bo->isFlag(lsfSellNFToken))
165 if ((*bo)[sfOwner] == ctx.
tx[sfAccount])
177 if (
auto const dest = bo->at(~sfDestination);
178 dest.has_value() && *dest != ctx.
tx[sfAccount])
186 auto const needed = bo->at(sfAmount);
196 if (ctx.
view.
rules().
enabled(fixEnforceNFTokenTrustlineV2) && !needed.native())
199 ctx.
view, bo->at(sfOwner), ctx.
j, needed.asset().get<
Issue>());
206 ctx.
view, ctx.
tx[sfAccount], ctx.
j, needed.asset().get<
Issue>());
211 ctx.
view, ctx.
tx[sfAccount], ctx.
j, needed.asset().get<
Issue>());
220 if (!so->isFlag(lsfSellNFToken))
224 if ((*so)[sfOwner] == ctx.
tx[sfAccount])
236 if (
auto const dest = so->at(~sfDestination);
237 dest.has_value() && *dest != ctx.
tx[sfAccount])
242 auto const needed = so->at(sfAmount);
264 if (!needed.native())
269 ctx.
view, (*so)[sfOwner], ctx.
j, needed.asset().get<
Issue>());
276 ctx.
view, ctx.
tx[sfAccount], ctx.
j, needed.asset().get<
Issue>());
283 ctx.
view, (*so)[sfOwner], ctx.
j, needed.asset().get<
Issue>());
291 auto const& offer = bo ? bo : so;
298 auto const& tokenID = offer->at(sfNFTokenID);
299 auto const& amount = offer->at(sfAmount);
309 nftMinter != amount.getIssuer() &&
317 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
322 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
335 if (amount < beast::kZero)
373 std::uint32_t const buyerOwnerCountBefore = sleBuyer->getFieldU32(sfOwnerCount);
385 auto const buyerBalance = sleBuyer->getFieldAmount(sfBalance);
387 auto const buyerOwnerCountAfter = sleBuyer->getFieldU32(sfOwnerCount);
388 if (buyerOwnerCountAfter > buyerOwnerCountBefore)
390 if (
auto const reserve =
view().fees().accountReserve(buyerOwnerCountAfter);
391 buyerBalance < reserve)
401 bool const isSell = offer->isFlag(lsfSellNFToken);
402 AccountID const owner = (*offer)[sfOwner];
406 auto const nftokenID = (*offer)[sfNFTokenID];
408 if (
auto amount = offer->getFieldAmount(sfAmount); amount != beast::kZero)
416 cut != beast::kZero && seller != issuer && buyer != issuer)
443 auto bo = loadToken(
ctx_.tx[~sfNFTokenBuyOffer]);
444 auto so = loadToken(
ctx_.tx[~sfNFTokenSellOffer]);
448 if (
view().rules().enabled(fixCleanup3_1_3))
450 bool foundExpired =
false;
452 auto const deleteOfferIfExpired = [
this, &foundExpired](
SLE::ref offer) ->
TER {
455 JLOG(
j_.trace()) <<
"Offer is expired, deleting: " << offer->
key();
460 <<
"Unable to delete expired offer '" << offer->
key() <<
"': ignoring";
464 JLOG(
j_.trace()) <<
"Deleted offer " << offer->
key();
470 if (
auto const r = deleteOfferIfExpired(bo); !
isTesSuccess(r))
472 if (
auto const r = deleteOfferIfExpired(so); !
isTesSuccess(r))
482 JLOG(
j_.fatal()) <<
"Unable to delete buy offer '" <<
to_string(bo->key()) <<
"': ignoring";
490 JLOG(
j_.fatal()) <<
"Unable to delete sell offer '" <<
to_string(so->key())
502 auto const nftokenID = (*so)[sfNFTokenID];
521 if (
auto const cut =
ctx_.tx[~sfNFTokenBrokerFee]; cut && cut.value() != beast::kZero)
526 amount -= cut.
value();
534 if (
auto const issuer =
nft::getIssuer(nftokenID); seller != issuer && buyer != issuer)
544 if (amount > beast::kZero)
A generic endpoint for log messages.
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
A currency issued by an account.
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
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)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
TER acceptOffer(SLE::ref offer)
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual SLE::const_pointer 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.
STAmount const & value() const noexcept
std::shared_ptr< STLedgerEntry > const & ref
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry const > const & const_ref
AccountID const accountID_
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet nftokenOffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
TER insertToken(ApplyView &view, AccountID owner, STObject &&nft)
Insert the token in the owner's token directory.
constexpr std::uint16_t const kFlagCreateTrustLines
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)
TER checkTrustlineAuthorized(ReadView const &view, AccountID const id, beast::Journal const j, Issue const &issue)
std::optional< STObject > findToken(ReadView const &view, AccountID const &owner, uint256 const &nftokenID)
Finds the specified token in the owner's token directory.
AccountID getIssuer(uint256 const &id)
std::uint16_t getTransferFee(uint256 const &id)
bool deleteTokenOffer(ApplyView &view, SLE::ref offer)
Deletes the given token offer.
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.
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
std::string to_string(BaseUInt< Bits, Tag > const &a)
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
TERSubset< CanCvtToNotTEC > NotTEC
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
@ tecCANT_ACCEPT_OWN_NFTOKEN_OFFER
@ tecNFTOKEN_BUY_SELL_MISMATCH
@ tecNFTOKEN_OFFER_TYPE_MISMATCH
@ tecINSUFFICIENT_RESERVE
@ tecINSUFFICIENT_PAYMENT
STAmount multiply(STAmount const &amount, Number const &frac, Number::RoundingMode rm)
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.