xrpld
Loading...
Searching...
No Matches
VaultHelpers.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/ledger/ReadView.h>
6#include <xrpl/protocol/AccountID.h>
7#include <xrpl/protocol/Asset.h>
8#include <xrpl/protocol/Protocol.h>
9#include <xrpl/protocol/Rules.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/STLedgerEntry.h>
12#include <xrpl/protocol/TER.h>
13
14#include <cstdint>
15#include <expected>
16#include <optional>
17#include <string_view>
18
19namespace xrpl {
20
21class STTx;
22
37[[nodiscard]] Number
39
44{
51
56
61
66};
67
81[[nodiscard]] TER
83
96[[nodiscard]] TER
97adjustVaultCash(SLE::Ref vault, STAmount const& delta, beast::Journal j);
98
106[[nodiscard]] int
108
115[[nodiscard]] int
117
121[[nodiscard]] STAmount
123 SLE::ConstRef vault,
124 STAmount const& amount,
125 Number::RoundingMode roundingMode);
126
127namespace detail {
128
132[[nodiscard]] int
134
141[[nodiscard]] int
142liveScale(Number const& reference, Asset const& asset, int baseScale);
143
157[[nodiscard]] int
159 VaultVersion version,
160 Asset const& asset,
161 int baseScale,
162 Number const& reference,
163 Number const& delta);
164
176[[nodiscard]] STAmount
178 Asset const& asset,
179 Number const& reference,
180 int baseScale,
181 Number const& raw,
182 Number::RoundingMode roundingMode);
183
184} // namespace detail
185
206[[nodiscard]] STAmount
208 SLE::ConstRef vault,
209 Number const& raw,
210 Number::RoundingMode roundingMode);
211
217[[nodiscard]] Number
219
230[[nodiscard]] TER
231checkOptionalVaultInflow(SLE::ConstRef vault, STAmount const& amount);
232
247[[nodiscard]] Number
248vaultOpenZoneCapacity(SLE::ConstRef vault, Number const& roundedAmount);
249
261[[nodiscard]] std::optional<STAmount>
262assetsToSharesDeposit(SLE::ConstRef vault, SLE::ConstRef issuance, STAmount const& assets);
263
275[[nodiscard]] std::optional<STAmount>
276sharesToAssetsDeposit(SLE::ConstRef vault, SLE::ConstRef issuance, STAmount const& shares);
277
305[[nodiscard]] std::expected<STAmount, TER>
306clampToAssetsTotalScale(SLE::ConstRef vault, STAmount const& delta);
307
322[[nodiscard]] std::expected<STAmount, TER>
323clampVaultOutflow(SLE::ConstRef vault, STAmount const& delta);
324
335[[nodiscard]] int
336vaultBaseScale(Asset const& asset, std::uint8_t scale);
337
351[[nodiscard]] bool
352isOnVaultBaseGrid(Asset const& asset, Number const& value, int baseScale);
353
363[[nodiscard]] TER
364checkAssetsMaximum(SLE::ConstRef vault, Number const& amount);
365
369enum class TruncateShares : bool { No = false, Yes = true };
370
378enum class WaiveUnrealizedLoss : bool { No = false, Yes = true };
379
389[[nodiscard]] Number
391
405[[nodiscard]] TER
407 SLE::ConstRef vault,
408 Number const& amount,
410 std::string_view name);
411
427[[nodiscard]] std::optional<STAmount>
429 SLE::ConstRef vault,
430 SLE::ConstRef issuance,
431 STAmount const& assets,
434
448[[nodiscard]] std::optional<STAmount>
450 SLE::ConstRef vault,
451 SLE::ConstRef issuance,
452 STAmount const& shares,
454
465[[nodiscard]] bool
466isSoleShareholder(ReadView const& view, AccountID const& account, SLE::ConstRef issuance);
467
482[[nodiscard]] VaultVersion
484
494[[nodiscard]] VaultVersion
496
510[[nodiscard]] VaultVersion
511getVaultDefaultVersion(Rules const& rules);
512
520[[nodiscard]] VaultKind
522
532[[nodiscard]] VaultKind
533getVaultKind(STTx const& tx);
534
542[[nodiscard]] bool
543isValidVaultKind(STTx const& tx);
544
555[[nodiscard]] bool
557
567[[nodiscard]] VaultPhase
568getVaultPhase(ReadView const& view, SLE::ConstRef vault);
569
584[[nodiscard]] VaultPhase
586 ReadView const& view,
588 std::optional<std::uint32_t> subscriptionDate,
589 std::optional<std::uint32_t> redemptionDate);
590
597enum class SuppressExpired : bool { No = false, Yes = true };
598
620[[nodiscard]] TER
622 ReadView const& view,
623 SLE::ConstRef issuance,
624 AccountID const& subject,
625 SuppressExpired suppressExpired);
626
627} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::shared_ptr< STLedgerEntry > const & Ref
int posteriorAssetScale(VaultVersion version, Asset const &asset, int baseScale, Number const &reference, Number const &delta)
Shared core of getPosteriorVaultScale and getPosteriorBrokerCoverScale: the exponent reference + delt...
int liveScale(Number const &reference, Asset const &asset, int baseScale)
Shared grid-floor core of getVaultScale / getPosteriorVaultScale / getPosteriorBrokerCoverScale: base...
STAmount creditToPosteriorScale(Asset const &asset, Number const &reference, int baseScale, Number const &raw, Number::RoundingMode roundingMode)
Shared core of creditToPosteriorAvailableScale and creditToPosteriorBrokerCoverScale: floors referenc...
int getPosteriorVaultScale(SLE::ConstRef vault, STAmount const &delta)
Return the Vault's posterior live exponent after applying an unrounded delta.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
VaultPhase
Lifecycle phase of a vault.
Definition Protocol.h:367
std::optional< STAmount > sharesToAssetsWithdraw(SLE::ConstRef vault, SLE::ConstRef 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...
bool isSoleShareholder(ReadView const &view, AccountID const &account, SLE::ConstRef issuance)
Returns true iff account holds all of the vault's outstanding shares, i.e.
Number vaultOpenZoneCapacity(SLE::ConstRef vault, Number const &roundedAmount)
Open-zone capacity after adding roundedAmount: AssetsTotal + YieldUnrealized.
std::expected< STAmount, TER > clampVaultOutflow(SLE::ConstRef vault, STAmount const &delta)
Outflow clamp dispatcher for VaultWithdraw and VaultClawback: rounds a negative delta to the scale ap...
bool isOnVaultBaseGrid(Asset const &asset, Number const &value, int baseScale)
Returns true iff value is exactly representable both as an STAmount of asset and on the base grid at ...
TER checkVaultDomain(ReadView const &view, SLE::ConstRef issuance, AccountID const &subject, SuppressExpired suppressExpired)
Checks that subject belongs to the permissioned domain governing a vault's shares.
std::optional< STAmount > assetsToSharesWithdraw(SLE::ConstRef vault, SLE::ConstRef 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...
bool isValidClosedEndedGap(std::uint32_t sub, std::uint32_t red)
Returns true iff the (SubscriptionDate, RedemptionDate) gap of a closed-ended vault satisfies kMinInv...
std::optional< STAmount > sharesToAssetsDeposit(SLE::ConstRef vault, SLE::ConstRef issuance, STAmount const &shares)
From the perspective of a vault, return the number of assets to take from depositor when they receive...
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
Definition STAmount.h:819
Number assetsTotalForWithdrawal(SLE::ConstRef vault, WaiveUnrealizedLoss waive)
Returns the assets backing outstanding shares for a withdrawal: sfAssetsTotal minus sfLossUnrealized,...
int getVaultScale(SLE::ConstRef vault)
Return the Vault's current live exponent.
std::expected< STAmount, TER > clampToAssetsTotalScale(SLE::ConstRef vault, STAmount const &delta)
Adjusts a requested asset change (delta) to match the decimal scale of the updated total vault assets...
TER checkAssetsMaximum(SLE::ConstRef vault, Number const &amount)
Checks that a requested AssetsMaximum is exactly representable on the Vault's base grid,...
VaultKind
Vault kind.
Definition Protocol.h:358
TER adjustVaultCash(SLE::Ref vault, STAmount const &delta, beast::Journal j)
Apply a signed cash movement to a Vault of any version.
VaultVersion getVaultDefaultVersion(Rules const &rules)
Resolves which LEVersion a newly-created Vault should get under the currently active amendments.
@ Yes
We have consensus along with the network.
@ No
We do not have consensus.
TruncateShares
Controls whether to truncate shares instead of rounding.
TER adjustVaultBalances(SLE::Ref vault, VaultBalanceChange const &change, beast::Journal j)
Apply a balance change to a FixedPrecision Vault.
VaultVersion
Vault ledger-entry schema versions, persisted as sfLEVersion.
Definition Protocol.h:342
TER checkDebitNotDust(SLE::ConstRef vault, Number const &amount, beast::Journal j, std::string_view name)
Reject a cash outflow that is dust (see debitIsNonZeroDust) or whose check overflows Number,...
std::optional< STAmount > assetsToSharesDeposit(SLE::ConstRef vault, SLE::ConstRef issuance, STAmount const &assets)
From the perspective of a vault, return the number of shares to give depositor when they offer a fixe...
STAmount roundToPosteriorVaultScale(SLE::ConstRef vault, STAmount const &amount, Number::RoundingMode roundingMode)
Round an amount at the Vault's posterior live exponent.
VaultPhase getVaultPhase(ReadView const &view, SLE::ConstRef vault)
Returns the current lifecycle phase of a vault.
int getVaultBaseScale(SLE::ConstRef vault)
Return the Vault's base exponent.
TER checkOptionalVaultInflow(SLE::ConstRef vault, STAmount const &amount)
Check whether amount is an admissible optional inflow.
VaultKind getVaultKind(SLE::ConstRef vault)
Resolves the VaultKind of a vault SLE.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
Number getVaultOpenLimit(SLE::ConstRef vault)
Open-zone capacity ceiling: 9 * 10^(15 + baseScale).
TERSubset< CanCvtToTER > TER
Definition TER.h:657
int vaultBaseScale(Asset const &asset, std::uint8_t scale)
Returns the Vault's base exponent for asset at scale, before a Vault object exists to read it from: 0...
Number getAssetsTotal(SLE::ConstRef vault)
Return the Vault's current assets total.
SuppressExpired
Controls whether checkVaultDomain reports an expired credential as an error.
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::ConstRef vault)
Resolves a Vault's LEVersion.
STAmount creditToPosteriorAvailableScale(SLE::ConstRef vault, Number const &raw, Number::RoundingMode roundingMode)
Round a FixedPrecision cash inflow (LoanPay's credit, LoanManage's default cover credit) into AssetsA...
VaultVersion decodeVaultVersion(std::optional< std::uint8_t > leVersion)
Decodes an already-extracted sfLEVersion value with the same range check as getVaultVersion.
WaiveUnrealizedLoss
Controls whether the withdraw conversion helpers (assetsToSharesWithdraw and sharesToAssetsWithdraw) ...
A change to a FixedPrecision Vault's balances.
Number loss
Signed change to impaired principal (LossUnrealized), exact.
std::optional< STAmount > cash
Signed change to AssetsAvailable.
Number yield
Signed change to scheduled, unpaid interest (YieldUnrealized), exact.
Number deployed
Signed change to AssetsDeployed: open Loan principal, exact.