xrpld
Loading...
Searching...
No Matches
LoanBrokerCoverTests.cpp
1#include <xrpl/basics/Number.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/ledger/helpers/LendingHelpers.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/STTakesAsset.h>
14#include <xrpl/protocol/TER.h>
15#include <xrpl/protocol/UintTypes.h>
16
17#include <gtest/gtest.h>
18#include <helpers/Account.h>
19#include <protocol/VaultTestHelpers.h>
20
21#include <cstdint>
22#include <memory>
23
24namespace xrpl {
25namespace {
26
27// Minimal bare LoanBroker SLE for helpers that only touch sfCoverAvailable
28// and the broker's asset (e.g. creditToPosteriorBrokerCoverScale,
29// getPosteriorBrokerCoverScale). Not a valid ledger entry on its own -- just
30// enough for the helper's own field reads and type asserts.
31std::shared_ptr<SLE>
32makeBroker(Asset const& asset, Number const& coverAvailable)
33{
35 broker->at(sfCoverAvailable) = coverAvailable;
36 associateAsset(*broker, asset);
37 return broker;
38}
39
40TEST(LoanBrokerCover, broker_cover_scale_and_rounding)
41{
42 test::Account const issuer{"issuer"};
43 Issue const iou{toCurrency("USD"), issuer.id()};
44 auto const vault = makeVault(iou, Number{0}, VaultVersion::FixedPrecision, 6);
45 auto const broker = makeBroker(iou, Number{9'999'999'999'999'999, -6});
46 STAmount const inflow{iou, Number{21, -6}};
47
48 EXPECT_EQ(detail::getPosteriorBrokerCoverScale(vault, broker, inflow), -5);
49 EXPECT_EQ(
51 vault, broker, inflow, Number::RoundingMode::TowardsZero),
52 STAmount(iou, Number{20, -6}));
53
54 broker->at(sfCoverAvailable) = Number{1'000'000'000'000'001, -5};
55 associateAsset(*broker, iou);
56 STAmount const outflow{iou, -Number{11, -6}};
57 EXPECT_EQ(detail::getPosteriorBrokerCoverScale(vault, broker, outflow), -6);
58 EXPECT_EQ(
60 vault, broker, outflow, Number::RoundingMode::TowardsZero),
61 outflow);
62
63 // CoverAvailable exactly 1e10 (exponent -5). Withdrawing 1e-6 re-fines
64 // to -6; the posterior rounded amount is 1e-6, which isZeroAtScale(-5)
65 // would treat as zero.
66 broker->at(sfCoverAvailable) = Number{1, 10};
67 associateAsset(*broker, iou);
68 STAmount const refine{iou, -Number{1, -6}};
69 EXPECT_EQ(detail::getPosteriorBrokerCoverScale(vault, broker, refine), -6);
70 EXPECT_EQ(
72 vault, broker, refine, Number::RoundingMode::TowardsZero),
73 refine);
74}
75
76TEST(LoanBrokerCover, broker_cover_optional_inflow_boundaries)
77{
78 test::Account const issuer{"issuer"};
79 Issue const iou{toCurrency("USD"), issuer.id()};
80
81 auto const fixedIou = makeVault(iou, Number{0}, VaultVersion::FixedPrecision, 10);
82 auto const iouBroker = makeBroker(iou, Number{9, 5});
83 STAmount const iouUnit{iou, Number{1, -10}};
84 EXPECT_EQ(checkOptionalBrokerCoverInflow(fixedIou, iouBroker, STAmount{iou}), tesSUCCESS);
85 EXPECT_EQ(checkOptionalBrokerCoverInflow(fixedIou, iouBroker, iouUnit), tecLIMIT_EXCEEDED);
86
87 auto const legacy = makeVault(iou, Number{0}, VaultVersion::CashBasis, 10);
88 EXPECT_EQ(checkOptionalBrokerCoverInflow(legacy, iouBroker, iouUnit), tesSUCCESS);
89
90 for (Asset const asset : {Asset{xrpIssue()}, Asset{MPTIssue{makeMptID(1, issuer.id())}}})
91 {
92 auto const vault = makeVault(asset, Number{0}, VaultVersion::FixedPrecision, 0);
93 auto const broker = makeBroker(asset, Number{9, 15});
94 EXPECT_EQ(
95 checkOptionalBrokerCoverInflow(vault, broker, STAmount{asset, std::uint64_t{1}}),
97 }
98}
99
100TEST(LoanBrokerCover, broker_cover_optional_inflow_scale_mismatch)
101{
102 // Base scale 6: CoverAvailable already carries 16 significant digits, so
103 // any positive inflow needs a coarser posterior scale than the broker's
104 // base scale. CoverAvailable (2e10) is also over the Open limit (9e9), so
105 // both checks reject this input; the test checks that the scale-mismatch
106 // check rejects it first. No input trips the scale check alone: anything
107 // under 9e9 fits in 16 digits at the base scale.
108 test::Account const issuer{"issuer"};
109 Issue const iou{toCurrency("USD"), issuer.id()};
110 auto const vault = makeVault(iou, Number{0}, VaultVersion::FixedPrecision, 6);
111 auto const broker = makeBroker(iou, Number{2, 10});
112 STAmount const amount{iou, Number{1, -5}};
113 EXPECT_EQ(checkOptionalBrokerCoverInflow(vault, broker, amount), tecLIMIT_EXCEEDED);
114}
115
116TEST(LoanBrokerCover, credit_to_posterior_broker_cover_scale_at_to_nearest_carry_cusp)
117{
118 // Base scale 6; CoverAvailable already has 16 significant digits, one
119 // unit below the next power of ten. Number holds 19 digits, so the raw
120 // sum CoverAvailable + raw is exact and never carries; the Downward
121 // conversion to STAmount floors it at the unchanged base exponent. A
122 // sub-unit raw amount (below the scale-6 grid) therefore credits
123 // exactly zero.
124 test::Account const issuer{"issuer"};
125 Issue const iou{toCurrency("USD"), issuer.id()};
126 Number const coverAvailable{9'999'999'999'999'999, -6};
127 auto const vault =
129 auto const broker = makeBroker(iou, coverAvailable);
130
131 STAmount const raw{iou, 5, -7}; // 0.0000005
132
133 STAmount const credit =
135
136 // Sub-unit raw amount at the unchanged base exponent credits nothing.
137 EXPECT_EQ(Number(credit), Number{0});
138
139 // CA + credit must stay representable in 16 digits.
140 Number const total = Number(coverAvailable) + Number(credit);
141 EXPECT_EQ(Number(STAmount{iou, total}), total);
142}
143
144} // namespace
145} // namespace xrpl
A currency issued by an account.
Definition Issue.h:18
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
T make_shared(T... args)
STAmount roundToPosteriorBrokerCoverScale(SLE::ConstRef vault, SLE::ConstRef broker, STAmount const &delta, Number::RoundingMode roundingMode)
Round a cover outflow delta (cover withdraw, cover clawback, the broker-side decrease of a default's ...
int getPosteriorBrokerCoverScale(SLE::ConstRef vault, SLE::ConstRef broker, Number const &delta)
Return a LoanBroker's posterior live cover exponent after applying an unrounded delta.
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:571
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
TEST(FileUtilitiesTest, get_file_contents)
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)
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
TER checkOptionalBrokerCoverInflow(SLE::ConstRef vault, SLE::ConstRef broker, STAmount const &amount)
Check whether amount is an admissible optional cover inflow.
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
@ tecLIMIT_EXCEEDED
Definition TER.h:372
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
@ tesSUCCESS
Definition TER.h:253
UInt256 uint256
Definition base_uint.h:587
STAmount creditToPosteriorBrokerCoverScale(SLE::ConstRef vault, SLE::ConstRef broker, Number const &raw, Number::RoundingMode roundingMode)
Round a cover inflow (cover deposit, LoanPay's fee redirect into cover) into CoverAvailable by floori...