From 840a41f880811602e2c86931be0bdce15b75869a Mon Sep 17 00:00:00 2001 From: Vito <5780819+Tapanito@users.noreply.github.com> Date: Thu, 13 Aug 2026 14:08:14 +0200 Subject: [PATCH] refactor: clean up VaultHelpers --- src/libxrpl/ledger/helpers/VaultHelpers.cpp | 90 +++++++++------------ 1 file changed, 40 insertions(+), 50 deletions(-) diff --git a/src/libxrpl/ledger/helpers/VaultHelpers.cpp b/src/libxrpl/ledger/helpers/VaultHelpers.cpp index c16aaa821a..b75fc9ebb5 100644 --- a/src/libxrpl/ledger/helpers/VaultHelpers.cpp +++ b/src/libxrpl/ledger/helpers/VaultHelpers.cpp @@ -27,6 +27,45 @@ #include namespace xrpl { +namespace { + +// Applies a full-removal mutation to the Vault's ledger fields: both +// callers (clawbackVaultAssets and removeVaultAssets) apply `amount` to +// sfAssetsTotal and sfAssetsAvailable equally (unlike +// addVaultAssets/moveVaultAssets, a full removal always shrinks both fields +// by the same amount). On a final removal, both fields are hard-reset to +// exactly zero rather than computed via subtraction: see FinalRemoval's +// doc comment for why an arithmetic subtraction cannot be trusted to land +// on exactly zero here. +void +applyRemoveVaultAssets( + ApplyView& view, + SLE::ref vault, + STAmount const& amount, + FinalRemoval finalRemoval) +{ + if (finalRemoval == FinalRemoval::Yes) + { + vault->at(sfAssetsTotal) = 0; + vault->at(sfAssetsAvailable) = 0; + } + else + { + vault->at(sfAssetsTotal) -= amount; + vault->at(sfAssetsAvailable) -= amount; + } + view.update(vault); +} + +[[nodiscard]] VaultKind +decodeVaultKind(std::optional vaultKind) +{ + if (vaultKind && *vaultKind == std::to_underlying(VaultKind::ClosedEnded)) + return VaultKind::ClosedEnded; + return VaultKind::OpenEnded; +} + +} // namespace [[nodiscard]] std::optional assetsToSharesDeposit(SLE::const_ref vault, SLE::const_ref issuance, STAmount const& assets) @@ -205,46 +244,9 @@ addVaultAssets( vault->at(sfAssetsAvailable) += amount; view.update(vault); - if (auto const ter = - accountSend(view, sender, vault->at(sfAccount), amount, j, {}, WaiveTransferFee::Yes); - !isTesSuccess(ter)) - return ter; - - return tesSUCCESS; + return accountSend(view, sender, vault->at(sfAccount), amount, j, {}, WaiveTransferFee::Yes); } -namespace { - -// Applies a full-removal mutation to the Vault's ledger fields: both -// callers (clawbackVaultAssets and removeVaultAssets) apply `amount` to -// sfAssetsTotal and sfAssetsAvailable equally (unlike -// addVaultAssets/moveVaultAssets, a full removal always shrinks both fields -// by the same amount). On a final removal, both fields are hard-reset to -// exactly zero rather than computed via subtraction: see FinalRemoval's -// doc comment for why an arithmetic subtraction cannot be trusted to land -// on exactly zero here. -void -applyRemoveVaultAssets( - ApplyView& view, - SLE::ref vault, - STAmount const& amount, - FinalRemoval finalRemoval) -{ - if (finalRemoval == FinalRemoval::Yes) - { - vault->at(sfAssetsTotal) = 0; - vault->at(sfAssetsAvailable) = 0; - } - else - { - vault->at(sfAssetsTotal) -= amount; - vault->at(sfAssetsAvailable) -= amount; - } - view.update(vault); -} - -} // namespace - [[nodiscard]] TER clawbackVaultAssets( ApplyView& view, @@ -356,18 +358,6 @@ moveVaultAssets( view, vault->at(sfAccount), asset, recipients, j, WaiveTransferFee::Yes); } -namespace { - -[[nodiscard]] VaultKind -decodeVaultKind(std::optional vaultKind) -{ - if (vaultKind && *vaultKind == std::to_underlying(VaultKind::ClosedEnded)) - return VaultKind::ClosedEnded; - return VaultKind::OpenEnded; -} - -} // namespace - [[nodiscard]] VaultKind getVaultKind(SLE::const_ref vault) {