xrpld
Loading...
Searching...
No Matches
LoanFixedPrecisionBase.h
1#pragma once
2
3#include <test/app/lending/LoanTestBase.h>
4#include <test/app/vault/VaultFixedPrecisionBase.h>
5#include <test/jtx/Account.h>
6#include <test/jtx/Env.h>
7#include <test/jtx/TestHelpers.h>
8#include <test/jtx/amount.h>
9#include <test/jtx/fee.h>
10#include <test/jtx/pay.h>
11#include <test/jtx/sig.h>
12#include <test/jtx/trust.h>
13#include <test/jtx/vault.h>
14
15#include <xrpl/basics/Number.h>
16#include <xrpl/basics/chrono.h>
17#include <xrpl/protocol/Indexes.h>
18#include <xrpl/protocol/Keylet.h>
19#include <xrpl/protocol/SField.h>
20#include <xrpl/protocol/SeqProxy.h>
21#include <xrpl/protocol/Units.h>
22
23#include <chrono>
24#include <cstdint>
25#include <optional>
26#include <string>
27#include <vector>
28
29namespace xrpl {
30
31// Shared scaffolding for the lending FixedPrecision test suites
32// (LoanSetFixedPrecision, and, via LoanPayFixedPrecisionBase /
33// LoanManageFixedPrecisionBase, LoanPayFixedPrecision and
34// LoanManageFixedPrecision): closed-ended lending vault and loan fixtures.
35// Builds on the vault-only VaultFixedPrecisionBase instead of duplicating its
36// account setup and vault assertions. Keep this header to what LoanSet-level
37// tests need; the coarsened-vault fixture and the default/impair helpers live
38// in the derived bases below so a LoanSet-only test file does not carry
39// LoanPay/LoanManage-only scaffolding.
41{
42protected:
43 // Funds holder and gives it a USD trust line with the given balance.
44 static void
46 test::jtx::Env& env,
47 test::jtx::Account const& holder,
48 AssetAccounts const& accounts,
49 Number const& trustLimit,
50 Number const& funds)
51 {
52 using namespace test::jtx;
53
54 env.fund(XRP(1'000'000), holder);
55 env.close();
56 env(trust(holder, accounts.asset(trustLimit)));
57 env.close();
58 env(pay(accounts.issuer, holder, accounts.asset(funds)));
59 env.close();
60 }
61
62 // A closed-ended vault (required by LoanBrokerSet) with a deposit booked,
63 // advanced into the Investment phase, with a LoanBroker attached. The vault
64 // version follows the amendments active in env. redemptionDate is when
65 // VaultWithdraw becomes possible; tests that withdraw must advance past it.
66 // Vault holds an Env&, so the struct is aggregate-initialized by
67 // setupLendingVault.
69 {
74 // The broker owner (loan counterparty) and the depositor, who borrows in
75 // openLoan.
78 };
79
80 // Cover rates default to 0%, so no cover deposit is needed; pass non-zero
81 // values to exercise First-Loss Capital. scale overrides the default sfScale.
82 // managementFeeRate defaults to 0%, so interest due equals gross interest.
83 static LendingFixture
85 test::jtx::Env& env,
86 test::jtx::Account const& owner,
87 test::jtx::Account const& depositor,
88 PrettyAsset const& asset,
89 Number const& depositAmount,
90 std::chrono::seconds investmentWindow = std::chrono::seconds{1'000'000},
92 TenthBips32 coverRateMinimum = TenthBips32(0),
93 TenthBips32 coverRateLiquidation = TenthBips32(0),
94 TenthBips16 managementFeeRate = TenthBips16(0))
95 {
96 using namespace test::jtx;
97 using namespace loan_broker;
98
99 Vault const vault{env};
100 auto [tx, vaultKeylet, subscriptionDate] = vault.createClosedEnded({
101 .owner = owner,
102 .asset = asset,
103 .subscriptionOffset = std::chrono::seconds{60},
104 .investmentWindow = investmentWindow,
105 });
106 if (scale)
107 tx[sfScale] = *scale;
108 env(tx);
109 env.close();
110
111 env(vault.deposit(
112 {.depositor = depositor, .id = vaultKeylet.key, .amount = asset(depositAmount)}));
113 env.close();
114
115 vault.closePastSubscription(subscriptionDate);
116
117 auto const brokerKeylet =
118 keylet::loanBroker(owner.id(), SeqProxy::rawSequence(env.seq(owner)));
119 env(set(owner, vaultKeylet.key),
120 kCoverRateMinimum(coverRateMinimum),
121 kCoverRateLiquidation(coverRateLiquidation),
122 kManagementFeeRate(managementFeeRate));
123 env.close();
124
125 return {
126 .vault = vault,
127 .vaultKeylet = vaultKeylet,
128 .brokerKeylet = brokerKeylet,
129 .redemptionDate = subscriptionDate + investmentWindow,
130 .owner = owner,
131 .depositor = depositor,
132 };
133 }
134
135 // Submits a LoanSet for principal against the fixture's broker, borrowed by
136 // fixture.depositor and countersigned by fixture.owner, using the broker's
137 // current LoanSequence. Interest defaults to 0%, so payments are pure
138 // principal; flags pass through to LoanSet (e.g. tfLoanOverpayment).
139 static Keylet
141 test::jtx::Env& env,
142 LendingFixture const& fixture,
143 Number const& principal,
144 std::uint32_t paymentTotal,
145 TenthBips32 interestRate = TenthBips32(0),
146 std::uint32_t flags = 0,
147 std::uint32_t paymentInterval = 120,
148 std::uint32_t gracePeriod = 60)
149 {
150 using namespace test::jtx;
151 using namespace loan;
152
153 auto const brokerSle = env.le(fixture.brokerKeylet);
154 auto const loanKeylet = keylet::loan(
155 fixture.brokerKeylet.key, SeqProxy::rawSequence(brokerSle->at(sfLoanSequence)));
156 env(set(fixture.depositor, fixture.brokerKeylet.key, principal, flags),
157 kInterestRate(interestRate),
158 kGracePeriod(gracePeriod),
159 kPaymentInterval(paymentInterval),
160 kPaymentTotal(paymentTotal),
161 Sig(sfCounterpartySignature, fixture.owner),
162 Fee(env.current()->fees().base * 2));
163 env.close();
164 return loanKeylet;
165 }
166
167 // Forwards to the shared checkFixedPrecisionVaultAssetsDeployed
168 // (LoanTestBase.h): AssetsDeployed against broker and loan sums, stored against
169 // derived AssetsTotal, and AssetsAvailable against the pseudo-account balance.
170 void
172 test::jtx::Env& env,
173 LendingFixture const& fixture,
174 std::vector<Keylet> const& loanKeylets,
175 std::string const& label = {})
176 {
178 *this, env, fixture.vaultKeylet, {fixture.brokerKeylet}, loanKeylets, label);
179 }
180};
181
182} // namespace xrpl
static LendingFixture setupLendingVault(test::jtx::Env &env, test::jtx::Account const &owner, test::jtx::Account const &depositor, PrettyAsset const &asset, Number const &depositAmount, std::chrono::seconds investmentWindow=std::chrono::seconds{1 '000 '000}, std::optional< std::uint8_t > scale=std::nullopt, TenthBips32 coverRateMinimum=TenthBips32(0), TenthBips32 coverRateLiquidation=TenthBips32(0), TenthBips16 managementFeeRate=TenthBips16(0))
static void fundIouHolder(test::jtx::Env &env, test::jtx::Account const &holder, AssetAccounts const &accounts, Number const &trustLimit, Number const &funds)
void checkVaultLoanSums(test::jtx::Env &env, LendingFixture const &fixture, std::vector< Keylet > const &loanKeylets, std::string const &label={})
static Keylet openLoan(test::jtx::Env &env, LendingFixture const &fixture, Number const &principal, std::uint32_t paymentTotal, TenthBips32 interestRate=TenthBips32(0), std::uint32_t flags=0, std::uint32_t paymentInterval=120, std::uint32_t gracePeriod=60)
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
test::jtx::PrettyAsset PrettyAsset
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
AccountID id() const
Returns the Account ID.
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
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
Keylet loan(UInt256 const &loanBrokerID, SeqProxy const &loanSeq) noexcept
Definition Indexes.cpp:577
void checkFixedPrecisionVaultAssetsDeployed(beast::unit_test::Suite &suite, jtx::Env &env, Keylet const &vaultKeylet, std::vector< Keylet > const &brokerKeylets, std::vector< Keylet > const &loanKeylets, std::string const &label={})
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
Definition STAmount.h:819
TenthBips< std::uint32_t > TenthBips32
Definition Units.h:454
TenthBips< std::uint16_t > TenthBips16
Definition Units.h:453
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21