Compare commits

..

3 Commits

Author SHA1 Message Date
mDuo13
c6df042c7b Add code sample showing how to walk an owner directory 2025-11-14 16:39:35 -08:00
Rome Reginelli
003927517f Merge pull request #3379 from XRPLF/rr-mpt-dupe-flag
Remove duplicate flags field in sample JSON
2025-11-14 12:19:31 -08:00
Rome Reginelli
9c8c231900 Remove duplicate flags field in sample JSON 2025-11-11 15:53:25 -08:00
8 changed files with 137 additions and 25 deletions

View File

@@ -1,9 +1,7 @@
// @chunk {"steps": ["import-node-tag"]}
// Import the library
import xrpl from "xrpl"
// @chunk-end
// @chunk {"steps": ["connect-tag"]}
import xrpl from "xrpl"
// Define the network client
const SERVER_URL = "wss://s.altnet.rippletest.net:51233/"
const client = new xrpl.Client(SERVER_URL)

View File

@@ -0,0 +1,4 @@
# Walk Owner Directory
Iterate over an account's owner directory and display how many ledger entries are in each page. In cases of highly active accounts, this can demonstrate the extent of "fragmentation" with skipped page numbers and non-full pages.
This code sample demonstrates the low-level structure of owner directories. If you don't need to see the breakdown by pages, you can use [`account_objects`](https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_objects) instead, since it provides a more convenient list of ledger entries attached to an account.

View File

@@ -0,0 +1,55 @@
// iterate-owner-directory.js
// Iterate over an account's owner directory and display how many ledger entries
// are in each page. In cases of highly active accounts, it can demonstrate
// the extent of "fragmentation" with skipped page numbers and non-full pages.
import xrpl from 'xrpl'
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233')
await client.connect()
const owner = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe" // Testnet faucet
// const owner = "rP9jPyP5kyvFRb6ZiRghAGw5u8SGAmU4bd" // TST issuer
// Set initial values for iterating
let sub_index = 0 // Directory root
let ledger_index = "validated"
// Query pages from the owner directory until they run out
console.log("Page #\t\t\tEntry count")
console.log("-----------------------------------")
while (true) {
// console.log(`Getting directory page ${sub_index}`)
const resp = await client.request({
"command": "ledger_entry",
"directory": {
"owner": owner,
"sub_index": sub_index
},
"ledger_index": ledger_index
})
if (resp.error) {
console.error("ledger_entry failed with error",resp.error)
break
}
// Consistently iterate the same ledger: query by index after the first
if (ledger_index === "validated") {
ledger_index = resp.result.ledger_index
}
console.log(`${sub_index}\t\t\t${resp.result.node.Indexes.length}`)
// console.log(`This page contains ${resp.result.node.Indexes.length} items.`)
// Continue onto another page if this one has more
if (resp.result.node.hasOwnProperty("IndexNext")) {
// The directory continues onto another page.
// IndexNext is returned as hex but sub_index needs decimal
sub_index = parseInt(resp.result.node.IndexNext, 16)
} else {
console.info("This is the last page of the directory")
break
}
}
client.disconnect()

View File

@@ -0,0 +1,5 @@
{
"dependencies": {
"xrpl": "^4.4.3"
}
}

View File

@@ -0,0 +1,55 @@
# iterate-owner-directory.py
# Iterate over an account's owner directory and display how many ledger entries
# are in each page. In cases of highly active accounts, it can demonstrate
# the extent of "fragmentation" with skipped page numbers and non-full pages.
from xrpl.clients import JsonRpcClient
from xrpl.models.requests import LedgerEntry
from xrpl.clients import XRPLRequestFailureException
OWNER_ADDRESS = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe" # Testnet faucet
# OWNER_ADDRESS = "rP9jPyP5kyvFRb6ZiRghAGw5u8SGAmU4bd" # TST issuer
client = JsonRpcClient("https://s.altnet.rippletest.net:51234/")
# Set initial values for iterating
sub_index = 0 # Directory root
ledger_index = "validated"
# Query pages from the owner directory until they run out
print("Page #\t\t\tEntry count")
print("-----------------------------------")
while True:
# Construct the LedgerEntry request for the directory page
directory_request = LedgerEntry(
directory={
"owner": OWNER_ADDRESS,
"sub_index": sub_index
},
ledger_index=ledger_index
)
# Send the request
try:
response = client.request(directory_request)
except Exception as e:
print(f"\nError: ledger_entry failed: {e}")
break
# The 'ledger_index' is consistently set after the first successful query.
# This ensures subsequent pages are read from the same ledger version.
if ledger_index == "validated":
ledger_index = response.result["ledger_index"]
# The entries are stored in the 'Indexes' field of the 'DirectoryNode'
entry_count = len(response.result["node"]["Indexes"])
print(f"{sub_index}\t\t\t{entry_count}")
# Check for the next page indicator
if "IndexNext" in response.result["node"].keys():
# The directory continues onto another page.
# Convert IndexNext from hex to decimal for sub_index.
hex_next = response.result["node"]["IndexNext"]
sub_index = int(hex_next, 16)
else:
print("\nThis is the last page of the directory.")
break

