Appendix: Helper Functions

Common Utility Functions for RPC Handler Development

← Back to Building and Integrating Custom RPC Handlers


Introduction

The rippled RPC module provides a rich set of helper functions that handle common operations in handlers. Rather than reimplementing these utilities, your handlers should leverage existing functions for consistency, reliability, and maintainability.

This reference guide catalogs the most frequently-used helper functions with signatures, usage examples, and common patterns.

Source Location: src/xrpld/rpc/detail/RPCHelpers.h and src/xrpld/rpc/detail/RPCHelpers.cpp


Account and Address Functions

RPC::accountFromString()

Purpose: Parse account identifier from string, supporting multiple formats

Signature:

namespace RPC {
    boost::optional<AccountID>
    accountFromString(std::string const& strAccount);
}

Parameters:

  • strAccount: Account identifier as string (Base58Check address or account ID)

Returns:

  • boost::optional<AccountID> - Contains parsed AccountID if successful, empty if invalid

Error Conditions:

  • Invalid Base58Check format

  • Wrong checksum

  • Incorrect length

Example Usage:

When to Use:

  • Parsing account addresses from RPC parameters

  • Validating account identifiers in handlers

  • Converting string representations to AccountID objects

Notes:

  • Wrapper around parseBase58<AccountID>()

  • Handles both standard and non-standard account formats

  • Preferred over manual Base58 decoding


parseBase58()

Purpose: Generic Base58Check decoder template

Signature:

Template Parameters:

  • T: Target type (usually AccountID, Blob, etc.)

Parameters:

  • encoded: Base58Check encoded string

Returns:

  • boost::optional<T> - Decoded value if valid, empty if invalid

Example Usage - Account:

Example Usage - Generic Blob:

Example Usage - Seed Decoding:

When to Use:

  • Decoding Base58Check encoded data

  • Parsing binary identifiers and hashes

  • Converting external formats to internal representations

Notes:

  • Template specialization required for custom types

  • Performs checksum validation automatically

  • More general than accountFromString()


Ledger Access Functions

RPC::lookupLedger()

Purpose: Retrieve ledger by specification, handling current/validated/indexed requests

Signature:

Parameters:

  • ledger: Output reference (populated if successful)

  • context: RPC request context with parameters and app reference

Returns:

  • Json::Value: Empty on success, error response if lookup failed

Supported Parameters:

  • ledger_hash: Specific ledger hash

  • ledger_index: Numeric index or "current"/"validated"

  • ledger_min_index / ledger_max_index: Range lookup (less common)

Example Usage - Default to Current:

Example Usage - With Default Ledger:

Error Responses:

  • rpcLGR_NOT_FOUND: Specified ledger doesn't exist

  • rpcNO_CURRENT: No current ledger available

  • rpcNO_CLOSED: No validated ledger available

  • rpcLGR_INDEX_BOUNDS: Index out of valid range

When to Use:

  • Retrieving ledgers for queries

  • Handling user-specified ledger parameters

  • Validating ledger availability before operations

Notes:

  • Single most important helper for handlers

  • Always use instead of manual ledger lookup

  • Properly handles all ledger specification modes


RPC::getLedgerRange()

Purpose: Determine valid ledger index range available on this node

Signature:

Parameters:

  • minIndex: Output - oldest available ledger index

  • maxIndex: Output - newest available ledger index

  • context: RPC request context

Returns:

  • bool: True if range valid, false if no ledgers available

Example Usage:

When to Use:

  • Validating user-supplied ledger indices

  • Determining available data range

  • Implementing range-based queries

  • Error messages about available history

Notes:

  • Range depends on node's history mode

  • Archival nodes have larger ranges

  • Always validate user indices against this range


Amount and Value Functions

amountFromJsonNoThrow()

Purpose: Safely parse XRP or token amount from JSON without throwing exceptions

Signature:

Parameters:

  • value: JSON value containing amount specification

Returns:

  • boost::optional<Amount>: Parsed amount if valid, empty if invalid

Supported Formats:

Example Usage - XRP Amount:

Example Usage - Token Amount:

When to Use:

  • Parsing transaction amounts

  • Validating amount parameters

  • Handling both XRP and token amounts

  • Any situation dealing with ledger values

Notes:

  • Preferred over throwing version for handlers

  • Handles format variations automatically

  • Returns optional for clear error handling


JSON and Type Conversion Functions

ripple::to_string()

Purpose: Convert various types to string representation

Common Overloads:

Example Usage:

When to Use:

  • Converting internal types to JSON-serializable format

  • Building human-readable output

  • Creating hash/address strings for responses

Notes:

  • Overloaded for many ripple types

  • Preferred over manual formatting

  • Handles endianness and encoding automatically


Permission and State Functions

context.role Functions

Purpose: Check request caller's authorization level

Available Methods:

Example Usage:

Role Hierarchy:

When to Use:

Notes:

  • Check early, before expensive operations

  • Roles defined in Config.h

  • Proxy role is special case


Specialized Query Functions

ledger->read()

Purpose: Read ledger entry by keylet

Signature:

Common Keylets:

Example Usage:

When to Use:

  • Querying ledger state

  • Accessing account information

  • Reading any ledger object

Notes:


Error Response Functions

rpcError()

Purpose: Create standardized RPC error responses

Overloads:

Example Usage:

When to Use:

  • Building all error responses

  • Consistent error formatting

  • Always prefer to manual error JSON

Notes:


Function Selection Guide

Task
Function(s)

Parse account address

accountFromString()

Parse any Base58Check

parseBase58<T>()

Get ledger to query

lookupLedger()

Validate ledger index

getLedgerRange()

Parse amount

amountFromJsonNoThrow()

Convert to string

to_string()

Check permissions

context.role.is*()

Read ledger object

ledger->read()

Create error

rpcError()


Usage Pattern Summary


← Back to Appendices | Handler Examples | Debugging Guide →


Related Module Sections:

Last updated