1#include <xrpl/tx/transactors/escrow/EscrowCreate.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/chrono.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/conditions/Condition.h>
7#include <xrpl/core/ServiceRegistry.h>
8#include <xrpl/ledger/ApplyView.h>
9#include <xrpl/ledger/View.h>
10#include <xrpl/ledger/helpers/AccountRootHelpers.h>
11#include <xrpl/ledger/helpers/DirectoryHelpers.h>
12#include <xrpl/ledger/helpers/MPTokenHelpers.h>
13#include <xrpl/ledger/helpers/RippleStateHelpers.h>
14#include <xrpl/ledger/helpers/TokenHelpers.h>
15#include <xrpl/protocol/AccountID.h>
16#include <xrpl/protocol/Concepts.h>
17#include <xrpl/protocol/Feature.h>
18#include <xrpl/protocol/Indexes.h>
19#include <xrpl/protocol/Issue.h>
20#include <xrpl/protocol/LedgerFormats.h>
21#include <xrpl/protocol/MPTAmount.h>
22#include <xrpl/protocol/MPTIssue.h>
23#include <xrpl/protocol/Protocol.h>
24#include <xrpl/protocol/Rate.h>
25#include <xrpl/protocol/SField.h>
26#include <xrpl/protocol/STAmount.h>
27#include <xrpl/protocol/STLedgerEntry.h>
28#include <xrpl/protocol/STTx.h>
29#include <xrpl/protocol/TER.h>
30#include <xrpl/protocol/UintTypes.h>
31#include <xrpl/protocol/XRPAmount.h>
32#include <xrpl/tx/Transactor.h>
33#include <xrpl/tx/applySteps.h>
80 auto const amount = ctx.
tx[sfAmount];
94template <Val
idIssueType T>
103 if (amount.
native() || amount <= beast::kZero)
119 auto const amount = ctx.
tx[sfAmount];
120 if (amount.native() || amount.mpt() >
MPTAmount{kMaxMpTokenAmount} || amount <= beast::kZero)
137 amount.
asset().value());
143 if (amount <= beast::kZero)
148 if (!ctx.
tx[~sfCancelAfter] && !ctx.
tx[~sfFinishAfter])
153 if (ctx.
tx[~sfCancelAfter] && ctx.
tx[~sfFinishAfter] &&
154 ctx.
tx[sfCancelAfter] <= ctx.
tx[sfFinishAfter])
161 if (!ctx.
tx[~sfFinishAfter] && !ctx.
tx[~sfCondition])
164 if (
auto const cb = ctx.
tx[~sfCondition])
173 JLOG(ctx.
j.
debug()) <<
"Malformed condition during escrow creation: " << ec.
message();
181template <Val
idIssueType T>
200 if (issuer == account)
207 if (!sleIssuer->isFlag(lsfAllowTrustLineLocking))
215 STAmount const balance = (*sleRippleState)[sfBalance];
218 if (balance > beast::kZero && issuer < account)
222 if (balance < beast::kZero && issuer > account)
245 if (spendableAmount <= beast::kZero)
250 if (spendableAmount < amount)
254 if (!
canAdd(spendableAmount, amount))
270 if (issuer == account)
275 auto const sleIssuance = ctx.
view.
read(issuanceKey);
280 if (!sleIssuance->isFlag(lsfMPTCanEscrow))
285 if (sleIssuance->getAccountID(sfIssuer) != issuer)
326 if (spendableAmount <= beast::kZero)
331 if (spendableAmount < amount)
361 [&]<
typename T>(T
const&) {
364 amount.
asset().value());
371template <Val
idIssueType T>
390 if (issuer == sender)
410 if (issuer == sender)
413 auto const ter =
lockEscrowMPT(view, sender, amount, journal);
422 auto const closeTime =
ctx_.view().header().parentCloseTime;
424 if (
ctx_.tx[~sfCancelAfter] &&
after(closeTime,
ctx_.tx[sfCancelAfter]))
427 if (
ctx_.tx[~sfFinishAfter] &&
after(closeTime,
ctx_.tx[sfFinishAfter]))
437 auto const reserve =
ctx_.view().fees().accountReserve((*sle)[sfOwnerCount] + 1);
439 auto const balance = sle->getFieldAmount(sfBalance).xrp();
440 if (balance < reserve)
446 if (balance < reserve +
STAmount(amount).xrp())
455 if (sled->isFlag(lsfRequireDestTag) && !
ctx_.tx[~sfDestinationTag])
463 (*slep)[sfAmount] = amount;
465 (*slep)[~sfCondition] =
ctx_.tx[~sfCondition];
466 (*slep)[~sfSourceTag] =
ctx_.tx[~sfSourceTag];
467 (*slep)[sfDestination] =
ctx_.tx[sfDestination];
468 (*slep)[~sfCancelAfter] =
ctx_.tx[~sfCancelAfter];
469 (*slep)[~sfFinishAfter] =
ctx_.tx[~sfFinishAfter];
470 (*slep)[~sfDestinationTag] =
ctx_.tx[~sfDestinationTag];
472 if (
ctx_.view().rules().enabled(fixIncludeKeyletFields))
474 (*slep)[sfSequence] =
ctx_.tx.getSeqValue();
477 if (
ctx_.view().rules().enabled(featureTokenEscrow) && !
isXRP(amount))
481 (*slep)[sfTransferRate] = xferRate.value;
484 ctx_.view().insert(slep);
488 auto page =
ctx_.view().dirInsert(
492 (*slep)[sfOwnerNode] = *page;
503 (*slep)[sfDestinationNode] = *page;
516 (*slep)[sfIssuerNode] = *page;
522 (*sle)[sfBalance] = (*sle)[sfBalance] - amount;
527 [&]<
typename T>(T
const&) {
530 amount.
asset().value());
539 ctx_.view().update(sle);
A generic endpoint for log messages.
Writeable view to a ledger, for applying a transaction.
static bool checkExtraFeatures(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
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.
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
A currency issued by an account.
constexpr MPTID const & getMptID() const
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
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.
constexpr bool holds() const noexcept
constexpr TIss const & get() const
bool native() const noexcept
Asset const & asset() const
AccountID const & getIssuer() const
std::shared_ptr< STLedgerEntry const > const & const_ref
AccountID const accountID_
Class describing the consequences to the account of applying a transaction if the transaction consume...
static std::unique_ptr< Condition > deserialize(Slice s, std::error_code &ec)
Load a condition from its binary form.
Keylet mptokenIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet escrow(AccountID const &src, std::uint32_t seq) noexcept
An escrow entry.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool isXRP(AccountID const &c)
NotTEC escrowCreatePreflightHelper< Issue >(PreflightContext const &ctx)
TER lockEscrowMPT(ApplyView &view, AccountID const &uGrantorID, STAmount const &saAmount, beast::Journal j)
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to, WaiveMPTCanTransfer waive=WaiveMPTCanTransfer::No, std::uint8_t depth=0)
Check whether to may receive the given MPT from from.
TER escrowCreatePreclaimHelper< MPTIssue >(PreclaimContext const &ctx, AccountID const &account, AccountID const &dest, STAmount const &amount)
bool canAdd(STAmount const &amt1, STAmount const &amt2)
Safely checks if two STAmount values can be added without overflow, underflow, or precision loss.
TERSubset< CanCvtToNotTEC > NotTEC
TER escrowLockApplyHelper< MPTIssue >(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Rate const kParityRate
A transfer rate signifying a 1:1 exchange.
TER directSendNoFee(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Calls static directSendNoFeeIOU if saAmount represents Issue.
static TER escrowLockApplyHelper(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, std::uint8_t depth=0)
TER escrowCreatePreclaimHelper< Issue >(PreclaimContext const &ctx, AccountID const &account, AccountID const &dest, STAmount const &amount)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
static NotTEC escrowCreatePreflightHelper(PreflightContext const &ctx)
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
static TER escrowCreatePreclaimHelper(PreclaimContext const &ctx, AccountID const &account, AccountID const &dest, STAmount const &amount)
@ tecINSUFFICIENT_RESERVE
TER escrowLockApplyHelper< Issue >(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
NotTEC escrowCreatePreflightHelper< MPTIssue >(PreflightContext const &ctx)
bool isPseudoAccount(SLE::const_pointer sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
A pair of SHAMap key and LedgerEntryType.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.