Skip to content

Lending Protocol implementation XLS-66 #5270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 48 commits into
base: ximinez/lending-refactoring-4
Choose a base branch
from

Conversation

ximinez
Copy link
Collaborator

@ximinez ximinez commented Jan 31, 2025

High Level Overview of Change

Implement the Lending Protocol as described in XLS-66

Context of Change

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Refactor (non-breaking change that only restructures code)
  • Tests (you added tests for code that already exists, or your new feature included in this PR)

API Impact

  • Public API: New feature (new methods and/or new fields)
  • Public API: Breaking change (in general, breaking changes should only impact the next api_version)
  • libxrpl change (any change that may affect libxrpl or dependents of libxrpl)
  • Peer protocol change (must be backward compatible or bump the peer protocol version)

@ximinez ximinez force-pushed the ximinez/lending-XLS-66 branch 4 times, most recently from 1240135 to 6f5d1ad Compare February 4, 2025 19:35
Copy link

codecov bot commented Feb 4, 2025

Codecov Report

❌ Patch coverage is 90.59544% with 169 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.1%. Comparing base (3b446f1) to head (41c2409).

Files with missing lines Patch % Lines
src/xrpld/app/misc/LendingHelpers.h 77.0% 37 Missing ⚠️
...rc/xrpld/app/tx/detail/LoanBrokerCoverWithdraw.cpp 80.2% 22 Missing ⚠️
src/xrpld/app/tx/detail/InvariantCheck.cpp 90.5% 19 Missing ⚠️
src/xrpld/app/tx/detail/LoanSet.cpp 93.2% 14 Missing ⚠️
src/xrpld/app/tx/detail/LoanBrokerDelete.cpp 82.6% 12 Missing ⚠️
src/xrpld/app/tx/detail/LoanManage.cpp 94.3% 9 Missing ⚠️
src/xrpld/app/tx/detail/LoanPay.cpp 93.1% 8 Missing ⚠️
...rc/xrpld/app/tx/detail/LoanBrokerCoverClawback.cpp 95.0% 7 Missing ⚠️
src/xrpld/app/tx/detail/LoanDelete.cpp 88.5% 7 Missing ⚠️
src/xrpld/app/misc/detail/LendingHelpers.cpp 79.3% 6 Missing ⚠️
... and 8 more
Additional details and impacted files

Impacted file tree graph

@@                       Coverage Diff                       @@
##           ximinez/lending-refactoring-4   #5270     +/-   ##
===============================================================
+ Coverage                           78.8%   79.1%   +0.3%     
===============================================================
  Files                                814     837     +23     
  Lines                              71102   72716   +1614     
  Branches                            8359    8411     +52     
===============================================================
+ Hits                               56007   57493   +1486     
- Misses                             15095   15223    +128     
Files with missing lines Coverage Δ
include/xrpl/protocol/Indexes.h 100.0% <100.0%> (ø)
include/xrpl/protocol/Protocol.h 100.0% <100.0%> (ø)
include/xrpl/protocol/SField.h 100.0% <ø> (ø)
include/xrpl/protocol/STAmount.h 95.8% <100.0%> (+0.3%) ⬆️
include/xrpl/protocol/STObject.h 93.1% <ø> (ø)
include/xrpl/protocol/STTx.h 100.0% <100.0%> (ø)
include/xrpl/protocol/detail/ledger_entries.macro 100.0% <100.0%> (ø)
include/xrpl/protocol/detail/transactions.macro 100.0% <100.0%> (ø)
src/libxrpl/basics/Number.cpp 99.5% <ø> (ø)
src/libxrpl/protocol/Indexes.cpp 97.0% <100.0%> (+0.1%) ⬆️
... and 41 more

... and 12 files with indirect coverage changes

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ximinez ximinez force-pushed the ximinez/lending-XLS-66 branch 3 times, most recently from 27e4c4f to 2d209c4 Compare February 8, 2025 00:58
@ximinez ximinez force-pushed the ximinez/lending-XLS-66 branch 5 times, most recently from e8fbd22 to a697138 Compare February 28, 2025 19:24
@ximinez ximinez force-pushed the ximinez/lending-XLS-66 branch 7 times, most recently from 72d979e to 441d5b5 Compare March 17, 2025 20:23
@ximinez ximinez changed the base branch from develop to ximinez/Bronek/vault March 17, 2025 22:52
@ximinez ximinez force-pushed the ximinez/Bronek/vault branch from b1e091c to 2a8861d Compare March 19, 2025 00:44
@ximinez ximinez force-pushed the ximinez/lending-XLS-66 branch 8 times, most recently from 5223459 to c7b296c Compare March 25, 2025 16:30
@ximinez ximinez changed the base branch from develop to ximinez/lending-refactoring-4 July 23, 2025 16:07
@ximinez ximinez force-pushed the ximinez/lending-XLS-66 branch from 038b776 to 1e74626 Compare July 23, 2025 21:27
…to ximinez/lending-XLS-66

* XRPLF/ximinez/lending-refactoring-4:
  fixup! Rename Transactor preflight functions
  Rename Transactor preflight functions
  fixup! Make preflight1 and preflight2 private static Transactor functions
  Make preflight1 and preflight2 private static Transactor functions
  Fix formatting
@gregtatcam
Copy link
Collaborator

There are build errors in the latest commit:

/User/rippled/src/test/app/Loan_test.cpp:1816:25: error: use of undeclared identifier 'json'
1816 | createJson, json(sfCounterpartySignature, Json::objectValue));
| ^
/User/rippled/src/test/app/Loan_test.cpp:1829:25: error: use of undeclared identifier 'json'
1829 | createJson, json(sfCounterpartySignature, counterpartyJson));
| ^

