3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/ledger/ApplyView.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/ledger/helpers/AccountRootHelpers.h>
10#include <xrpl/ledger/helpers/MPTokenHelpers.h>
11#include <xrpl/ledger/helpers/RippleStateHelpers.h>
12#include <xrpl/ledger/helpers/SponsorHelpers.h>
13#include <xrpl/ledger/helpers/TokenHelpers.h>
14#include <xrpl/protocol/AccountID.h>
15#include <xrpl/protocol/Concepts.h>
16#include <xrpl/protocol/Feature.h>
17#include <xrpl/protocol/Fees.h>
18#include <xrpl/protocol/Indexes.h>
19#include <xrpl/protocol/Issue.h>
20#include <xrpl/protocol/Keylet.h>
21#include <xrpl/protocol/LedgerFormats.h>
22#include <xrpl/protocol/MPTAmount.h>
23#include <xrpl/protocol/MPTIssue.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/TER.h>
29#include <xrpl/protocol/UintTypes.h>
46template <Val
idIssueType T>
64 auto const& issue = amount.
get<
Issue>();
67 if (issuer == account)
74 if (!sleIssuer->isFlag(lsfAllowTrustLineLocking))
82 STAmount const balance = (*sleRippleState)[sfBalance];
89 if (balance < beast::kZero && issuer > account)
117 if (spendableAmount < amount)
121 if (!
canAdd(spendableAmount, amount))
138 if (issuer == account)
143 auto const sleIssuance = view.
read(issuanceKey);
148 if (!sleIssuance->isFlag(lsfMPTCanEscrow))
153 if (sleIssuance->getAccountID(sfIssuer) != issuer)
173 if (
isFrozen(view, account, *sleIssuance))
177 if (
isFrozen(view, dest, *sleIssuance))
198 if (spendableAmount < amount)
204template <Val
idIssueType T>
223 if (issuer == sender)
243 if (issuer == sender)
246 auto const ter =
lockEscrowMPT(view, sender, amount, journal);
252template <Val
idIssueType T>
270 if (issuer == account)
295 if (issuer == account)
300 auto const sleIssuance = view.
read(issuanceKey);
320template <Val
idIssueType T>
348 bool const senderIssuer = issuer == sender;
349 bool const receiverIssuer = issuer == receiver;
357 auto const& issue = amount.
get<
Issue>();
359 bool const recvLow = issuer > receiver;
361 if (!ctx.
view.
exists(trustLineKey) && createAsset)
366 return sponsorSle.error();
373 {.ownerCountDelta = 1},
378 JLOG(journal.
trace()) <<
"Trust line does not exist. "
379 "Insufficient reserve to create line.";
383 Currency const currency = issue.currency;
395 !sleDest->isFlag(lsfDefaultRipple),
399 Issue(currency, receiver),
412 if (!ctx.
view.
exists(trustLineKey) && !receiverIssuer)
417 if (xferRate < lockedRate)
418 lockedRate = xferRate;
429 auto finalAmt = amount;
430 if ((!senderIssuer && !receiverIssuer) && lockedRate !=
kParityRate)
436 finalAmt = amount.
value() - xferFee;
443 auto const sleRippleState = ctx.
view.
peek(trustLineKey);
450 sleRippleState->getFieldAmount(recvLow ? sfLowLimit : sfHighLimit);
452 STAmount lineBalance = sleRippleState->getFieldAmount(sfBalance);
459 lineBalance += finalAmt;
462 if (lineLimit < lineBalance)
490 bool const senderIssuer = issuer == sender;
491 bool const receiverIssuer = issuer == receiver;
493 auto const mptID = amount.
get<
MPTIssue>().getMptID();
496 if (!ctx.
view.
exists(mptKeylet) && createAsset && !receiverIssuer)
500 return sponsorSle.error();
503 ctx, sleDest, xrpBalance, *sponsorSle, {.ownerCountDelta = 1}, journal);
517 if (!ctx.
view.
exists(mptKeylet) && !receiverIssuer)
522 if (xferRate < lockedRate)
523 lockedRate = xferRate;
534 auto finalAmt = amount;
535 if ((!senderIssuer && !receiverIssuer) && lockedRate !=
kParityRate)
541 "xrpl::escrowUnlockApplyHelper<MPTIssue> : lockedRate is at least parity");
544 auto const delivered =
554 finalAmt = amount.
value() - xferFee;
597 static auto constexpr kBytecodeReserveIncrement = 500;
602 auto const size = finishFunction->size();
608 UNREACHABLE(
"xrpl::calculateAdditionalReserve : empty Bytecode");
613 return static_cast<int32_t
>((size + kBytecodeReserveIncrement - 1) / kBytecodeReserveIncrement);
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Writeable view to a ledger, for applying a transaction.
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
constexpr value_type const & value() const
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
Asset const & asset() const
AccountID const & getIssuer() const
STAmount const & value() const noexcept
std::shared_ptr< STLedgerEntry > const & Ref
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
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.
int32_t calculateAdditionalReserve(T const &finishFunction)
Owner count an escrow costs.
TER escrowUnlockPreclaimHelper< MPTIssue >(ReadView const &view, AccountID const &account, STAmount const &amount, bool checkFreeze)
TER escrowUnlockPreclaimHelper< Issue >(ReadView const &view, AccountID const &account, STAmount const &amount, bool checkFreeze)
TER escrowUnlockApplyHelper(ApplyViewContext ctx, Rate lockedRate, SLE::Ref sleDest, XRPAmount xrpBalance, STAmount const &amount, AccountID const &issuer, AccountID const &sender, AccountID const &receiver, bool createAsset, beast::Journal journal)
bool isBytecodeExecutionDisabled(Fees const &fees)
TER lockEscrowMPT(ApplyView &view, AccountID const &uGrantorID, STAmount const &saAmount, beast::Journal j)
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
TER escrowLockApplyHelper(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
TER checkReserve(ApplyViewContext ctx, SLE::ConstRef accSle, XRPAmount accBalance, SLE::ConstRef sponsorSle, Adjustment adj, beast::Journal j, TER insufReserveCode=tecINSUFFICIENT_RESERVE)
Check if an account has sufficient reserve.
TER trustCreate(ApplyView &view, bool const bSrcHigh, AccountID const &uSrcAccountID, AccountID const &uDstAccountID, UInt256 const &uIndex, SLE::Ref sleAccount, bool const bAuth, bool const bNoRipple, bool const bFreeze, bool bDeepFreeze, STAmount const &saBalance, STAmount const &saLimit, std::uint32_t uQualityIn, std::uint32_t uQualityOut, SLE::Ref sponsorSle, beast::Journal j)
Create a trust line.
TER unlockEscrowMPT(ApplyView &view, AccountID const &uGrantorID, AccountID const &uGranteeID, STAmount const &netAmount, STAmount const &grossAmount, beast::Journal j)
std::expected< SLE::pointer, TER > getEffectiveTxReserveSponsor(ApplyViewContext ctx, SLE::ConstRef accountSle)
The transaction's reserve sponsor for the given account, if applicable.
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.
void increaseOwnerCount(ApplyView &view, SLE::Ref accountSle, SLE::Ref sponsorSle, std::uint32_t count, beast::Journal j)
Increase owner-count fields when the caller supplies the sponsor.
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
bool canAdd(STAmount const &amt1, STAmount const &amt2)
Safely checks if two STAmount values can be added without overflow, underflow, or precision loss.
TER escrowLockApplyHelper< MPTIssue >(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
TER escrowUnlockPreclaimHelper(ReadView const &view, AccountID const &account, STAmount const &amount, bool checkFreeze=true)
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
Rate const kParityRate
A transfer rate signifying a 1:1 exchange.
TER checkFreeze(ReadView const &view, AccountID const &src, AccountID const &dst, Currency const ¤cy)
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.
TER escrowLockPreclaimHelper< MPTIssue >(ReadView const &view, AccountID const &account, AccountID const &dest, STAmount const &amount, beast::Journal j)
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, std::uint8_t depth=0)
Returns true if account cannot send or receive tokens of mptIssue because a freeze applies.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
AccountID const & noAccount()
A placeholder for empty accounts.
TER escrowLockPreclaimHelper< Issue >(ReadView const &view, AccountID const &account, AccountID const &dest, STAmount const &amount, beast::Journal j)
TER escrowLockPreclaimHelper(ReadView const &view, AccountID const &account, AccountID const &dest, STAmount const &amount, beast::Journal j)
Validate that account may lock amount of a token for later delivery to dest.
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.
@ tecNO_LINE_INSUF_RESERVE
TER escrowLockApplyHelper< Issue >(ApplyView &view, AccountID const &issuer, AccountID const &sender, STAmount const &amount, beast::Journal journal)
TER createMPToken(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, SLE::Ref sponsorSle, std::uint32_t const flags)
bool isBytecodeUploadDisabled(Fees const &fees)
Smart escrow kill switches, driven by the voted FeeSettings.
STAmount divideRound(STAmount const &amount, Rate const &rate, bool roundUp)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
TER escrowUnlockApplyHelper< Issue >(ApplyViewContext ctx, Rate lockedRate, SLE::Ref sleDest, XRPAmount xrpBalance, STAmount const &amount, AccountID const &issuer, AccountID const &sender, AccountID const &receiver, bool createAsset, beast::Journal journal)
TER escrowUnlockApplyHelper< MPTIssue >(ApplyViewContext ctx, Rate lockedRate, SLE::Ref sleDest, XRPAmount xrpBalance, STAmount const &amount, AccountID const &issuer, AccountID const &sender, AccountID const &receiver, bool createAsset, beast::Journal journal)
Bundles the mutable ledger view and the transaction being applied.
Reflects the fee settings for a particular ledger.
std::uint32_t gasLimit
Gas limit for Feature Extensions (instructions).
std::uint32_t bytecodeSizeLimit
Bytecode size limit for Feature Extensions (bytes).
A pair of SHAMap key and LedgerEntryType.
Represents a transfer rate.