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/Protocol.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/STAmount.h>
22#include <xrpl/protocol/STLedgerEntry.h>
23#include <xrpl/protocol/STNumber.h>
24#include <xrpl/protocol/STTakesAsset.h>
25#include <xrpl/protocol/STTx.h>
26#include <xrpl/protocol/TER.h>
27#include <xrpl/protocol/XRPAmount.h>
28#include <xrpl/tx/Transactor.h>
41 JLOG(ctx.
j.
debug()) <<
"VaultClawback: zero/empty vault ID.";
45 auto const amount = ctx.
tx[~sfAmount];
53 if (
isXRP(amount->asset()))
55 JLOG(ctx.
j.
debug()) <<
"VaultClawback: cannot clawback XRP.";
73 if (account == vault->at(sfOwner))
86 Asset const vaultAsset = vault->at(sfAsset);
87 auto const account = ctx.
tx[sfAccount];
88 auto const holder = ctx.
tx[sfHolder];
89 auto const maybeAmount = ctx.
tx[~sfAmount];
90 auto const mptIssuanceID = vault->at(sfShareMPTID);
92 if (!sleShareIssuance)
95 JLOG(ctx.
j.
error()) <<
"VaultClawback: missing issuance of vault shares.";
104 JLOG(ctx.
j.
debug()) <<
"VaultClawback: holder account does not exist.";
115 JLOG(ctx.
j.
debug()) <<
"VaultClawback: holder is a pseudo-account.";
122 if (!maybeAmount && !vaultAsset.
native() && vaultAsset.
getIssuer() == vault->at(sfOwner))
124 JLOG(ctx.
j.
debug()) <<
"VaultClawback: must specify amount when issuer is owner.";
134 if (amount.asset() == share)
137 if (account != vault->at(sfOwner))
139 JLOG(ctx.
j.
debug()) <<
"VaultClawback: only vault owner can clawback shares.";
143 auto const assetsTotal = vault->at(sfAssetsTotal);
144 auto const assetsAvailable = vault->at(sfAssetsAvailable);
145 auto const sharesTotal = sleShareIssuance->at(sfOutstandingAmount);
148 if (sharesTotal == 0 || (assetsTotal != 0 || assetsAvailable != 0))
150 JLOG(ctx.
j.
debug()) <<
"VaultClawback: vault owner can clawback shares only"
151 " when vault has no assets.";
167 if (amount != sharesHeld)
169 JLOG(ctx.
j.
debug()) <<
"VaultClawback: vault owner must clawback all "
179 if (amount.asset() == vaultAsset)
184 JLOG(ctx.
j.
debug()) <<
"VaultClawback: cannot clawback XRP.";
191 JLOG(ctx.
j.
debug()) <<
"VaultClawback: only asset issuer can clawback asset.";
196 if (account == holder)
198 JLOG(ctx.
j.
debug()) <<
"VaultClawback: issuer cannot be the holder.";
202 return vaultAsset.
visit(
205 if (mptIssue ==
nullptr)
208 if (!mptIssue->isFlag(lsfMPTCanClawback))
210 JLOG(ctx.
j.
debug()) <<
"VaultClawback: cannot clawback "
222 JLOG(ctx.
j.
error()) <<
"VaultClawback: missing submitter account.";
227 if (!issuerSle->isFlag(lsfAllowTrustLineClawback) || issuerSle->isFlag(lsfNoFreeze))
229 JLOG(ctx.
j.
debug()) <<
"VaultClawback: cannot clawback "
242std::expected<std::pair<STAmount, STAmount>,
TER>
249 bool const fix340Enabled =
ctx_.view().rules().enabled(fixCleanup3_4_0);
254 JLOG(
j_.error()) <<
"VaultClawback: asset mismatch in clawback.";
259 auto const assetsAvailable = vault->at(sfAssetsAvailable);
260 auto const mptIssuanceID = *vault->at(sfShareMPTID);
261 MPTIssue const share{mptIssuanceID};
286 auto const waiveUnrealizedLoss =
299 ?
STAmount{share, sleShareIssuance->at(sfOutstandingAmount)}
308 vault, sleShareIssuance, sharesDestroyed, waiveUnrealizedLoss);
312 assetsRecovered = *maybeAssets;
322 vault, sleShareIssuance,
clawbackAmount, truncate, waiveUnrealizedLoss);
325 sharesDestroyed = *maybeShares;
328 vault, sleShareIssuance, sharesDestroyed, waiveUnrealizedLoss);
331 assetsRecovered = *maybeAssets;
334 if (assetsRecovered > *assetsAvailable)
336 assetsRecovered = *assetsAvailable;
343 waiveUnrealizedLoss);
346 sharesDestroyed = *maybeShares;
350 vault, sleShareIssuance, sharesDestroyed, waiveUnrealizedLoss);
353 assetsRecovered = *maybeAssets;
356 if (assetsRecovered > *assetsAvailable)
359 JLOG(
j_.error()) <<
"VaultClawback: invalid rounding of shares.";
369 sharesDestroyed ==
STAmount{share, sleShareIssuance->at(sfOutstandingAmount)} &&
373 <<
"VaultClawback: cannot burn all shares while AssetsDeployed is non-zero";
389 assetsRecovered = *maybeClamped;
397 <<
"VaultClawback: overflow error with"
398 <<
" scale=" << (int)vault->at(sfScale).value()
399 <<
", assetsTotal=" << vault->at(sfAssetsTotal).value()
400 <<
", sharesTotal=" << sleShareIssuance->at(sfOutstandingAmount)
413 auto const& tx =
ctx_.tx;
418 auto const mptIssuanceID = *vault->at(sfShareMPTID);
423 JLOG(
j_.error()) <<
"VaultClawback: missing issuance of vault shares.";
427 MPTIssue const share{mptIssuanceID};
429 Asset const vaultAsset = vault->at(sfAsset);
434 STAmount assetsRecovered = {vault->at(sfAsset)};
444 XRPL_ASSERT(amount.
asset() == vaultAsset,
"xrpl::VaultClawback::doApply : matching asset");
446 auto const clawbackParts =
assetsToClawback(vault, sleIssuance, holder, amount);
448 return clawbackParts.error();
450 assetsRecovered = clawbackParts->first;
451 sharesDestroyed = clawbackParts->second;
460 if (
view().rules().enabled(fixCleanup3_4_0))
473 auto const& vaultAccount = vault->at(sfAccount);
483 if (holder != vault->at(sfOwner))
490 <<
"VaultClawback: removed empty MPToken for vault shares"
498 <<
"VaultClawback: failed to remove MPToken for vault shares"
520 assetsRecovered.
asset(),
526 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 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.
Asset const & asset() const
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.
std::expected< STAmount, TER > clampVaultOutflow(SLE::ConstRef vault, STAmount const &delta)
Outflow clamp dispatcher for VaultWithdraw and VaultClawback: rounds a negative delta to the scale ap...
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::string transToken(TER code)
TER adjustVaultCash(SLE::Ref vault, STAmount const &delta, beast::Journal j)
Apply a signed cash movement to a Vault of any version.
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.
TER checkDebitNotDust(SLE::ConstRef vault, Number const &amount, beast::Journal j, std::string_view name)
Reject a cash outflow that is dust (see debitIsNonZeroDust) or whose check overflows Number,...
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.
VaultVersion getVaultVersion(SLE::ConstRef vault)
Resolves a Vault's LEVersion.
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.