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/AccountRootHelpers.h>
9#include <xrpl/ledger/helpers/NFTokenHelpers.h>
10#include <xrpl/ledger/helpers/TokenHelpers.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/Issue.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/Rate.h>
16#include <xrpl/protocol/SField.h>
17#include <xrpl/protocol/STLedgerEntry.h>
18#include <xrpl/protocol/STTx.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/XRPAmount.h>
21#include <xrpl/protocol/nft.h>
22#include <xrpl/tx/Transactor.h>
33 auto const bo = ctx.
tx[~sfNFTokenBuyOffer];
34 auto const so = ctx.
tx[~sfNFTokenSellOffer];
42 if (
auto const bf = ctx.
tx[~sfNFTokenBrokerFee])
57 auto const checkOffer =
79 if ((*offerSLE)[sfAmount].negative())
87 auto const [bo, err1] = checkOffer(ctx.
tx[~sfNFTokenBuyOffer]);
90 auto const [so, err2] = checkOffer(ctx.
tx[~sfNFTokenSellOffer]);
98 if ((*bo)[sfNFTokenID] != (*so)[sfNFTokenID])
102 if ((*bo)[sfAmount].asset() != (*so)[sfAmount].asset())
107 if (((*bo)[sfOwner] == (*so)[sfOwner]))
112 if ((*so)[sfAmount] > (*bo)[sfAmount])
117 if (
auto const dest = bo->at(~sfDestination); dest && *dest != ctx.
tx[sfAccount])
124 if (
auto const dest = so->at(~sfDestination); dest && *dest != ctx.
tx[sfAccount])
133 if (
auto const brokerFee = ctx.
tx[~sfNFTokenBrokerFee])
135 if (brokerFee->asset() != (*bo)[sfAmount].asset())
138 if (brokerFee >= (*bo)[sfAmount])
141 if ((*so)[sfAmount] > (*bo)[sfAmount] - *brokerFee)
145 if (!brokerFee->native() && ctx.
view.
rules().
enabled(fixEnforceNFTokenTrustlineV2))
148 ctx.
view, ctx.
tx[sfAccount], ctx.
j, brokerFee->asset().get<
Issue>());
153 ctx.
view, ctx.
tx[sfAccount], ctx.
j, brokerFee->asset().get<
Issue>());
162 if (bo->isFlag(lsfSellNFToken))
166 if ((*bo)[sfOwner] == ctx.
tx[sfAccount])
178 if (
auto const dest = bo->at(~sfDestination);
179 dest.has_value() && *dest != ctx.
tx[sfAccount])
187 auto const needed = bo->at(sfAmount);
197 if (ctx.
view.
rules().
enabled(fixEnforceNFTokenTrustlineV2) && !needed.native())
200 ctx.
view, bo->at(sfOwner), ctx.
j, needed.asset().get<
Issue>());
207 ctx.
view, ctx.
tx[sfAccount], ctx.
j, needed.asset().get<
Issue>());
212 ctx.
view, ctx.
tx[sfAccount], ctx.
j, needed.asset().get<
Issue>());
221 if (!so->isFlag(lsfSellNFToken))
225 if ((*so)[sfOwner] == ctx.
tx[sfAccount])
237 if (
auto const dest = so->at(~sfDestination);
238 dest.has_value() && *dest != ctx.
tx[sfAccount])
243 auto const needed = so->at(sfAmount);
265 if (!needed.native())
270 ctx.
view, (*so)[sfOwner], ctx.
j, needed.asset().get<
Issue>());
277 ctx.
view, ctx.
tx[sfAccount], ctx.
j, needed.asset().get<
Issue>());
284 ctx.
view, (*so)[sfOwner], ctx.
j, needed.asset().get<
Issue>());
292 auto const& offer = bo ? bo : so;
299 auto const& tokenID = offer->at(sfNFTokenID);
300 auto const& amount = offer->at(sfAmount);
310 nftMinter != amount.getIssuer() &&
318 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
323 ctx.
view, nftMinter, ctx.
j, amount.asset().get<
Issue>());
374 std::uint32_t const buyerOwnerCountBefore = sleBuyer->getFieldU32(sfOwnerCount);
386 auto const buyerBalance = sleBuyer->getFieldAmount(sfBalance);
388 auto const buyerOwnerCountAfter = sleBuyer->getFieldU32(sfOwnerCount);
389 if (buyerOwnerCountAfter > buyerOwnerCountBefore)
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)
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 nftokenOffer(AccountID const &owner, SeqProxy const &seq)
An offer from an account to buy or sell an NFT.
Keylet account(AccountID const &id) noexcept
AccountID root.
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, ExpiryComparison comparison=ExpiryComparison::Inclusive)
Determines whether the given expiration time has passed.
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, SLE::ref sponsorSle={}, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
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
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)
XRPAmount accountReserve(ReadView const &view, SLE::const_ref sle, beast::Journal j, Adjustment adj={})
Returns the account reserve, in drops.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.