1#include <test/app/lending/LoanTestBase.h>
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/JTx.h>
5#include <test/jtx/TestHelpers.h>
6#include <test/jtx/amount.h>
7#include <test/jtx/credentials.h>
8#include <test/jtx/fee.h>
9#include <test/jtx/pay.h>
10#include <test/jtx/permissioned_domains.h>
11#include <test/jtx/sig.h>
12#include <test/jtx/ter.h>
13#include <test/jtx/trust.h>
15#include <xrpl/basics/Number.h>
16#include <xrpl/basics/base_uint.h>
17#include <xrpl/beast/unit_test/suite.h>
18#include <xrpl/beast/utility/Zero.h>
19#include <xrpl/protocol/Feature.h>
20#include <xrpl/protocol/Indexes.h>
21#include <xrpl/protocol/Keylet.h>
22#include <xrpl/protocol/LedgerFormats.h>
23#include <xrpl/protocol/SField.h>
24#include <xrpl/protocol/TER.h>
25#include <xrpl/protocol/TxFlags.h>
26#include <xrpl/protocol/Units.h>
53 env.
fund(
XRP(1'000'000), account);
55 env(
trust(account, asset(1'000'000'000)));
56 env(
pay(issuer, account, asset(1'000'000)));
83 Sig(sfCounterpartySignature, counterparty)(env, setTx);
84 Fee{env.
current()->fees().base * 2}(env, setTx);
105 auto const sleLoanBefore = env.
le(loanKeylet);
106 if (!BEAST_EXPECT(sleLoanBefore))
108 auto const balanceBefore = env.
balance(borrower, asset).
value();
109 auto const paymentRemainingBefore = sleLoanBefore->at(sfPaymentRemaining);
110 auto const principalBefore = sleLoanBefore->at(sfPrincipalOutstanding);
111 auto const totalValueBefore = sleLoanBefore->at(sfTotalValueOutstanding);
116 auto const sleLoan = env.
le(loanKeylet);
117 if (!BEAST_EXPECT(sleLoan))
119 BEAST_EXPECT(env.
balance(borrower, asset).
value() < balanceBefore);
120 BEAST_EXPECT(sleLoan->at(sfPaymentRemaining) == paymentRemainingBefore - 1);
121 BEAST_EXPECT(sleLoan->at(sfPrincipalOutstanding) < principalBefore);
122 BEAST_EXPECT(sleLoan->at(sfTotalValueOutstanding) < totalValueBefore);
128 testcase(
"Private broker gates new loans on its permissioned domain");
132 Account const issuer{
"issuer"};
136 Account const credIssuer{
"credIssuer"};
139 Env env{*
this,
all_ | featureLendingProtocolV1_2};
143 env.
fund(
XRP(1'000'000), credIssuer);
152 env, asset, alice, {.flags = tfLoanBrokerPrivate, .domainID = domainId});
154 auto const setBrokerDomain = [&](
uint256 const& newDomainId) {
155 env(
set(alice, broker.vaultID), kLoanBrokerId(broker.brokerID), kDomainId(newDomainId));
172 .name =
"DomainID set",
174 .expectedDomainId = domainId,
179 .name =
"DomainID unset",
181 .expectedDomainId = std::nullopt,
186 .name =
"DomainID set again",
187 .changeState = [&] { setBrokerDomain(domainId); },
188 .expectedDomainId = domainId,
193 .name =
"Domain deleted while the broker still refers to it",
200 .expectedDomainId = domainId,
205 .name =
"Stale DomainID cleared by the broker owner",
207 .expectedDomainId = std::nullopt,
215 auto const trySetLoan = [&](
Account const& borrower,
222 BEAST_EXPECT(
static_cast<bool>(env.
le(loanKeylet)) == created);
230 for (
auto const& step : steps)
234 if (step.changeState)
237 if (
auto const sleBroker = env.
le(broker.brokerKeylet()); BEAST_EXPECT(sleBroker))
239 BEAST_EXPECT(sleBroker->at(~sfDomainID) == step.expectedDomainId);
240 BEAST_EXPECT(sleBroker->isFlag(lsfLoanBrokerPrivate));
247 trySetLoan(carol, step.nonMemberResult);
248 outstandingLoan = trySetLoan(bob, step.memberResult);
255 testcase(
"Private Broker Credential Validation");
258 Account const issuer{
"issuer"};
262 Account const credIssuer{
"credIssuer"};
265 Env env{*
this,
all_ | featureLendingProtocolV1_2};
269 env.
fund(
XRP(1'000'000), credIssuer);
275 env, asset, alice, {.flags = tfLoanBrokerPrivate, .domainID = domainId});
278 testcase(
"Borrower without credentials cannot create loan");
285 testcase(
"Borrower with valid credentials can create loan");
290 BEAST_EXPECT(env.
le(loanKeylet));
294 testcase(
"Private broker without DomainID configured rejects loans");
295 auto const noDomainBroker =
303 testcase(
"Public broker allows anyone to create loan");
311 BEAST_EXPECT(env.
le(loanKeylet));
316 "When the broker owner submits, the domain check must validate the borrower (the "
322 testcase(
"Broker owner submits with a credentialed borrower (bob) as counterparty.");
327 BEAST_EXPECT(env.
le(loanKeylet));
331 testcase(
"Broker owner outside the domain cannot take a loan from their own broker");
336 BEAST_EXPECT(!env.
le(loanKeylet));
340 testcase(
"Borrower whose only credential has expired");
341 using namespace std::chrono_literals;
345 env.
current()->header().parentCloseTime.time_since_epoch().count() + 100;
346 jv[sfExpiration.jsonName] = expiration;
355 BEAST_EXPECT(env.
le(credKeylet));
361 BEAST_EXPECT(!env.
le(credKeylet));
372 testcase(
"Private broker does not treat the domain owner as a member");
375 Account const issuer{
"issuer"};
379 Account const credIssuer{
"credIssuer"};
382 Env env{*
this,
all_ | featureLendingProtocolV1_2};
386 env.
fund(
XRP(1'000'000), credIssuer);
395 env, asset, alice, {.flags = tfLoanBrokerPrivate, .domainID = domainId});
398 testcase(
"Owning the domain does not make the owner a member");
403 BEAST_EXPECT(!env.
le(loanKeylet));
407 testcase(
"The domain owner is not a member as the counterparty either");
412 BEAST_EXPECT(!env.
le(loanKeylet));
416 testcase(
"Anyone else without a credential is rejected the same way");
424 testcase(
"The domain owner can borrow once they hold a credential");
429 BEAST_EXPECT(env.
le(loanKeylet));
433 testcase(
"The credentialed domain owner can also borrow as the counterparty");
438 BEAST_EXPECT(env.
le(loanKeylet));
442 testcase(
"Broker owner who also owns the domain");
445 env, asset, alice, {.flags = tfLoanBrokerPrivate, .domainID = aliceDomainId});
456 BEAST_EXPECT(!env.le(loanKeylet));
467 BEAST_EXPECT(env.le(loanKeylet));
472 testcase(
"Once the domain is deleted, the former owner gets tecOBJECT_NOT_FOUND");
486 testcase(
"LoanPay works after borrower credential is revoked");
489 Account const issuer{
"issuer"};
492 Account const credIssuer{
"credIssuer"};
495 Env env{*
this,
all_ | featureLendingProtocolV1_2};
498 env.
fund(
XRP(1'000'000), credIssuer);
505 env, asset, alice, {.flags = tfLoanBrokerPrivate, .domainID = domainId});
511 BEAST_EXPECT(env.
le(loanKeylet));
519 BEAST_EXPECT(!env.
le(credKeylet));
529 BEAST_EXPECT(!env.
le(newLoanKeylet));
536 testcase(
"LoanPay works after borrower credential has expired");
538 using namespace std::chrono_literals;
540 Account const issuer{
"issuer"};
543 Account const credIssuer{
"credIssuer"};
546 Env env{*
this,
all_ | featureLendingProtocolV1_2};
549 env.
fund(
XRP(1'000'000), credIssuer);
554 env, asset, alice, {.flags = tfLoanBrokerPrivate, .domainID = domainId});
560 env.
current()->header().parentCloseTime.time_since_epoch().count() + 20;
561 jv[sfExpiration.jsonName] = expiration;
570 BEAST_EXPECT(env.
le(loanKeylet));
575 BEAST_EXPECT(env.
le(credKeylet));
582 BEAST_EXPECT(env.
le(credKeylet));
590 BEAST_EXPECT(!env.
le(newLoanKeylet));
TestcaseT testcase
Memberspace for declaring test cases.
A step in a payment path.
std::string const credType_
void testPrivateBrokerLoanPayAfterCredentialRevoked()
void issueCredential(jtx::Env &env, jtx::Account const &subject, jtx::Account const &credIssuer)
void testPrivateBrokerLoanPayAfterCredentialExpired()
static jtx::JTx makeLoanSet(jtx::Env &env, jtx::Account const &submitter, BrokerInfo const &broker, jtx::Account const &counterparty)
void testPrivateBrokerDomainOwner()
void testPrivateBrokerDomainStates()
void payLoanAndVerify(jtx::Env &env, jtx::Account const &borrower, Keylet const &loanKeylet, jtx::PrettyAsset const &asset)
void testPrivateBrokerCredentials()
void run() override
Runs the suite.
static void fundBorrower(jtx::Env &env, jtx::Account const &issuer, jtx::Account const &account, jtx::PrettyAsset const &asset)
Shared base for the Loan*_test family under src/test/app/lending/.
jtx::PrettyAsset createFundedIouAsset(jtx::Env &env, jtx::Account const &issuer, jtx::Account const &lender, jtx::Account const &borrower, Number const &lenderPay=100 '000 '000, Number const &borrowerPay=1 '000 '000)
BrokerInfo createVaultAndBroker(jtx::Env &env, jtx::PrettyAsset const &asset, jtx::Account const &lender, BrokerParameters const ¶ms=BrokerParameters::defaults())
Keylet nextLoanKeylet(jtx::Env const &env, BrokerInfo const &broker)
uint256 createDomain(jtx::Env &env, jtx::Account const &owner, jtx::Account const &credIssuer, std::string const &credType)
Immutable cryptographic account descriptor.
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
SLE::const_pointer le(Account const &account) const
Return an account root.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Set the regular signature on a JTx.
Set the expected result code for a JTx The test will fail if the code doesn't match.
Keylet permissionedDomain(AccountID const &account, SeqProxy const &seq) noexcept
json::Value deleteCred(jtx::Account const &acc, jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
json::Value accept(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
json::Value create(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Keylet keylet(test::jtx::Account const &subject, test::jtx::Account const &issuer, std::string_view credType)
json::Value set(AccountID const &account, UInt256 const &loanBrokerID, Number principalRequested, std::uint32_t flags)
auto const kPaymentInterval
json::Value pay(AccountID const &account, UInt256 const &loanID, STAmount const &amount, std::uint32_t flags)
json::Value deleteTx(AccountID const &account, UInt256 const &domain)
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
XrpT const XRP
Converts to XRP Issue or STAmount.
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool set(T &target, std::string const &name, Section const §ion)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
TenthBips< std::uint32_t > TenthBips32
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
A pair of SHAMap key and LedgerEntryType.
Execution context for applying a JSON transaction.
STAmount const & value() const