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/protocol/AccountID.h>
7#include <xrpl/protocol/Indexes.h>
8#include <xrpl/protocol/LedgerFormats.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/STLedgerEntry.h>
12#include <xrpl/protocol/STNumber.h>
19[[nodiscard]] std::optional<STAmount>
22 XRPL_ASSERT(!assets.
negative(),
"xrpl::assetsToSharesDeposit : non-negative assets");
24 assets.
asset() == vault->at(sfAsset),
25 "xrpl::assetsToSharesDeposit : assets and vault match");
26 if (assets.
negative() || assets.
asset() != vault->at(sfAsset))
29 Number const assetTotal = vault->at(sfAssetsTotal);
30 STAmount shares{vault->at(sfShareMPTID)};
38 Number const shareTotal = issuance->at(sfOutstandingAmount);
39 shares = ((shareTotal * assets) / assetTotal).truncate();
46 XRPL_ASSERT(!shares.
negative(),
"xrpl::sharesToAssetsDeposit : non-negative shares");
48 shares.
asset() == vault->at(sfShareMPTID),
49 "xrpl::sharesToAssetsDeposit : shares and vault match");
50 if (shares.
negative() || shares.
asset() != vault->at(sfShareMPTID))
53 Number const assetTotal = vault->at(sfAssetsTotal);
61 Number const shareTotal = issuance->at(sfOutstandingAmount);
62 assets = (assetTotal * shares) / shareTotal;
74 XRPL_ASSERT(!assets.
negative(),
"xrpl::assetsToSharesWithdraw : non-negative assets");
76 assets.
asset() == vault->at(sfAsset),
77 "xrpl::assetsToSharesWithdraw : assets and vault match");
78 if (assets.
negative() || assets.
asset() != vault->at(sfAsset))
81 Number assetTotal = vault->at(sfAssetsTotal);
83 assetTotal -= vault->at(sfLossUnrealized);
84 STAmount shares{vault->at(sfShareMPTID)};
87 Number const shareTotal = issuance->at(sfOutstandingAmount);
88 Number result = (shareTotal * assets) / assetTotal;
102 XRPL_ASSERT(!shares.
negative(),
"xrpl::sharesToAssetsWithdraw : non-negative shares");
104 shares.
asset() == vault->at(sfShareMPTID),
105 "xrpl::sharesToAssetsWithdraw : shares and vault match");
106 if (shares.
negative() || shares.
asset() != vault->at(sfShareMPTID))
109 Number assetTotal = vault->at(sfAssetsTotal);
111 assetTotal -= vault->at(sfLossUnrealized);
112 STAmount assets{vault->at(sfAsset)};
115 Number const shareTotal = issuance->at(sfOutstandingAmount);
116 assets = (assetTotal * shares) / shareTotal;
124 issuance && issuance->getType() == ltMPTOKEN_ISSUANCE,
125 "xrpl::isSoleShareholder : valid issuance SLE");
127 std::uint64_t const outstanding = issuance->at(sfOutstandingAmount);
128 if (outstanding == 0)
131 auto const shareMPTID =
132 makeMptID(issuance->getFieldU32(sfSequence), issuance->getAccountID(sfIssuer));
137 return sleToken->getFieldU64(sfMPTAmount) == outstanding;
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.
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...
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.
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...
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.
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
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) ...