1#include <xrpl/ledger/helpers/VaultHelpers.h>
3#include <xrpl/basics/Number.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/ledger/ReadView.h>
6#include <xrpl/ledger/View.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/LedgerFormats.h>
10#include <xrpl/protocol/Protocol.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/STAmount.h>
13#include <xrpl/protocol/STLedgerEntry.h>
14#include <xrpl/protocol/STNumber.h>
15#include <xrpl/protocol/STTx.h>
23[[nodiscard]] std::optional<STAmount>
26 XRPL_ASSERT(!assets.
negative(),
"xrpl::assetsToSharesDeposit : non-negative assets");
28 assets.
asset() == vault->at(sfAsset),
29 "xrpl::assetsToSharesDeposit : assets and vault match");
30 if (assets.
negative() || assets.
asset() != vault->at(sfAsset))
33 Number const assetTotal = vault->at(sfAssetsTotal);
34 STAmount shares{vault->at(sfShareMPTID)};
42 Number const shareTotal = issuance->at(sfOutstandingAmount);
43 shares = ((shareTotal * assets) / assetTotal).truncate();
50 XRPL_ASSERT(!shares.
negative(),
"xrpl::sharesToAssetsDeposit : non-negative shares");
52 shares.
asset() == vault->at(sfShareMPTID),
53 "xrpl::sharesToAssetsDeposit : shares and vault match");
54 if (shares.
negative() || shares.
asset() != vault->at(sfShareMPTID))
57 Number const assetTotal = vault->at(sfAssetsTotal);
65 Number const shareTotal = issuance->at(sfOutstandingAmount);
66 assets = (assetTotal * shares) / shareTotal;
78 XRPL_ASSERT(!assets.
negative(),
"xrpl::assetsToSharesWithdraw : non-negative assets");
80 assets.
asset() == vault->at(sfAsset),
81 "xrpl::assetsToSharesWithdraw : assets and vault match");
82 if (assets.
negative() || assets.
asset() != vault->at(sfAsset))
85 Number assetTotal = vault->at(sfAssetsTotal);
87 assetTotal -= vault->at(sfLossUnrealized);
88 STAmount shares{vault->at(sfShareMPTID)};
91 Number const shareTotal = issuance->at(sfOutstandingAmount);
92 Number result = (shareTotal * assets) / assetTotal;
106 XRPL_ASSERT(!shares.
negative(),
"xrpl::sharesToAssetsWithdraw : non-negative shares");
108 shares.
asset() == vault->at(sfShareMPTID),
109 "xrpl::sharesToAssetsWithdraw : shares and vault match");
110 if (shares.
negative() || shares.
asset() != vault->at(sfShareMPTID))
113 Number assetTotal = vault->at(sfAssetsTotal);
115 assetTotal -= vault->at(sfLossUnrealized);
116 STAmount assets{vault->at(sfAsset)};
119 Number const shareTotal = issuance->at(sfOutstandingAmount);
120 assets = (assetTotal * shares) / shareTotal;
128 issuance && issuance->getType() == ltMPTOKEN_ISSUANCE,
129 "xrpl::isSoleShareholder : valid issuance SLE");
131 std::uint64_t const outstanding = issuance->at(sfOutstandingAmount);
132 if (outstanding == 0)
135 auto const shareMPTID =
136 makeMptID(issuance->getFieldU32(sfSequence), issuance->getAccountID(sfIssuer));
141 return sleToken->getFieldU64(sfMPTAmount) == outstanding;
147 XRPL_ASSERT(vault && vault->getType() == ltVAULT,
"xrpl::getVaultVersion : valid Vault sle");
148 if (!vault->isFieldPresent(sfLEVersion))
151 auto const version = vault->at(sfLEVersion);
155 UNREACHABLE(
"xrpl::getVaultVersion : invalid vault version");
177 XRPL_ASSERT(vault && vault->getType() == ltVAULT,
"xrpl::getVaultKind : valid Vault sle");
178 return decodeVaultKind(vault->at(~sfVaultKind));
184 return decodeVaultKind(tx[~sfVaultKind]);
190 auto const kindField = tx[~sfVaultKind];
208 XRPL_ASSERT(vault && vault->getType() == ltVAULT,
"xrpl::getVaultPhase : valid Vault sle");
210 view, (*vault)[~sfVaultKind], (*vault)[~sfSubscriptionDate], (*vault)[~sfRedemptionDate]);
Number is a floating point type that can represent a wide range of values.
Number truncate() const noexcept
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
std::uint64_t mantissa() const noexcept
bool negative() const noexcept
Asset const & asset() const
int exponent() const noexcept
std::shared_ptr< STLedgerEntry const > const & const_ref
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
VaultPhase
Lifecycle phase of a vault.
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 shares to demand from the depositor when they a...
VaultKind getVaultKind(SLE::const_ref vault)
Resolves the VaultKind of a vault SLE.
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp, ExpiryComparison comparison=ExpiryComparison::Inclusive)
Determines whether the given expiration time has passed.
bool isValidClosedEndedGap(std::uint32_t sub, std::uint32_t red)
Returns true iff the (SubscriptionDate, RedemptionDate) gap of a closed-ended vault satisfies kMinInv...
VaultPhase getVaultPhase(ReadView const &view, SLE::const_ref vault)
Returns the current lifecycle phase of a vault.
constexpr std::uint32_t kMaxInvestmentPeriod
std::optional< STAmount > sharesToAssetsWithdraw(SLE::const_ref vault, SLE::const_ref 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...
TruncateShares
Controls whether to truncate shares instead of rounding.
VaultVersion
Vault ledger-entry schema versions.
std::optional< STAmount > sharesToAssetsDeposit(SLE::const_ref vault, SLE::const_ref issuance, STAmount const &shares)
From the perspective of a vault, return the number of assets to take from depositor when they receive...
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isSoleShareholder(ReadView const &view, AccountID const &account, SLE::const_ref issuance)
Returns true iff account holds all of the vault's outstanding shares — i.e.
bool isValidVaultKind(STTx const &tx)
Returns true iff sfVaultKind is either absent from tx or is present and equal to a recognised VaultKi...
VaultVersion getVaultVersion(SLE::const_ref vault)
Resolves a Vault's LEVersion, the single point every accounting touch point should call to determine ...
constexpr std::uint32_t kMinInvestmentPeriod
Bounds on the length of a closed-ended vault's Investment phase (RedemptionDate - SubscriptionDate).
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 shares to give depositor when they offer a fixe...
WaiveUnrealizedLoss
Controls whether the withdraw conversion helpers (assetsToSharesWithdraw and sharesToAssetsWithdraw) ...