View File

@@ -0,0 +1 @@
xrpl-py==4.3.1

View File

@@ -24,7 +24,6 @@ This example assumes that the issuer of the token is the signer of the transacti
"AssetScale": 4,
"TransferFee": 0,
"MaximumAmount": "50000000",
"Flags": 83659,
"MPTokenMetadata": "7B2274223A225442494C4C222C226E223A22542D42696C6C205969656C6420546F6B656E222C2264223A2241207969656C642D62656172696E6720737461626C65636F696E206261636B65642062792073686F72742D7465726D20552E532E205472656173757269657320616E64206D6F6E6579206D61726B657420696E737472756D656E74732E222C2269223A226578616D706C652E6F72672F7462696C6C2D69636F6E2E706E67222C226163223A22727761222C226173223A227472656173757279222C22696E223A224578616D706C65205969656C6420436F2E222C227573223A5B7B2275223A226578616D706C657969656C642E636F2F7462696C6C222C2263223A2277656273697465222C2274223A2250726F647563742050616765227D2C7B2275223A226578616D706C657969656C642E636F2F646F6373222C2263223A22646F6373222C2274223A225969656C6420546F6B656E20446F6373227D5D2C226169223A7B22696E7465726573745F72617465223A22352E303025222C22696E7465726573745F74797065223A227661726961626C65222C227969656C645F736F75726365223A22552E532E2054726561737572792042696C6C73222C226D617475726974795F64617465223A22323034352D30362D3330222C226375736970223A22393132373936525830227D7D",
"Fee": "12",
"Flags": 122,

View File

