1#include <xrpl/ledger/helpers/VaultHelpers.h>
3#include <xrpl/basics/Number.h>
4#include <xrpl/protocol/LedgerFormats.h>
5#include <xrpl/protocol/st.h>
15 XRPL_ASSERT(!assets.
negative(),
"xrpl::assetsToSharesDeposit : non-negative assets");
17 assets.
asset() == vault->at(sfAsset),
18 "xrpl::assetsToSharesDeposit : assets and vault match");
19 if (assets.
negative() || assets.
asset() != vault->at(sfAsset))
22 Number const assetTotal = vault->at(sfAssetsTotal);
23 STAmount shares{vault->at(sfShareMPTID)};
31 Number const shareTotal = issuance->at(sfOutstandingAmount);
32 shares = ((shareTotal * assets) / assetTotal).truncate();
42 XRPL_ASSERT(!shares.
negative(),
"xrpl::sharesToAssetsDeposit : non-negative shares");
44 shares.
asset() == vault->at(sfShareMPTID),
45 "xrpl::sharesToAssetsDeposit : shares and vault match");
46 if (shares.
negative() || shares.
asset() != vault->at(sfShareMPTID))
49 Number const assetTotal = vault->at(sfAssetsTotal);
57 Number const shareTotal = issuance->at(sfOutstandingAmount);
58 assets = (assetTotal * shares) / shareTotal;
69 XRPL_ASSERT(!assets.
negative(),
"xrpl::assetsToSharesWithdraw : non-negative assets");
71 assets.
asset() == vault->at(sfAsset),
72 "xrpl::assetsToSharesWithdraw : assets and vault match");
73 if (assets.
negative() || assets.
asset() != vault->at(sfAsset))
76 Number assetTotal = vault->at(sfAssetsTotal);
77 assetTotal -= vault->at(sfLossUnrealized);
78 STAmount shares{vault->at(sfShareMPTID)};
81 Number const shareTotal = issuance->at(sfOutstandingAmount);
82 Number result = (shareTotal * assets) / assetTotal;
95 XRPL_ASSERT(!shares.
negative(),
"xrpl::sharesToAssetsWithdraw : non-negative shares");
97 shares.
asset() == vault->at(sfShareMPTID),
98 "xrpl::sharesToAssetsWithdraw : shares and vault match");
99 if (shares.
negative() || shares.
asset() != vault->at(sfShareMPTID))
102 Number assetTotal = vault->at(sfAssetsTotal);
103 assetTotal -= vault->at(sfLossUnrealized);
104 STAmount assets{vault->at(sfAsset)};
107 Number const shareTotal = issuance->at(sfOutstandingAmount);
108 assets = (assetTotal * shares) / shareTotal;
Number is a floating point type that can represent a wide range of values.
Number truncate() const noexcept
std::uint64_t mantissa() const noexcept
bool negative() const noexcept
Asset const & asset() const
int exponent() const noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::optional< STAmount > sharesToAssetsDeposit(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
From the perspective of a vault, return the number of assets to take from depositor when they receive...
TruncateShares
Controls whether to truncate shares instead of rounding.
std::optional< STAmount > assetsToSharesDeposit(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets)
From the perspective of a vault, return the number of shares to give depositor when they offer a fixe...
std::optional< STAmount > assetsToSharesWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets, TruncateShares truncate=TruncateShares::no)
From the perspective of a vault, return the number of shares to demand from the depositor when they a...
std::optional< STAmount > sharesToAssetsWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
From the perspective of a vault, return the number of assets to give the depositor when they redeem a...