Skip to content
Last updated
Edit

This tutorial shows you how to create a LoanBroker on the XRP Ledger using a private vault. A loan broker creates and manages loans, and also manages the first-loss capital for a connected single asset vault.

Requires the LendingProtocol amendment. Loading...

Goals

By the end of this tutorial, you will be able to:

  • Create a loan broker linked to a private single asset vault.
  • Configure loan broker parameters, such as a management fee rate.
  • Retrieve the loan broker ID and pseudo-account.

Prerequisites

To complete this tutorial, you should:

  • Have a basic understanding of the XRP Ledger.
  • Have an XRP Ledger client library set up in your development environment. This page provides examples for the following:

Source Code

You can find the complete source code for this tutorial's examples in the code samples section of this website's repository.

Steps

1. Install dependencies

From the code sample folder, use npm to install dependencies:

npm install xrpl

2. Set up client and accounts

To get started, import the necessary libraries and instantiate a client to connect to the XRPL. This example imports:

  • xrpl: Used for XRPL client connection and transaction handling.
  • fs and child_process: Used to run tutorial set up scripts.
// IMPORTANT: This example creates a loan broker using an existing account
// that has already created a PRIVATE vault.
// If you want to create a loan broker for a PUBLIC vault, you can replace the vaultID
// and loanBroker values with your own.

import fs from 'fs'
import { execSync } from 'child_process'
import xrpl from 'xrpl'

// Connect to the network ----------------------
const client = new xrpl.Client('wss://s.devnet.rippletest.net:51233')
await client.connect()

Next, load the vault owner account and vault ID. The vault owner will also be the loan broker.

// This step checks for the necessary setup data to run the lending protocol tutorials.
// If missing, lendingSetup.js will generate the data.
if (!fs.existsSync('lendingSetup.json')) {
  console.log(`\n=== Lending tutorial data doesn't exist. Running setup script... ===\n`)
  execSync('node lendingSetup.js', { stdio: 'inherit' })
}

// Load preconfigured accounts and VaultID.
const setupData = JSON.parse(fs.readFileSync('lendingSetup.json', 'utf8'))

// You can replace these values with your own
const loanBroker = xrpl.Wallet.fromSeed(setupData.loanBroker.seed)
const vaultID = setupData.vaultID

console.log(`\nLoan broker/vault owner address: ${loanBroker.address}`)
console.log(`Vault ID: ${vaultID}`)

This example uses preconfigured accounts and vault data from the lendingSetup.js script, but you can replace loanBroker and vaultID with your own values.

3. Prepare LoanBrokerSet transaction

Create the LoanBrokerSet transaction object:

// Prepare LoanBrokerSet transaction ----------------------
console.log(`\n=== Preparing LoanBrokerSet transaction ===\n`)
const loanBrokerSetTx = {
  TransactionType: 'LoanBrokerSet',
  Account: loanBroker.address,
  VaultID: vaultID,
  ManagementFeeRate: 1000
}

// Validate the transaction structure before submitting
xrpl.validate(loanBrokerSetTx)
console.log(JSON.stringify(loanBrokerSetTx, null, 2))

The ManagementFeeRate is set in 1/10th basis points. A value of 1000 equals 1% (100 basis points).

4. Submit LoanBrokerSet transaction

Sign and submit the LoanBrokerSet transaction to the XRP Ledger.

// Submit, sign, and wait for validation ----------------------
console.log(`\n=== Submitting LoanBrokerSet transaction ===\n`)
const submitResponse = await client.submitAndWait(loanBrokerSetTx, {
  wallet: loanBroker,
  autofill: true
})
if (submitResponse.result.meta.TransactionResult !== 'tesSUCCESS') {
  const resultCode = submitResponse.result.meta.TransactionResult
  console.error('Error: Unable to create loan broker:', resultCode)
  await client.disconnect()
  process.exit(1)
}
console.log('Loan broker created successfully!')

Verify that the transaction succeeded by checking for a tesSUCCESS result code.

5. Get loan broker information

Retrieve the loan broker's information from the transaction result by checking for the LoanBroker entry in the transaction metadata.

// Extract loan broker information from the transaction result
console.log(`\n=== Loan Broker Information ===\n`)
const loanBrokerNode = submitResponse.result.meta.AffectedNodes.find(node =>
  node.CreatedNode?.LedgerEntryType === 'LoanBroker'
)
console.log(`LoanBroker ID: ${loanBrokerNode.CreatedNode.LedgerIndex}`)
console.log(`LoanBroker Psuedo-Account Address: ${loanBrokerNode.CreatedNode.NewFields.Account}`)

await client.disconnect()

The loan broker pseudo-account is a special account that holds first-loss capital.

See Also

Concepts:

Tutorials:

References: