xrpld
Loading...
Searching...
No Matches
VaultTestHelpers.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/protocol/Asset.h>
6#include <xrpl/protocol/Indexes.h>
7#include <xrpl/protocol/Protocol.h>
8#include <xrpl/protocol/SField.h>
9// Needed for standalone compilation of sfScale/sfLEVersion assignment below;
10// full-TU builds already see it transitively, so misc-include-cleaner flags
11// it as unused there. Keep it so this header still compiles on its own.
12#include <xrpl/protocol/STInteger.h> // NOLINT(misc-include-cleaner)
13#include <xrpl/protocol/STIssue.h>
14#include <xrpl/protocol/STLedgerEntry.h>
15#include <xrpl/protocol/STNumber.h> // IWYU pragma: keep
16#include <xrpl/protocol/STTakesAsset.h>
17
18#include <cstdint>
19#include <memory>
20#include <optional>
21#include <utility>
22
23namespace xrpl {
24
25// Shared by VaultGridTests, VaultBalanceTests and LoanBrokerCoverTests. For
26// FixedPrecision, getAssetsTotal derives from AssetsAvailable plus
27// AssetsDeployed rather than reading the stored AssetsTotal cache, so a
28// FixedPrecision vault must have AssetsAvailable (and, if requested, a
29// non-zero AssetsDeployed) seeded separately from assetsTotal. By default
30// AssetsAvailable is assetsTotal - assetsDeployed, so the derived total
31// equals assetsTotal; pass assetsAvailable explicitly to decouple it (e.g.
32// to leave assetsTotal as a deliberately stale cache). Legacy/CashBasis
33// vaults have no AssetsDeployed field and normally leave AssetsAvailable
34// unset, but some cash-flow tests need it seeded too, so assetsAvailable is
35// honored for those versions as well when given.
36inline std::shared_ptr<SLE>
38 Asset const& asset,
39 Number const& assetsTotal,
42 Number const& assetsDeployed = Number{0},
43 std::optional<Number> assetsAvailable = std::nullopt)
44{
46 vault->setFieldIssue(sfAsset, STIssue{sfAsset, asset});
47 vault->at(sfAssetsTotal) = assetsTotal;
48 if (!asset.integral())
49 vault->at(sfScale) = scaleValue;
50 if (version)
51 vault->at(sfLEVersion) = std::to_underlying(*version);
52 if (version == VaultVersion::FixedPrecision)
53 {
54 vault->at(sfAssetsAvailable) = assetsAvailable.value_or(assetsTotal - assetsDeployed);
55 vault->at(sfAssetsDeployed) = assetsDeployed;
56 }
57 else if (assetsAvailable)
58 {
59 vault->at(sfAssetsAvailable) = *assetsAvailable;
60 }
61 associateAsset(*vault, asset);
62 return vault;
63}
64
65} // namespace xrpl
bool integral() const
Definition Asset.h:133
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
T make_shared(T... args)
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::shared_ptr< SLE > makeVault(Asset const &asset, Number const &assetsTotal, std::optional< VaultVersion > version, std::uint8_t scaleValue=kVaultDefaultIouScale, Number const &assetsDeployed=Number{0}, std::optional< Number > assetsAvailable=std::nullopt)
constexpr std::uint8_t kVaultDefaultIouScale
Default IOU scale factor for a Vault.
Definition Protocol.h:313
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
UInt256 uint256
Definition base_uint.h:587
T value_or(T... args)