xrpld
Loading...
Searching...
No Matches
VaultBalanceTests.cpp
1#include <xrpl/basics/Number.h>
2#include <xrpl/beast/utility/Journal.h>
3#include <xrpl/ledger/helpers/VaultHelpers.h>
4#include <xrpl/protocol/Asset.h>
5#include <xrpl/protocol/Indexes.h>
6#include <xrpl/protocol/Issue.h>
7#include <xrpl/protocol/MPTIssue.h>
8#include <xrpl/protocol/Protocol.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/STLedgerEntry.h>
12#include <xrpl/protocol/STNumber.h> // IWYU pragma: keep
13#include <xrpl/protocol/TER.h>
14#include <xrpl/protocol/UintTypes.h>
15
16#include <gtest/gtest.h>
17#include <helpers/Account.h>
18#include <protocol/VaultTestHelpers.h>
19
20#include <cstdint>
21#include <memory>
22#include <string_view>
23#include <vector>
24
25namespace xrpl {
26namespace {
27
28beast::Journal const kNullJournal{beast::Journal::getNullSink()};
29
30// Snapshots every field adjustVaultBalances can touch, for atomicity checks.
31struct BalanceSnapshot
32{
33 Number available;
34 Number assetsDeployed;
35 Number loss;
36 Number yield;
37 Number total;
38
39 static BalanceSnapshot
40 of(SLE::ConstRef vault)
41 {
42 return {
43 .available = vault->at(sfAssetsAvailable),
44 .assetsDeployed = vault->at(sfAssetsDeployed),
45 .loss = vault->at(sfLossUnrealized),
46 .yield = vault->at(sfYieldUnrealized),
47 .total = vault->at(sfAssetsTotal),
48 };
49 }
50
51 bool
52 operator==(BalanceSnapshot const&) const = default;
53};
54
55class VaultBalance : public ::testing::Test
56{
57protected:
58 test::Account const issuer_{"issuer"};
59 Issue const iou_{toCurrency("USD"), issuer_.id()};
60
61 // FixedPrecision IOU vault; stored is the (derived, hence possibly
62 // stale) sfAssetsTotal cache.
63 [[nodiscard]] std::shared_ptr<SLE>
64 iouVault(
65 Number const& assetsDeployed,
66 Number const& available,
67 Number const& stored = Number{0},
68 std::uint8_t scale = 6) const
69 {
70 return makeVault(
71 iou_, stored, VaultVersion::FixedPrecision, scale, assetsDeployed, available);
72 }
73
74 // AssetsDeployed 500, available 100.
75 [[nodiscard]] std::shared_ptr<SLE>
76 standardVault() const
77 {
78 return iouVault(Number{500}, Number{100});
79 }
80
81 [[nodiscard]] STAmount
82 iouAmount(Number const& n) const
83 {
84 return STAmount{iou_, n};
85 }
86
87 // Zero cash change: syncs the cached sfAssetsTotal.
88 static void
89 syncTotal(std::shared_ptr<SLE> const& vault)
90 {
91 ASSERT_EQ(adjustVaultBalances(vault, {}, kNullJournal), tesSUCCESS);
92 }
93
94 // Above coarsening the cached sfAssetsTotal (Downward, 16 digits) is a
95 // floor of the exact total: never above it, and within one unit of its
96 // own live exponent.
97 void
98 expectCacheFloorsExactTotal(std::shared_ptr<SLE> const& vault) const
99 {
100 Number const exact = getAssetsTotal(vault);
101 Number const cached = vault->at(sfAssetsTotal);
102 EXPECT_LE(cached, exact);
103 int const liveExponent = scale(cached, iou_);
104 EXPECT_LE(exact - cached, (Number{1, liveExponent}));
105 }
106
107 // Adjusts asset's integral vault by +25 and by an overdraft.
108 static void
109 checkIntegralControl(Asset const& asset)
110 {
111 auto const vault =
112 makeVault(asset, Number{0}, VaultVersion::FixedPrecision, 0, Number{500}, Number{100});
113
114 ASSERT_EQ(
115 adjustVaultBalances(vault, {.cash = STAmount{asset, Number{25}}}, kNullJournal),
116 tesSUCCESS);
117 EXPECT_EQ(vault->at(sfAssetsAvailable), Number{125});
118 EXPECT_EQ(vault->at(sfAssetsTotal), getAssetsTotal(vault));
119
120 EXPECT_EQ(
121 adjustVaultBalances(vault, {.cash = STAmount{asset, Number{-1'000}}}, kNullJournal),
123 }
124};
125
126// ---------------------------------------------------------------------------
127// getAssetsTotal
128// ---------------------------------------------------------------------------
129
130TEST_F(VaultBalance, get_assets_total_stored_for_non_fixed_precision)
131{
132 struct Row
133 {
134 std::string_view name;
135 VaultVersion version = VaultVersion::Legacy;
136 std::int64_t stored = 0;
137 std::int64_t available = 0;
138 };
139 std::vector<Row> const rows{
140 {.name = "Legacy", .version = VaultVersion::Legacy, .stored = 1'234'567, .available = 999},
141 {
142 .name = "CashBasis",
143 .version = VaultVersion::CashBasis,
144 .stored = 9'876'543,
145 .available = 100,
146 },
147 };
148
149 for (auto const& row : rows)
150 {
151 SCOPED_TRACE(row.name);
152 auto const vault =
153 makeVault(iou_, Number{row.stored}, row.version, 6, Number{0}, Number{row.available});
154 EXPECT_EQ(getAssetsTotal(vault), Number{row.stored});
155 EXPECT_FALSE(vault->isFieldPresent(sfAssetsDeployed));
156 }
157}
158
159TEST_F(VaultBalance, get_assets_total_fixed_precision_derived_sum)
160{
161 struct Row
162 {
163 std::string_view name;
164 Number assetsDeployed;
165 Number available;
166 // The synced cache equals the exact total (not merely a floor of it).
167 bool cacheExact = false;
168 };
169 std::vector<Row> const rows{
170 {
171 .name = "ExactSum",
172 .assetsDeployed = Number{250'000},
173 .available = Number{1'000'000},
174 .cacheExact = true,
175 },
176 // available is already 16 significant digits; assetsDeployed pushes the exact
177 // sum to 17, past what a 16-digit STAmount represents exactly.
178 {
179 .name = "ExactPastSixteenDigits",
180 .assetsDeployed = Number{8, -6},
181 .available = Number{9'999'999'999'999'999, -6},
182 },
183 };
184
185 for (auto const& row : rows)
186 {
187 SCOPED_TRACE(row.name);
188 auto const vault = iouVault(row.assetsDeployed, row.available);
189 Number const exact = row.available + row.assetsDeployed;
190
191 // Exact sum: no rounding, no conversion to a 16-digit STAmount.
192 EXPECT_EQ(getAssetsTotal(vault), exact);
193 EXPECT_GE(getAssetsTotal(vault), vault->at(sfAssetsAvailable));
194
195 syncTotal(vault);
196 if (row.cacheExact)
197 {
198 EXPECT_EQ(vault->at(sfAssetsTotal), getAssetsTotal(vault));
199 }
200 expectCacheFloorsExactTotal(vault);
201 }
202}
203
204TEST_F(VaultBalance, get_assets_total_fixed_precision_integral_exact)
205{
206 auto const vault = makeVault(
208 EXPECT_EQ(getAssetsTotal(vault), Number{1'250});
209}
210
211TEST_F(VaultBalance, get_assets_total_ignores_stale_cache)
212{
213 Number const available{4'000'000};
214 Number const assetsDeployed{1'000'000};
215 auto const vault = iouVault(assetsDeployed, available, Number{99});
216 EXPECT_EQ(getAssetsTotal(vault), available + assetsDeployed);
217 EXPECT_NE(vault->at(sfAssetsTotal), getAssetsTotal(vault));
218}
219
220// The exact total, and the synced cache once Downward-rounded, must never
221// fall below AssetsAvailable even when assetsDeployed coarsens the scale.
222TEST_F(VaultBalance, get_assets_total_never_below_available)
223{
224 Number const available{9'999'999'999'999'999, -6};
225 Number const assetsDeployed{5'000'000'000, -6};
226 auto const vault = iouVault(assetsDeployed, available);
227 EXPECT_GT(getVaultScale(vault), getVaultBaseScale(vault));
228 EXPECT_GE(getAssetsTotal(vault), available);
229
230 syncTotal(vault);
231 EXPECT_GE(Number(vault->at(sfAssetsTotal)), available);
232}
233
234// available and assetsDeployed are each at most 16 digits, so associateAsset leaves them
235// alone. Their sum, 99999999999999995 at exponent -4, has a 17th digit (5)
236// that would round up under ToNearest and carry through the run of nines,
237// bumping the exponent. Downward truncation drops it without a bump.
238TEST_F(VaultBalance, live_scale_downward_keeps_lower_exponent_at_carry_boundary)
239{
240 auto vault = iouVault(Number{35, -4}, Number{9'999'999'999'999'996LL, -3});
241
242 int const downwardExponent = [&] {
244 return scale(getAssetsTotal(vault), iou_);
245 }();
246 int const toNearestExponent = [&] {
248 return scale(getAssetsTotal(vault), iou_);
249 }();
250 EXPECT_LT(downwardExponent, toNearestExponent);
251 EXPECT_EQ(getVaultScale(vault), downwardExponent);
252
253 syncTotal(vault);
254 EXPECT_EQ(scale(vault->at(sfAssetsTotal), iou_), downwardExponent);
255 EXPECT_EQ(getVaultScale(vault), scale(vault->at(sfAssetsTotal), iou_));
256}
257
258TEST_F(VaultBalance, scale_and_open_include_assets_deployed)
259{
260 // Available alone stays at base scale; Available + AssetsDeployed coarsens.
261 Number const available{1'000'000'000};
262 Number const assetsDeployed{9'000'000'000};
263 auto const vault = iouVault(assetsDeployed, available, available);
264
265 EXPECT_EQ(getAssetsTotal(vault), available + assetsDeployed);
266 EXPECT_GT(getVaultScale(vault), getVaultBaseScale(vault));
267
268 // Open capacity uses the derived total, so assetsDeployed counts against the
269 // ceiling: with assetsDeployed filling most of Open, the inflow is rejected.
270 auto const nearOpen = iouVault(Number{1, 9}, Number{8, 9});
271 EXPECT_EQ(checkOptionalVaultInflow(nearOpen, iouAmount(Number{1})), tecLIMIT_EXCEEDED);
272}
273
274// ---------------------------------------------------------------------------
275// adjustVaultBalances
276// ---------------------------------------------------------------------------
277
278TEST_F(VaultBalance, fixed_precision_adjust_balances)
279{
280 Number const assetsDeployed{300};
281 auto const vault = iouVault(assetsDeployed, Number{100}, Number{99});
282
283 ASSERT_EQ(
284 adjustVaultBalances(vault, {.cash = iouAmount(Number{50})}, kNullJournal), tesSUCCESS);
285 EXPECT_EQ(vault->at(sfAssetsAvailable), Number{150});
286 EXPECT_EQ(vault->at(sfAssetsDeployed), assetsDeployed);
287 EXPECT_EQ(vault->at(sfAssetsTotal), getAssetsTotal(vault));
288 EXPECT_EQ(getAssetsTotal(vault), Number{450});
289
290 ASSERT_EQ(
291 adjustVaultBalances(vault, {.cash = iouAmount(Number{-20})}, kNullJournal), tesSUCCESS);
292 EXPECT_EQ(vault->at(sfAssetsAvailable), Number{130});
293 EXPECT_EQ(vault->at(sfAssetsDeployed), assetsDeployed);
294 EXPECT_EQ(vault->at(sfAssetsTotal), getAssetsTotal(vault));
295 EXPECT_EQ(getAssetsTotal(vault), Number{430});
296
297 // Clear both rails together, the way a final VaultWithdraw does.
298 ASSERT_EQ(
300 vault,
301 {.cash = iouAmount(Number{-130}), .deployed = -Number(assetsDeployed)},
302 kNullJournal),
303 tesSUCCESS);
304 EXPECT_EQ(vault->at(sfAssetsAvailable), Number{0});
305 EXPECT_EQ(vault->at(sfAssetsDeployed), Number{0});
306 EXPECT_EQ(vault->at(sfAssetsTotal), Number{0});
307 EXPECT_EQ(getAssetsTotal(vault), Number{0});
308}
309
310TEST_F(VaultBalance, fixed_precision_add_above_coarsening_syncs)
311{
312 Number const assetsDeployed{5, -6};
313 auto const vault = iouVault(assetsDeployed, Number{9'999'999'999'999'990, -6});
314
315 ASSERT_EQ(
316 adjustVaultBalances(vault, {.cash = iouAmount(Number{10, -6})}, kNullJournal), tesSUCCESS);
317 EXPECT_EQ(vault->at(sfAssetsDeployed), assetsDeployed);
318 expectCacheFloorsExactTotal(vault);
319}
320
321// For each (balance, delta), AssetsAvailable after adjustVaultBalances must
322// equal the plain STAmount sum, the arithmetic the ledger uses for the real
323// trust-line/MPT transfer.
324TEST_F(VaultBalance, trust_line_arithmetic_mirror)
325{
326 struct Row
327 {
328 std::string_view name;
329 Number balance;
330 Number delta;
331 };
332 std::vector<Row> const rows{
333 {.name = "Plain", .balance = Number{1'000'000}, .delta = Number{500'000}},
334 {
335 .name = "CreditCrossesPowerOfTenUp",
336 .balance = Number{9'999'999'999'999'999, -9},
337 .delta = Number{2, -9},
338 },
339 {
340 .name = "DebitCrossesPowerOfTenDown",
341 .balance = Number{1'000'000'000'000'000, -9},
342 .delta = Number{-1, -9},
343 },
344 {.name = "DebitPartial", .balance = Number{5'000'000}, .delta = Number{-3'000'000}},
345 {.name = "DebitToZero", .balance = Number{2'000'000}, .delta = Number{-2'000'000}},
346 {
347 .name = "CreditFinerThanBalanceGrid",
348 .balance = Number{1'873'013'129'122'272LL, -9},
349 .delta = Number{7, -10},
350 },
351 };
352
353 for (auto const& row : rows)
354 {
355 SCOPED_TRACE(row.name);
356 STAmount const balance{iou_, row.balance};
357 STAmount const delta{iou_, row.delta};
358 STAmount const expected{iou_, Number(balance) + Number(delta)};
359
360 auto const vault = iouVault(Number{0}, row.balance, Number{0}, 9);
361 ASSERT_EQ(adjustVaultBalances(vault, {.cash = delta}, kNullJournal), tesSUCCESS);
362 EXPECT_EQ(Number(vault->at(sfAssetsAvailable)), Number(expected));
363 }
364}
365
366// Each row starts from assetsDeployed 500, available 100.
367TEST_F(VaultBalance, fields_move)
368{
369 struct Row
370 {
371 std::string_view name;
372 std::int64_t cash = 0;
373 std::int64_t deployed = 0;
374 std::int64_t yield = 0;
375 std::int64_t loss = 0;
376 std::int64_t expectedAvailable = 100;
377 std::int64_t expectedAssetsDeployed = 500;
378 std::int64_t expectedYield = 0;
379 std::int64_t expectedLoss = 0;
380 };
381 std::vector<Row> const rows{
382 {.name = "CashAlone", .cash = 25, .expectedAvailable = 125},
383 {.name = "AssetsDeployedAlone", .deployed = -200, .expectedAssetsDeployed = 300},
384 {.name = "YieldAlone", .yield = 40, .expectedYield = 40},
385 {.name = "LossAlone", .loss = 150, .expectedLoss = 150},
386 {
387 .name = "AllTogether",
388 .cash = 40,
389 .deployed = -100,
390 .yield = 10,
391 .loss = 50,
392 .expectedAvailable = 140,
393 .expectedAssetsDeployed = 400,
394 .expectedYield = 10,
395 .expectedLoss = 50,
396 },
397 };
398
399 for (auto const& row : rows)
400 {
401 SCOPED_TRACE(row.name);
402 auto const vault = standardVault();
403 ASSERT_EQ(
405 vault,
406 {.cash = iouAmount(Number{row.cash}),
407 .deployed = Number{row.deployed},
408 .yield = Number{row.yield},
409 .loss = Number{row.loss}},
410 kNullJournal),
411 tesSUCCESS);
412 EXPECT_EQ(vault->at(sfAssetsAvailable), Number{row.expectedAvailable});
413 EXPECT_EQ(vault->at(sfAssetsDeployed), Number{row.expectedAssetsDeployed});
414 EXPECT_EQ(vault->at(sfYieldUnrealized), Number{row.expectedYield});
415 EXPECT_EQ(vault->at(sfLossUnrealized), Number{row.expectedLoss});
416 EXPECT_EQ(vault->at(sfAssetsTotal), getAssetsTotal(vault));
417 }
418}
419
420// A failing field rejects the whole change as a unit, including any valid
421// cash leg. Each row starts from assetsDeployed 500, available 100.
422TEST_F(VaultBalance, invalid_change_fails_atomically)
423{
424 struct Row
425 {
426 std::string_view name;
427 std::int64_t cash = 0;
428 std::int64_t deployed = 0;
429 std::int64_t loss = 0;
430 TER expected;
431 };
432 std::vector<Row> const rows{
433 {.name = "AssetsAvailableNegative", .cash = -200, .expected = tefBAD_LEDGER},
434 {.name = "AssetsDeployedNegative", .deployed = -600, .expected = tefBAD_LEDGER},
435 // The +50 credit is valid on its own; the deployed change is not.
436 {
437 .name = "AssetsDeployedNegativeWithValidCash",
438 .cash = 50,
439 .deployed = -600,
440 .expected = tefBAD_LEDGER,
441 },
442 // The -50 debit is valid on its own (available stays at 50); the
443 // loss exceeds AssetsDeployed.
444 {
445 .name = "LossExceedsAssetsDeployedWithValidCash",
446 .cash = -50,
447 .loss = 600,
448 .expected = tecLIMIT_EXCEEDED,
449 },
450 {.name = "LossUnrealizedNegative", .loss = -1, .expected = tefBAD_LEDGER},
451 {.name = "LossExceedsAssetsDeployed", .loss = 600, .expected = tecLIMIT_EXCEEDED},
452 };
453
454 for (auto const& row : rows)
455 {
456 SCOPED_TRACE(row.name);
457 auto const vault = standardVault();
458 BalanceSnapshot const before = BalanceSnapshot::of(vault);
459
460 EXPECT_EQ(
462 vault,
463 {.cash = iouAmount(Number{row.cash}),
464 .deployed = Number{row.deployed},
465 .loss = Number{row.loss}},
466 kNullJournal),
467 row.expected);
468 EXPECT_EQ(BalanceSnapshot::of(vault), before);
469 }
470}
471
472TEST_F(VaultBalance, loss_at_assets_deployed_boundary_allowed)
473{
474 auto const vault = standardVault();
475 EXPECT_EQ(adjustVaultBalances(vault, {.loss = Number{500}}, kNullJournal), tesSUCCESS);
476 EXPECT_EQ(vault->at(sfLossUnrealized), Number{500});
477}
478
479TEST_F(VaultBalance, negative_yield_unrealized_rejected)
480{
481 auto const vault = standardVault();
482 vault->at(sfYieldUnrealized) = Number{5};
483 auto const before = BalanceSnapshot::of(vault);
484
485 EXPECT_EQ(adjustVaultBalances(vault, {.yield = Number{-20}}, kNullJournal), tefBAD_LEDGER);
486 EXPECT_EQ(BalanceSnapshot::of(vault), before);
487}
488
489TEST_F(VaultBalance, assets_total_equals_floor16_of_available_plus_assets_deployed)
490{
491 Number const assetsDeployed{7, -9};
492 auto vault = iouVault(assetsDeployed, Number{9'999'999'999'999'999, -9}, Number{0}, 9);
493
494 syncTotal(vault);
495
496 Number const expected = [&] {
498 return Number(STAmount{iou_, Number(vault->at(sfAssetsAvailable)) + assetsDeployed});
499 }();
500 EXPECT_EQ(Number(vault->at(sfAssetsTotal)), expected);
501}
502
503// ---------------------------------------------------------------------------
504// Integral assets
505// ---------------------------------------------------------------------------
506
507TEST_F(VaultBalance, xrp_control)
508{
509 checkIntegralControl(xrpIssue());
510}
511
512TEST_F(VaultBalance, mpt_control)
513{
514 checkIntegralControl(MPTIssue{makeMptID(1, issuer_.id())});
515}
516
517TEST_F(VaultBalance, credit_to_posterior_scale_ignores_ambient_rounding_mode)
518{
519 // reference is on a fine grid (scale -10); raw needs a coarser atScale once
520 // reference + raw is floored, so flooredSum - reference needs more digits
521 // than fit in a 16-digit STAmount. creditToPosteriorScale must build that
522 // difference under its own roundingMode (Downward here), not whatever
523 // rounding mode happens to be ambient when it is called.
524 Number const reference{4'218'667'505'995'833LL, -10};
525 STAmount const raw{iou_, std::int64_t{4'004'426'170'440'612LL}, -8};
526 int const atScale = -7;
527
528 // The exact (pre-STAmount) Downward credit, computed independently, floors
529 // strictly below raw: part of raw is lost rounding reference + raw onto
530 // the coarser atScale grid.
531 Number const exactDownwardCredit = [&] {
533 Number const flooredSum =
534 roundToAsset(iou_, reference + Number(raw), atScale, Number::RoundingMode::Downward);
535 return Number(STAmount{iou_, flooredSum - reference});
536 }();
537 EXPECT_LT(exactDownwardCredit, Number(raw));
538
539 // Calling under an ambient ToNearest mode (the common case: no caller sets
540 // an explicit guard around this call) must still produce the Downward
541 // result, not a value rounded under the ambient mode.
544 iou_, reference, atScale, raw, Number::RoundingMode::Downward);
545 EXPECT_LT(Number(credit), Number(raw));
546 EXPECT_EQ(Number(credit), exactDownwardCredit);
547}
548
549TEST_F(VaultBalance, credit_to_posterior_scale_just_under_power_of_ten_is_not_negative)
550{
551 // reference is all nines on the scale -10 grid and raw is just under one
552 // unit of it. The exact sum is just under 10^6, but rounds up to it to
553 // nearest. The grid must come from the sum that is floored, so the credit
554 // is zero, not negative.
555 Number const reference{9'999'999'999'999'999LL, -10};
556 Number const raw{9'970'000'001LL, -20};
557 int const baseScale = -10;
558
561 iou_, reference, baseScale, raw, Number::RoundingMode::Downward);
562 EXPECT_EQ(credit, STAmount(iou_, 0));
563}
564
565// creditToPosteriorAvailableScale returns
566// floor16(AssetsAvailable + raw) - AssetsAvailable, never finer than -Scale.
567
568TEST_F(VaultBalance, credit_to_posterior_available_scale_integral_passes_through)
569{
571 STAmount const credit =
573 EXPECT_EQ(credit, STAmount(xrpIssue(), 250));
574}
575
576TEST_F(VaultBalance, credit_to_posterior_available_scale_floors_to_base_grid)
577{
578 // Scale 6: the base grid is 1e-6, and AssetsAvailable is small enough
579 // that the posterior grid is the base grid.
580 auto const vault = iouVault(Number{0}, Number{100});
582 vault, Number{12'345'678, -7}, Number::RoundingMode::Downward);
583 EXPECT_EQ(credit, iouAmount(Number{1'234'567, -6}));
584}
585
586TEST_F(VaultBalance, credit_to_posterior_available_scale_floors_the_sum_across_power_of_ten)
587{
588 // 9999999999.999999 + 0.000011 crosses 10^10, where the 16-digit grid is
589 // 1e-5. Flooring the delta alone would give 0.00001; flooring the sum
590 // gives 10000000000.00001, so the credit is exactly 0.000011.
591 auto const vault = iouVault(Number{0}, Number{9'999'999'999'999'999, -6});
592 STAmount const credit =
594 EXPECT_EQ(credit, iouAmount(Number{11, -6}));
595 EXPECT_EQ(
596 STAmount(iou_, Number{9'999'999'999'999'999, -6} + Number(credit)),
597 iouAmount(Number{1'000'000'000'000'001, -5}));
598}
599
600TEST_F(VaultBalance, credit_to_posterior_available_scale_uses_the_exact_raw_amount)
601{
602 // raw has 17 significant digits. Exact: 0.000009 + 12345678901.234561 =
603 // 12345678901.23457 on the 1e-5 grid, so the credit is
604 // 12345678901.234561, stored as 12345678901.23456. Rounding raw to 16
605 // digits first (12345678901.23456) would floor the sum to
606 // 12345678901.23456 and give a credit one unit lower, 12345678901.23455.
607 auto const vault = iouVault(Number{0}, Number{9, -6});
609 vault, Number{12'345'678'901'234'561, -6}, Number::RoundingMode::Downward);
610 EXPECT_EQ(credit, iouAmount(Number{1'234'567'890'123'456, -5}));
611}
612
613} // namespace
614} // namespace xrpl
static Sink & getNullSink()
Returns a Sink which does nothing.
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
STAmount creditToPosteriorScale(Asset const &asset, Number const &reference, int baseScale, Number const &raw, Number::RoundingMode roundingMode)
Shared core of creditToPosteriorAvailableScale and creditToPosteriorBrokerCoverScale: floors referenc...
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
json::Value cash(jtx::Account const &dest, UInt256 const &checkId, STAmount const &amount)
Cash a check requiring that a specific amount be delivered.
Definition check.cpp:15
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:613
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
Definition STAmount.h:819
@ tefBAD_LEDGER
Definition TER.h:164
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)
TEST_F(HardenedHashTest, user_types)
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
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
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)
Definition Indexes.cpp:219
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:745
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecLIMIT_EXCEEDED
Definition TER.h:372
Number getAssetsTotal(SLE::ConstRef vault)
Return the Vault's current assets total.
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...
@ tesSUCCESS
Definition TER.h:253
T yield(T... args)