1#include <xrpl/basics/Number.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/beast/hash/uhash.h>
4#include <xrpl/beast/utility/Zero.h>
5#include <xrpl/ledger/helpers/VaultHelpers.h>
6#include <xrpl/protocol/Asset.h>
7#include <xrpl/protocol/Feature.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/Issue.h>
10#include <xrpl/protocol/MPTIssue.h>
11#include <xrpl/protocol/Protocol.h>
12#include <xrpl/protocol/Rules.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STAmount.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STNumber.h>
17#include <xrpl/protocol/STTakesAsset.h>
18#include <xrpl/protocol/TER.h>
19#include <xrpl/protocol/UintTypes.h>
21#include <gtest/gtest.h>
22#include <helpers/Account.h>
23#include <protocol/VaultTestHelpers.h>
33TEST(VaultGrid, base_and_live_scale)
55TEST(VaultGrid, pre_v12_behavior_is_preserved)
59 Number const assetsTotal{1'000'000};
70 for (
auto const version : {
71 std::optional<VaultVersion>{},
85TEST(VaultGrid, pre_v12_credit_clamp_floors_posterior_total)
89 Number const assetsTotal{9'999'999'999'999'999LL, -15};
91 STAmount const expected{iou,
Number{4'999'999'999'999'991LL, -15}};
93 for (
auto const version : {
94 std::optional<VaultVersion>{},
100 ASSERT_TRUE(result.has_value());
101 EXPECT_EQ(*result, expected);
105TEST(VaultGrid, integral_scale_is_zero)
116TEST(VaultGrid, posterior_scale_rounds_delta)
131TEST(VaultGrid, posterior_scale_rejects_dust_at_call_site)
142TEST(VaultGrid, posterior_outflow_can_refine_scale)
155TEST(VaultGrid, optional_inflow_capacity_boundaries)
175 auto const coarsening =
181TEST(VaultGrid, optional_inflow_includes_yield_unrealized)
198TEST(VaultGrid, credit_floors_sum_in_requested_mode)
208 Number{1'234'567'890'123'456LL, -6});
209 Number const raw{9'996, -10};
214 EXPECT_EQ(credit,
STAmount(iou, 0));
215 EXPECT_LE(
Number(credit), raw);
218TEST(VaultGrid, is_on_vault_base_grid)
230 {.value =
Number{0}, .expected =
true},
231 {.value =
Number{1}, .expected =
true},
232 {.value =
Number{1, -6}, .expected =
true},
233 {.value =
Number{1, 20}, .expected =
true},
234 {.value =
Number{1'234'567'890'123'456LL, -6}, .expected =
true},
236 {.value =
Number{1, -7}, .expected =
false},
237 {.value =
Number{15, -7}, .expected =
false},
239 {.value =
Number{12'345'678'901'234'567LL, 0}, .expected =
false},
241 for (
auto const& row : rows)
242 EXPECT_EQ(
isOnVaultBaseGrid(iou, row.value, baseScale), row.expected) << row.value;
253TEST(VaultGrid, clamp_vault_outflow)
261 char const* name =
nullptr;
265 std::optional<Number> expected;
269 .name =
"XRP outflow is returned unrounded",
271 .available =
Number{1'000},
276 .name =
"MPT outflow is returned unrounded",
278 .available =
Number{1'000},
283 .name =
"IOU outflow rounds towards zero at the posterior scale",
285 .available =
Number{1'000'000},
287 .expected =
Number{1, -6},
290 .name =
"IOU outflow below one unit at the posterior scale is rejected",
292 .available =
Number{1'000'000},
294 .expected = std::nullopt,
297 for (
auto const& row : rows)
303 row.asset.integral() ? 0 : 6,
309 ASSERT_TRUE(result.has_value()) << row.name;
310 EXPECT_EQ(*result, (
STAmount{row.asset, *row.expected})) << row.name;
314 ASSERT_FALSE(result.has_value()) << row.name;
320TEST(VaultGrid, get_vault_default_version)
324 char const* name =
nullptr;
325 std::unordered_set<UInt256, beast::Uhash<>> enabled;
331 .name =
"LendingProtocolV1_1: CashBasis",
332 .enabled = {featureLendingProtocolV1_1},
336 .name =
"V1_1 and V1_2 without the cleanups: CashBasis",
337 .enabled = {featureLendingProtocolV1_1, featureLendingProtocolV1_2},
341 .name =
"V1_1, V1_2 and fixCleanup3_2_0 without fixCleanup3_4_0: CashBasis",
342 .enabled = {featureLendingProtocolV1_1, featureLendingProtocolV1_2, fixCleanup3_2_0},
346 .name =
"V1_1, V1_2 and fixCleanup3_4_0: FixedPrecision",
347 .enabled = {featureLendingProtocolV1_1, featureLendingProtocolV1_2, fixCleanup3_4_0},
351 .name =
"V1_2 and both cleanups without V1_1: Legacy",
352 .enabled = {featureLendingProtocolV1_2, fixCleanup3_2_0, fixCleanup3_4_0},
356 .name =
"V1_1, V1_2 and both cleanups: FixedPrecision",
359 featureLendingProtocolV1_1,
360 featureLendingProtocolV1_2,
367 for (
auto const& row : rows)
370 Rules const rules{row.enabled};
A currency issued by an account.
Number is a floating point type that can represent a wide range of values.
Rules controlling protocol behavior.
static constexpr std::uint64_t kMaxNativeN
Immutable cryptographic account descriptor.
int getPosteriorVaultScale(SLE::ConstRef vault, STAmount const &delta)
Return the Vault's posterior live exponent after applying an unrounded delta.
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
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 ...
TEST(FileUtilitiesTest, get_file_contents)
int getVaultScale(SLE::ConstRef vault)
Return the Vault's current live exponent.
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)
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...
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
VaultVersion getVaultDefaultVersion(Rules const &rules)
Resolves which LEVersion a newly-created Vault should get under the currently active amendments.
VaultVersion
Vault ledger-entry schema versions, persisted as sfLEVersion.
STAmount roundToPosteriorVaultScale(SLE::ConstRef vault, STAmount const &amount, Number::RoundingMode roundingMode)
Round an amount at the Vault's posterior live exponent.
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.
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Number getVaultOpenLimit(SLE::ConstRef vault)
Open-zone capacity ceiling: 9 * 10^(15 + baseScale).
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...
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
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...