Files
rippled/include/xrpl/ledger/helpers/VaultHelpers.h
2026-07-13 10:40:40 +00:00

111 lines
4.0 KiB
C++

#pragma once
#include <xrpl/ledger/ReadView.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/STLedgerEntry.h>
#include <optional>
namespace xrpl {
/**
* From the perspective of a vault, return the number of shares to give
* depositor when they offer a fixed amount of assets. Note, since shares are
* MPT, this number is integral and always truncated in this calculation.
*
* @param vault The vault SLE.
* @param issuance The MPTokenIssuance SLE for the vault's shares.
* @param assets The amount of assets to convert.
*
* @return The number of shares, or nullopt on error.
*/
[[nodiscard]] std::optional<STAmount>
assetsToSharesDeposit(SLE::const_ref vault, SLE::const_ref issuance, STAmount const& assets);
/**
* From the perspective of a vault, return the number of assets to take from
* depositor when they receive a fixed amount of shares. Note, since shares are
* MPT, they are always an integral number.
*
* @param vault The vault SLE.
* @param issuance The MPTokenIssuance SLE for the vault's shares.
* @param shares The amount of shares to convert.
*
* @return The number of assets, or nullopt on error.
*/
[[nodiscard]] std::optional<STAmount>
sharesToAssetsDeposit(SLE::const_ref vault, SLE::const_ref issuance, STAmount const& shares);
/**
* Controls whether to truncate shares instead of rounding.
*/
enum class TruncateShares : bool { No = false, Yes = true };
/**
* Controls whether the withdraw conversion helpers
* (assetsToSharesWithdraw and sharesToAssetsWithdraw) subtract
* sfLossUnrealized from sfAssetsTotal before computing the exchange rate.
* The default (No) applies the standard discounted rate; Yes is used when
* the redeemer is the sole remaining shareholder.
*/
enum class WaiveUnrealizedLoss : bool { No = false, Yes = true };
/**
* From the perspective of a vault, return the number of shares to demand from
* the depositor when they ask to withdraw a fixed amount of assets. Since
* shares are MPT this number is integral, and it will be rounded to nearest
* unless explicitly requested to be truncated instead.
*
* @param vault The vault SLE.
* @param issuance The MPTokenIssuance SLE for the vault's shares.
* @param assets The amount of assets to convert.
* @param truncate Whether to truncate instead of rounding.
* @param waive Whether to waive the unrealized-loss discount when computing
* the exchange rate.
*
* @return The number of shares, or nullopt on error.
*/
[[nodiscard]] std::optional<STAmount>
assetsToSharesWithdraw(
SLE::const_ref vault,
SLE::const_ref issuance,
STAmount const& assets,
TruncateShares truncate = TruncateShares::No,
WaiveUnrealizedLoss waive = WaiveUnrealizedLoss::No);
/**
* From the perspective of a vault, return the number of assets to give the
* depositor when they redeem a fixed amount of shares. Note, since shares are
* MPT, they are always an integral number.
*
* @param vault The vault SLE.
* @param issuance The MPTokenIssuance SLE for the vault's shares.
* @param shares The amount of shares to convert.
* @param waive Whether to waive (i.e. not subtract) the vault's unrealized
* loss when computing the exchange rate.
*
* @return The number of assets, or nullopt on error.
*/
[[nodiscard]] std::optional<STAmount>
sharesToAssetsWithdraw(
SLE::const_ref vault,
SLE::const_ref issuance,
STAmount const& shares,
WaiveUnrealizedLoss waive = WaiveUnrealizedLoss::No);
/**
* Returns true iff `account` holds all of the vault's outstanding shares —
* i.e. is the sole remaining shareholder. Returns false if the account
* holds no shares or fewer than the total outstanding.
*
* @param view The ledger view.
* @param account The candidate sole shareholder.
* @param issuance The MPTokenIssuance SLE for the vault's shares; provides
* both the share MPTID and the outstanding-amount total.
*/
[[nodiscard]] bool
isSoleShareholder(ReadView const& view, AccountID const& account, SLE::const_ref issuance);
} // namespace xrpl