@@ -62,10 +62,10 @@ Click **Download** on the top right of the code preview panel to download the so
Follow the steps to create a simple application with `xrpl.js`.
<!-- Web steps -->
{% step id="import-web-tag" when={ "environment": "Web" } %}
### 1. Install Dependencies
<!-- Web steps -->
{% step id="import-web-tag" when={ "environment": "Web" } %}
To load `xrpl.js` into your project, add a `<script>` tag to your HTML.
You can load the library from a CDN as in the example, or download a release and host it on your own website.
@@ -74,8 +74,7 @@ This loads the module into the top level as `xrpl`.
{% /step %}
<!-- Node.js steps -->
{% step id="import-node-tag" when={ "environment": "Node" } %}
### 1. Install Dependencies
{% step id="install-node-tag" when={ "environment": "Node" } %}
Start a new project by creating an empty folder, then move into that folder and use [NPM](https://www.npmjs.com/) to install the latest version of xrpl.js:
@@ -95,7 +94,7 @@ Your `package.json` file should look something like this:
{% step id="connect-tag" %}
#### Connect to the XRP Ledger Testnet
To make queries and submit transactions, you need to connect to the XRP Ledger. To do this with `xrpl.js`, you create an instance of the [`Client`](https://js.xrpl.org/classes/Client.html) class and use the [`connect()`](https://js.xrpl.org/classes/Client.html#connect) method.
To make queries and submit transactions, you need to connect to the XRP Ledger. To do this with `xrpl.js`, you create an instance of the `Client` class and use the `connect()` method.
{% admonition type="success" name="Tip" %}Many network functions in `xrpl.js` use [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to return values asynchronously. The code samples here use the [`async/await` pattern](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await) to wait for the actual result of the Promises.{% /admonition %}
@@ -131,48 +130,46 @@ The sample code shows you how to connect to the Testnet, which is one of the ava
{% step id="get-account-create-wallet-tag" %}
#### Create and Fund a Wallet
The `xrpl.js` library has a [`Wallet`](https://js.xrpl.org/classes/Wallet.html) class for handling the keys and address of an XRP Ledger account. On Testnet, you can fund a new account as shown in the example.
The `xrpl.js` library has a `Wallet` class for handling the keys and address of an XRP Ledger account. On Testnet, you can fund a new account as shown in the example.
{% /step %}
{% step id="get-account-create-wallet-b-tag" %}
#### (Optional) Generate a Wallet Only
If you want to generate a wallet without funding it, you can create a new [`Wallet`](https://js.xrpl.org/classes/Wallet.html) instance. Keep in mind that you need to send XRP to the wallet for it to be a valid account on the ledger.
If you want to generate a wallet without funding it, you can create a new `Wallet` instance. Keep in mind that you need to send XRP to the wallet for it to be a valid account on the ledger.
{% /step %}
{% step id="get-account-create-wallet-c-tag" %}
#### (Optional) Use Your Own Wallet Seed
To use an existing wallet seed encoded in [base58][], you can create a [`Wallet`](https://js.xrpl.org/classes/Wallet.html) instance from it.
To use an existing wallet seed encoded in [base58][], you can create a `Wallet` instance from it.
{% /step %}
{% step id="query-xrpl-tag" %}
### 4. Query the XRP Ledger
Use the Client's [`request()`](https://js.xrpl.org/classes/Client.html#request) method to access the XRP Ledger's [WebSocket API](../../../references/http-websocket-apis/api-conventions/request-formatting.md).
{% step id="query-xrpl-tag" %}
Use the Client's `request()` method to access the XRP Ledger's [WebSocket API](../../../references/http-websocket-apis/api-conventions/request-formatting.md).
{% /step %}
{% step id="listen-for-events-tag" %}
### 5. Listen for Events
You can set up handlers for various types of events in `xrpl.js`, such as whenever the XRP Ledger's [consensus process](../../../concepts/consensus-protocol/index.md) produces a new [ledger version](../../../concepts/ledgers/index.md). To do that, first call the [subscribe method][] to get the type of events you want, then attach an event handler using the [`on(eventType, callback)`](https://js.xrpl.org/classes/Client.html#on) method of the client.
{% step id="listen-for-events-tag" %}
You can set up handlers for various types of events in `xrpl.js`, such as whenever the XRP Ledger's [consensus process](../../../concepts/consensus-protocol/index.md) produces a new [ledger version](../../../concepts/ledgers/index.md). To do that, first call the [subscribe method][] to get the type of events you want, then attach an event handler using the `on(eventType, callback)` method of the client.
{% /step %}
{% step id="disconnect-node-tag" when={ "environment": "Node" } %}
### 6. Disconnect
Call the [`disconnect()`](https://js.xrpl.org/classes/Client.html#disconnect) function so Node.js can end the process. The example code waits 10 seconds before disconnecting to allow time for the ledger event listener to receive and display events.
{% step id="disconnect-node-tag" when={ "environment": "Node" } %}
Disconnect when done so Node.js can end the process. The example code waits 10 seconds before disconnecting to allow time for the ledger event listener to receive and display events.
{% /step %}
{% step id="disconnect-web-tag" when={ "environment": "Web" } %}
### 6. Disconnect
Call the [`disconnect()`](https://js.xrpl.org/classes/Client.html#disconnect) function to disconnect from the ledger when done. The example code waits 10 seconds before disconnecting to allow time for the ledger event listener to receive and display events.
Disconnect from the ledger when done. The example code waits 10 seconds before disconnecting to allow time for the ledger event listener to receive and display events.
{% /step %}
{% step id="run-app-node-tag" when={ "environment": "Node" } %}
### 7. Run the Application
{% step id="run-app-node-tag" when={ "environment": "Node" } %}
Finally, in your terminal, run the application like so:
```sh
@@ -240,8 +237,6 @@ Disconnected
{% /step %}
{% step id="run-app-web-tag" when={ "environment": "Web" } %}
### 7. Run the Application
Open the `index.html` file in a web browser.
You should see output similar to the following: