mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-15 17:25:49 +00:00
Compare commits
3 Commits
sav-concep
...
add-code-s
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6df042c7b | ||
|
|
003927517f | ||
|
|
9c8c231900 |
4
_code-samples/walk-owner-directory/README.md
Normal file
4
_code-samples/walk-owner-directory/README.md
Normal 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.
|
||||
@@ -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()
|
||||
5
_code-samples/walk-owner-directory/js/package.json
Normal file
5
_code-samples/walk-owner-directory/js/package.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"xrpl": "^4.4.3"
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
1
_code-samples/walk-owner-directory/py/requirements.txt
Normal file
1
_code-samples/walk-owner-directory/py/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
xrpl-py==4.3.1
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user