1#include <xrpl/tx/transactors/vault/VaultClawback.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/core/ServiceRegistry.h>
9#include <xrpl/ledger/helpers/AccountRootHelpers.h>
10#include <xrpl/ledger/helpers/TokenHelpers.h>
11#include <xrpl/ledger/helpers/VaultHelpers.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/Asset.h>
14#include <xrpl/protocol/Feature.h>
15#include <xrpl/protocol/Indexes.h>
16#include <xrpl/protocol/Issue.h>
17#include <xrpl/protocol/LedgerFormats.h>
18#include <xrpl/protocol/MPTIssue.h>
19#include <xrpl/protocol/SField.h>
20#include <xrpl/protocol/STAmount.h>
21#include <xrpl/protocol/STLedgerEntry.h>
22#include <xrpl/protocol/STNumber.h>
23#include <xrpl/protocol/STTakesAsset.h>
24#include <xrpl/protocol/STTx.h>
25#include <xrpl/protocol/TER.h>
26#include <xrpl/protocol/XRPAmount.h>
27#include <xrpl/tx/Transactor.h>
40 JLOG(ctx.
j.
debug()) <<
"VaultClawback: zero/empty vault ID.";
44 auto const amount = ctx.
tx[~sfAmount];
52 if (
isXRP(amount->asset()))
54 JLOG(ctx.
j.
debug()) <<
"VaultClawback: cannot clawback XRP.";
72 if (account == vault->at(sfOwner))
85 Asset const vaultAsset = vault->at(sfAsset);
86 auto const account = ctx.
tx[sfAccount];
87 auto const holder = ctx.
tx[sfHolder];
88 auto const maybeAmount = ctx.
tx[~sfAmount];
89 auto const mptIssuanceID = vault->at(sfShareMPTID);
91 if (!sleShareIssuance)
94 JLOG(ctx.
j.
error()) <<
"VaultClawback: missing issuance of vault shares.";
106 JLOG(ctx.
j.
debug()) <<
"VaultClawback: holder is a pseudo-account.";
113 if (!maybeAmount && !vaultAsset.
native() && vaultAsset.
getIssuer() == vault->at(sfOwner))
115 JLOG(ctx.
j.
debug()) <<
"VaultClawback: must specify amount when issuer is owner.";
125 if (amount.asset() == share)
128 if (account != vault->at(sfOwner))
130 JLOG(ctx.
j.
debug()) <<
"VaultClawback: only vault owner can clawback shares.";
134 auto const assetsTotal = vault->at(sfAssetsTotal);
135 auto const assetsAvailable = vault->at(sfAssetsAvailable);
136 auto const sharesTotal = sleShareIssuance->at(sfOutstandingAmount);
139 if (sharesTotal == 0 || (assetsTotal != 0 || assetsAvailable != 0))
141 JLOG(ctx.
j.
debug()) <<
"VaultClawback: vault owner can clawback shares only"
142 " when vault has no assets.";
158 if (amount != sharesHeld)
160 JLOG(ctx.
j.
debug()) <<
"VaultClawback: vault owner must clawback all "
170 if (amount.asset() == vaultAsset)
175 JLOG(ctx.
j.
debug()) <<
"VaultClawback: cannot clawback XRP.";
182 JLOG(ctx.
j.
debug()) <<
"VaultClawback: only asset issuer can clawback asset.";
187 if (account == holder)
189 JLOG(ctx.
j.
debug()) <<
"VaultClawback: issuer cannot be the holder.";
193 return vaultAsset.
visit(
196 if (mptIssue ==
nullptr)
199 if (!mptIssue->isFlag(lsfMPTCanClawback))
201 JLOG(ctx.
j.
debug()) <<
"VaultClawback: cannot clawback "
213 JLOG(ctx.
j.
error()) <<
"VaultClawback: missing submitter account.";
218 if (!issuerSle->isFlag(lsfAllowTrustLineClawback) || issuerSle->isFlag(lsfNoFreeze))
220 JLOG(ctx.
j.
debug()) <<
"VaultClawback: cannot clawback "
233std::expected<std::pair<STAmount, STAmount>,
TER>
240 bool const fix340Enabled =
ctx_.view().rules().enabled(fixCleanup3_4_0);
245 JLOG(
j_.error()) <<
"VaultClawback: asset mismatch in clawback.";
250 auto const assetsAvailable = vault->at(sfAssetsAvailable);
251 auto const mptIssuanceID = *vault->at(sfShareMPTID);
252 MPTIssue const share{mptIssuanceID};
277 auto const waiveUnrealizedLoss =
291 ?
STAmount{share, sleShareIssuance->at(sfOutstandingAmount)}
300 vault, sleShareIssuance, sharesDestroyed, waiveUnrealizedLoss);
304 assetsRecovered = *maybeAssets;
315 vault, sleShareIssuance,
clawbackAmount, truncate, waiveUnrealizedLoss);
318 sharesDestroyed = *maybeShares;
321 vault, sleShareIssuance, sharesDestroyed, waiveUnrealizedLoss);
324 assetsRecovered = *maybeAssets;
328 if (assetsRecovered > *assetsAvailable)
330 assetsRecovered = *assetsAvailable;
337 waiveUnrealizedLoss);
340 sharesDestroyed = *maybeShares;
344 vault, sleShareIssuance, sharesDestroyed, waiveUnrealizedLoss);
347 assetsRecovered = *maybeAssets;
350 if (assetsRecovered > *assetsAvailable)
353 JLOG(
j_.error()) <<
"VaultClawback: invalid rounding of shares.";
363 if (
ctx_.view().rules().enabled(fixCleanup3_4_0) && assetsRecovered >
beast::kZero)
368 assetsRecovered = *maybeClamped;
376 <<
"VaultClawback: overflow error with"
377 <<
" scale=" << (int)vault->at(sfScale).value()
378 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
379 <<
", sharesTotal=" << sleShareIssuance->at(sfOutstandingAmount)
392 auto const& tx =
ctx_.tx;
397 auto const mptIssuanceID = *vault->at(sfShareMPTID);
402 JLOG(
j_.error()) <<
"VaultClawback: missing issuance of vault shares.";
406 MPTIssue const share{mptIssuanceID};
408 Asset const vaultAsset = vault->at(sfAsset);
411 auto assetsAvailable = vault->at(sfAssetsAvailable);
412 auto assetsTotal = vault->at(sfAssetsTotal);
416 STAmount assetsRecovered = {vault->at(sfAsset)};
426 XRPL_ASSERT(amount.
asset() == vaultAsset,
"xrpl::VaultClawback::doApply : matching asset");
428 auto const clawbackParts =
assetsToClawback(vault, sleIssuance, holder, amount);
430 return clawbackParts.error();
432 assetsRecovered = clawbackParts->first;
433 sharesDestroyed = clawbackParts->second;
442 if (
view().rules().enabled(fixCleanup3_4_0))
452 <<
"VaultClawback: clawback amount too small to change stored vault"
464 <<
"VaultClawback: overflow error with"
465 <<
" scale=" << (int)vault->at(sfScale).value()
466 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
467 <<
", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
468 <<
", amount=" << amount.
value();
476 assetsTotal -= assetsRecovered;
477 assetsAvailable -= assetsRecovered;
480 auto const& vaultAccount = vault->at(sfAccount);
490 if (holder != vault->at(sfOwner))
497 <<
"VaultClawback: removed empty MPToken for vault shares"
505 <<
"VaultClawback: failed to remove MPToken for vault shares"
527 assetsRecovered.
asset(),
533 JLOG(
j_.error()) <<
"VaultClawback: negative balance of vault assets.";
A generic endpoint for log messages.
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 auto visit(Visitors &&... visitors) const -> decltype(auto)
constexpr bool native() const
AccountID const & getIssuer() const
A currency issued by an account.
constexpr MPTID const & getMptID() const
Number is a floating point type that can represent a wide range of values.
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.
Asset const & asset() const
STAmount const & value() const noexcept
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::shared_ptr< STLedgerEntry > const & Ref
AccountID const accountID_
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 NotTEC preflight(PreflightContext const &ctx)
std::expected< std::pair< STAmount, STAmount >, TER > assetsToClawback(SLE::Ref vault, SLE::ConstRef sleShareIssuance, AccountID const &holder, STAmount const &clawbackAmount)
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::optional< STAmount > sharesToAssetsWithdraw(SLE::ConstRef vault, SLE::ConstRef issuance, STAmount const &shares, WaiveUnrealizedLoss waive=WaiveUnrealizedLoss::No)
From the perspective of a vault, return the number of assets to give the depositor when they redeem a...
bool isSoleShareholder(ReadView const &view, AccountID const &account, SLE::ConstRef issuance)
Returns true iff account holds all of the vault's outstanding shares — i.e.
bool debitIsNonZeroDust(Asset const &asset, Number const &total, Number const &amount)
Returns true if debiting amount from total (the current value of a vault's sfAssetsTotal or sfAssetsA...
std::optional< STAmount > assetsToSharesWithdraw(SLE::ConstRef vault, SLE::ConstRef issuance, STAmount const &assets, TruncateShares truncate=TruncateShares::No, WaiveUnrealizedLoss waive=WaiveUnrealizedLoss::No)
From the perspective of a vault, return the number of shares to demand from the depositor when they a...
STAmount clawbackAmount(SLE::ConstRef vault, std::optional< STAmount > const &maybeAmount, AccountID const &account)
bool isXRP(AccountID const &c)
TER removeEmptyHolding(ApplyViewContext ctx, AccountID const &accountID, MPTIssue const &mptIssue, beast::Journal journal)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::expected< STAmount, TER > clampToAssetsTotalScale(SLE::ConstRef vault, STAmount const &delta)
Adjusts a requested asset change (delta) to match the decimal scale of the updated total vault assets...
std::string transToken(TER code)
std::string to_string(BaseUInt< Bits, Tag > const &a)
TERSubset< CanCvtToNotTEC > NotTEC
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.
bool isPseudoAccount(SLE::const_pointer sleAcct)
Returns true if and only if sleAcct is a pseudo-account of any kind (i.e.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.