# Create a Loan Broker This tutorial shows you how to create a [LoanBroker](/docs/references/protocol/ledger-data/ledger-entry-types/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. LendingProtocol ## 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: - **JavaScript** with the [xrpl.js library](https://github.com/XRPLF/xrpl.js). See [Get Started Using JavaScript](/docs/tutorials/javascript/build-apps/get-started) for setup steps. ## 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 JavaScript From the code sample folder, use npm to install dependencies: ```bash 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. JavaScript // 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. JavaScript // 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](/docs/references/protocol/transactions/types/loanbrokerset) object: JavaScript // 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. JavaScript // 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. JavaScript // 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**: - [Lending Protocol](/docs/concepts/tokens/lending-protocol) - [Single Asset Vault](/docs/concepts/tokens/single-asset-vaults) **Tutorials**: - [Create a Single Asset Vault](/docs/tutorials/how-tos/set-up-lending/use-single-asset-vaults/create-a-single-asset-vault) - [Deposit and Withdraw First-Loss Capital](/docs/tutorials/how-tos/set-up-lending/use-the-lending-protocol/deposit-and-withdraw-cover) - [Create a Loan](/docs/tutorials/how-tos/set-up-lending/use-the-lending-protocol/create-a-loan) **References**: - [LoanBrokerSet transaction](/docs/references/protocol/transactions/types/loanbrokerset)