Skip to content

Add order parameter to getEvents for descending order support #575

@tomerweller

Description

@tomerweller

What problem does your feature solve?

There's no efficient way to query the N most recent events matching a filter. The getEvents endpoint only returns events in ascending order, forcing clients who want "latest events" to either:

  1. Scan the entire retention window and discard all but the last N events
  2. Guess a startLedger near the tip and retry with expanded windows on misses
  3. Implement binary search to find recent events

This is a common need for any tool that requires fetching latest activity.

What would you like to see?

An optional order field in pagination options:

{
  "startLedger": 1000,
  "pagination": {
    "limit": 10,
    "order": "desc"
  },
  "filters": [...]
}
Value Behavior
"asc" (default) Current behavior - oldest first
"desc" Newest first, starting from endLedger (or latest ledger)

Implementation cost should be low:

  • No database changes - existing composite indexes support reverse scans
  • Fully backwards compatible - defaults to "asc"

What alternatives are there?

  • Client-side filtering: Fetch all events in ascending order, keep last N. Inefficient and wasteful.
  • Heuristic windowing: Start queries near latest ledger, expand on miss. Unreliable and complex for clients.
  • External indexing: Use a separate service like SubQuery or a custom indexer. Adds operational complexity for a simple use case.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    To Do

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions