A complete Python library for interacting with the XRP Ledger blockchain, providing transaction creation, account management, and comprehensive XRPL protocol support
Query account information, balances, transaction history, and verify account existence on the XRPL ledger. These functions provide essential account-related operations for building XRPL applications.
Check if accounts exist on the ledger and retrieve basic account information.
from xrpl import account
def does_account_exist(address: str, client, ledger_index: str = "validated") -> bool:
"""
Check if an account exists on the XRPL ledger.
Args:
address: The XRPL address to check
client: XRPL client for network communication
ledger_index: Ledger version to query ("validated", "current", or ledger sequence)
Returns:
True if account exists, False otherwise
"""
def get_account_root(address: str, client, ledger_index: str = "validated") -> dict[str, Union[int, str]]:
"""
Retrieve the AccountRoot ledger object for an address.
Args:
address: The XRPL address to query
client: XRPL client for network communication
ledger_index: Ledger version to query
Returns:
AccountRoot object containing account data including:
- Account: The account address
- Balance: Account balance in drops
- Sequence: Next transaction sequence number
- Flags: Account flags and settings
- OwnerCount: Number of objects owned by account
- PreviousTxnID: Hash of previous transaction
- PreviousTxnLgrSeq: Ledger sequence of previous transaction
"""Query account balances and retrieve balance information.
def get_balance(address: str, client, ledger_index: str = "validated") -> int:
"""
Get the XRP balance of an account in drops.
Args:
address: The XRPL address to query
client: XRPL client for network communication
ledger_index: Ledger version to query
Returns:
Account balance in drops (1 XRP = 1,000,000 drops)
Raises:
XRPLException: If account does not exist or query fails
"""Manage transaction sequence numbers for reliable transaction submission.
def get_next_valid_seq_number(address: str, client, ledger_index: str = "current") -> int:
"""
Get the next valid sequence number for submitting transactions.
Args:
address: The XRPL address to query
client: XRPL client for network communication
ledger_index: Ledger version to query (typically "current" for latest)
Returns:
Next sequence number to use for transactions
Raises:
XRPLException: If account does not exist or query fails
"""Retrieve transaction history and latest transaction information.
def get_latest_transaction(address: str, client, ledger_index: str = "validated"):
"""
Get the most recent transaction for an account.
Args:
address: The XRPL address to query
client: XRPL client for network communication
ledger_index: Ledger version to query
Returns:
Transaction object of the most recent transaction, or None if no transactions
"""from xrpl.clients import JsonRpcClient
from xrpl import account
# Connect to XRPL testnet
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
# Check if account exists
address = "rN7n7otQDd6FczFgLdSqtcsAUxDkw6fzRH"
exists = account.does_account_exist(address, client)
print(f"Account exists: {exists}")
if exists:
# Get account balance
balance = account.get_balance(address, client)
print(f"Balance: {balance} drops ({balance / 1_000_000} XRP)")
# Get full account information
account_root = account.get_account_root(address, client)
print(f"Account data: {account_root}")
# Get next sequence number for transactions
next_seq = account.get_next_valid_seq_number(address, client)
print(f"Next sequence number: {next_seq}")from xrpl.clients import JsonRpcClient
from xrpl import account
def verify_account_setup(address: str, client) -> dict:
"""Verify account is properly set up and get key information."""
if not account.does_account_exist(address, client):
return {
"exists": False,
"error": "Account does not exist on the ledger"
}
try:
balance = account.get_balance(address, client)
account_root = account.get_account_root(address, client)
next_seq = account.get_next_valid_seq_number(address, client)
return {
"exists": True,
"balance_drops": balance,
"balance_xrp": balance / 1_000_000,
"sequence": account_root.get("Sequence", 0),
"next_sequence": next_seq,
"owner_count": account_root.get("OwnerCount", 0),
"flags": account_root.get("Flags", 0),
"account_data": account_root
}
except Exception as e:
return {
"exists": True,
"error": f"Failed to retrieve account information: {str(e)}"
}
# Usage
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
account_info = verify_account_setup("rN7n7otQDd6FczFgLdSqtcsAUxDkw6fzRH", client)
print(account_info)from xrpl.clients import JsonRpcClient
from xrpl import account
import time
def monitor_account_balance(address: str, client, interval: int = 10):
"""Monitor account balance changes over time."""
print(f"Monitoring balance for {address}")
previous_balance = None
while True:
try:
current_balance = account.get_balance(address, client)
if previous_balance is not None and current_balance != previous_balance:
change = current_balance - previous_balance
print(f"Balance changed: {change:+} drops (new balance: {current_balance} drops)")
else:
print(f"Current balance: {current_balance} drops ({current_balance / 1_000_000:.6f} XRP)")
previous_balance = current_balance
time.sleep(interval)
except KeyboardInterrupt:
print("Monitoring stopped")
break
except Exception as e:
print(f"Error monitoring account: {e}")
time.sleep(interval)
# Usage
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
monitor_account_balance("rN7n7otQDd6FczFgLdSqtcsAUxDkw6fzRH", client)import asyncio
from xrpl.asyncio.clients import AsyncJsonRpcClient
from xrpl.asyncio import account
async def get_account_info_async(address: str, client):
"""Get account information asynchronously."""
exists = await account.does_account_exist(address, client)
if not exists:
return {"exists": False}
# Run multiple queries concurrently
balance_task = account.get_balance(address, client)
account_root_task = account.get_account_root(address, client)
next_seq_task = account.get_next_valid_seq_number(address, client)
balance, account_root, next_seq = await asyncio.gather(
balance_task, account_root_task, next_seq_task
)
return {
"exists": True,
"balance": balance,
"account_root": account_root,
"next_sequence": next_seq
}
async def main():
client = AsyncJsonRpcClient("https://s.altnet.rippletest.net:51234")
try:
account_info = await get_account_info_async("rN7n7otQDd6FczFgLdSqtcsAUxDkw6fzRH", client)
print(account_info)
finally:
await client.close()
# Run async example
asyncio.run(main())Install with Tessl CLI
npx tessl i tessl/pypi-xrpl-py