rippled
Loading...
Searching...
No Matches
VaultHelpers.cpp
1#include <xrpl/ledger/helpers/VaultHelpers.h>
2//
3#include <xrpl/basics/Number.h>
4#include <xrpl/protocol/LedgerFormats.h>
5#include <xrpl/protocol/st.h>
6
7namespace xrpl {
8
9[[nodiscard]] std::optional<STAmount>
11 std::shared_ptr<SLE const> const& vault,
12 std::shared_ptr<SLE const> const& issuance,
13 STAmount const& assets)
14{
15 XRPL_ASSERT(!assets.negative(), "xrpl::assetsToSharesDeposit : non-negative assets");
16 XRPL_ASSERT(
17 assets.asset() == vault->at(sfAsset),
18 "xrpl::assetsToSharesDeposit : assets and vault match");
19 if (assets.negative() || assets.asset() != vault->at(sfAsset))
20 return std::nullopt; // LCOV_EXCL_LINE
21
22 Number const assetTotal = vault->at(sfAssetsTotal);
23 STAmount shares{vault->at(sfShareMPTID)};
24 if (assetTotal == 0)
25 {
26 return STAmount{
27 shares.asset(),
28 Number(assets.mantissa(), assets.exponent() + vault->at(sfScale)).truncate()};
29 }
30
31 Number const shareTotal = issuance->at(sfOutstandingAmount);
32 shares = ((shareTotal * assets) / assetTotal).truncate();
33 return shares;
34}
35
36[[nodiscard]] std::optional<STAmount>
38 std::shared_ptr<SLE const> const& vault,
39 std::shared_ptr<SLE const> const& issuance,
40 STAmount const& shares)
41{
42 XRPL_ASSERT(!shares.negative(), "xrpl::sharesToAssetsDeposit : non-negative shares");
43 XRPL_ASSERT(
44 shares.asset() == vault->at(sfShareMPTID),
45 "xrpl::sharesToAssetsDeposit : shares and vault match");
46 if (shares.negative() || shares.asset() != vault->at(sfShareMPTID))
47 return std::nullopt; // LCOV_EXCL_LINE
48
49 Number const assetTotal = vault->at(sfAssetsTotal);
50 STAmount assets{vault->at(sfAsset)};
51 if (assetTotal == 0)
52 {
53 return STAmount{
54 assets.asset(), shares.mantissa(), shares.exponent() - vault->at(sfScale), false};
55 }
56
57 Number const shareTotal = issuance->at(sfOutstandingAmount);
58 assets = (assetTotal * shares) / shareTotal;
59 return assets;
60}
61
62[[nodiscard]] std::optional<STAmount>
64 std::shared_ptr<SLE const> const& vault,
65 std::shared_ptr<SLE const> const& issuance,
66 STAmount const& assets,
67 TruncateShares truncate)
68{
69 XRPL_ASSERT(!assets.negative(), "xrpl::assetsToSharesWithdraw : non-negative assets");
70 XRPL_ASSERT(
71 assets.asset() == vault->at(sfAsset),
72 "xrpl::assetsToSharesWithdraw : assets and vault match");
73 if (assets.negative() || assets.asset() != vault->at(sfAsset))
74 return std::nullopt; // LCOV_EXCL_LINE
75
76 Number assetTotal = vault->at(sfAssetsTotal);
77 assetTotal -= vault->at(sfLossUnrealized);
78 STAmount shares{vault->at(sfShareMPTID)};
79 if (assetTotal == 0)
80 return shares;
81 Number const shareTotal = issuance->at(sfOutstandingAmount);
82 Number result = (shareTotal * assets) / assetTotal;
83 if (truncate == TruncateShares::yes)
84 result = result.truncate();
85 shares = result;
86 return shares;
87}
88
89[[nodiscard]] std::optional<STAmount>
91 std::shared_ptr<SLE const> const& vault,
92 std::shared_ptr<SLE const> const& issuance,
93 STAmount const& shares)
94{
95 XRPL_ASSERT(!shares.negative(), "xrpl::sharesToAssetsWithdraw : non-negative shares");
96 XRPL_ASSERT(
97 shares.asset() == vault->at(sfShareMPTID),
98 "xrpl::sharesToAssetsWithdraw : shares and vault match");
99 if (shares.negative() || shares.asset() != vault->at(sfShareMPTID))
100 return std::nullopt; // LCOV_EXCL_LINE
101
102 Number assetTotal = vault->at(sfAssetsTotal);
103 assetTotal -= vault->at(sfLossUnrealized);
104 STAmount assets{vault->at(sfAsset)};
105 if (assetTotal == 0)
106 return assets;
107 Number const shareTotal = issuance->at(sfOutstandingAmount);
108 assets = (assetTotal * shares) / shareTotal;
109 return assets;
110}
111
112} // namespace xrpl
Number is a floating point type that can represent a wide range of values.
Definition Number.h:207
Number truncate() const noexcept
Definition Number.cpp:819
std::uint64_t mantissa() const noexcept
Definition STAmount.h:451
bool negative() const noexcept
Definition STAmount.h:445
Asset const & asset() const
Definition STAmount.h:457
int exponent() const noexcept
Definition STAmount.h:420
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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...