@ximinez
Copy link
Collaborator Author

ximinez commented Jul 24, 2025

There are build errors in the latest commit:

/User/rippled/src/test/app/Loan_test.cpp:1816:25: error: use of undeclared identifier 'json' 1816 | createJson, json(sfCounterpartySignature, Json::objectValue)); | ^ /User/rippled/src/test/app/Loan_test.cpp:1829:25: error: use of undeclared identifier 'json' 1829 | createJson, json(sfCounterpartySignature, counterpartyJson)); | ^

Unity build strikes again. I'll have it fixed ASAP.

@ximinez ximinez force-pushed the ximinez/lending-XLS-66 branch from 91e0286 to 57c78c5 Compare July 24, 2025 19:08
loan(uint256 const& loanBrokerID, std::uint32_t loanSeq) noexcept;

inline Keylet
loan(uint256 const& vaultKey)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right

loanbroker(AccountID const& owner, std::uint32_t seq) noexcept;

inline Keylet
loanbroker(uint256 const& vaultKey)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right

}
else
{
auto const vaultID = tx[sfVaultID];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sfVaultID is also used on line 98. It makes sense to define vaultID at the start of the function and use it in both cases.

adjustOwnerCount(view(), owner, -2, j_);
}

return tesSUCCESS;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifications has State Changes section describing the balances update by CoverAvailable. Why is this state change requirements not implemented?

auto const pseudoAccountID = sleBroker->at(sfAccount);

// Cannot send a frozen Asset
if (auto const ret = checkFrozen(ctx.view, pseudoAccountID, vaultAsset))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns tecFROZEN if lsfGlobalFreeze is set on the issuer's account for IOU. It returns tecLOCKED if MPTokenIssuance has lsfMPTLocked set. Therefore, the tx fails. But if destination account is the issuer account then withdraw should succeed. This is fail condition according to the specs:

The AccountRoot object of the Issuer has the lsfGlobalFreeze flag set and Destination is not the Issuer of the asset.

The MPTokenIssuance object of the Vault(LoanBroker(LoanBrokerID).VaultID).Asset has the lsfMPTLocked flag set and Destination is not the Issuer of the asset.

}

// Transfer assets from pseudo-account to depositor.
return accountSend(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've already made a payment of amount above from brokerPseudoID to dstAcct. What is this for?

TRANSACTION(ttLOAN_BROKER_COVER_CLAWBACK, 78, LoanBrokerCoverClawback,
Delegation::delegatable,
noPriv, ({
{sfLoanBrokerID, soeOPTIONAL},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these two fields optional? They are required according to the specs.

*/
//==============================================================================

#include <xrpld/app/tx/detail/LoanBrokerSet.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are quite a few unnecessary include files in every loan related transactor. I only mention it here as an example. The unnecessary includes are commented out:

    #include <xrpld/app/tx/detail/LoanBrokerSet.h>
    //
    #include <xrpld/app/misc/LendingHelpers.h>
    //#include <xrpld/app/tx/detail/SignerEntries.h>
    //#include <xrpld/app/tx/detail/VaultCreate.h>
    #include <xrpld/ledger/ApplyView.h>
    #include <xrpld/ledger/View.h>
    
    #include <xrpl/basics/Log.h>
    #include <xrpl/basics/Number.h>
    //#include <xrpl/basics/chrono.h>
    //#include <xrpl/beast/utility/Journal.h>
    //#include <xrpl/beast/utility/instrumentation.h>
    //#include <xrpl/protocol/AccountID.h>
    //#include <xrpl/protocol/Feature.h>
    #include <xrpl/protocol/Indexes.h>
    //#include <xrpl/protocol/PublicKey.h>
    #include <xrpl/protocol/SField.h>
    //#include <xrpl/protocol/STAmount.h>
    //#include <xrpl/protocol/STNumber.h>
    //#include <xrpl/protocol/STObject.h>
    //#include <xrpl/protocol/STXChainBridge.h>
    #include <xrpl/protocol/TER.h>
    //#include <xrpl/protocol/TxFlags.h>
    //#include <xrpl/protocol/XRPAmount.h>

{
// Create the holding if it doesn't already exist (necessary for MPTs).
// The owner may have deleted their MPT / line at some point.
if (auto const ter = addEmptyHolding(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this applicable only when originationFee is set?

return temINVALID;

// Flags are mutually exclusive
int numFlags = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can simplify:

auto const flags = ctx.tx[sfFlags];
if (flags & (flags - 1)) != 0)
{
    JLOG(ctx.j.warn())
        << "LoanManage: Only one of tfLoanDefault, tfLoanImpair, or "
           "tfLoanUnimpair can be set.";
    return temINVALID_FLAG;
}

j_))
return ter;
}
if (tx.isFlag(tfLoanImpair))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flags are mutually exclusive so should be else if here and below.

(365 * 24 * 60 * 60);
}

Number
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there corresponding sections or formulas in the specifications for all of the functions here and in *cpp? It's not easy to verify without a reference. Comments pointing to a location in the specifications would be very helpful.

ximinez added 2 commits July 28, 2025 19:04
…to ximinez/lending-XLS-66

* XRPLF/ximinez/lending-refactoring-4:
  Build options cleanup (#5581)
  Updates Conan dependencies: Boost 1.86 (#5264)
  VaultWithdraw destination account bugfix (#5572)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Amendment DraftRunCI Normally CI does not run on draft PRs. This opts in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants