Skip to content

all: Store native XLM balances and trustline data in PostgreSQL#465

Closed
Copilot wants to merge 1 commit intostore-native-balancefrom
copilot/sub-pr-453-again
Closed

all: Store native XLM balances and trustline data in PostgreSQL#465
Copilot wants to merge 1 commit intostore-native-balancefrom
copilot/sub-pr-453-again

Conversation

Copy link

Copilot AI commented Jan 16, 2026

What

Implements database storage for native XLM balances and refactors token ingestion to eliminate RPC calls for balance queries.

Why

Balance queries currently hit RPC for every account lookup. This creates performance bottlenecks and unnecessary external dependencies for data we're already processing during ingestion.

Architecture changes:

  • Native balance tracking: New native_balances table stores XLM balances, minimum reserves, and liabilities extracted from account ledger entries via AccountsProcessor
  • Separated token storage: Split monolithic account_tokens into three specialized models:
    • native_balances - XLM account data
    • trustline_balances - Classic trustline holdings with full XDR fields
    • account_contract_tokens - SAC/SEP-41 contract mappings
  • Map-based deduplication: Changed IndexerBuffer from slice-based to map-based storage, keeping only the latest change per account/trustline per ledger. Handles CREATE→REMOVE no-ops within same ledger.
  • Database-first queries: GraphQL resolvers read from PostgreSQL via BalanceReader interface instead of RPC

Data flow:

Ledger ingestion → AccountsProcessor extracts balance changes → IndexerBuffer deduplicates → 
BatchUpsert to native_balances table → GraphQL queries read from DB

GraphQL schema additions:

type NativeBalance {
    minimumBalance: String!
    buyingLiabilities: String!
    sellingLiabilities: String!
    lastModifiedLedger: Int!
}

Minimum balance calculation:

minimumBalance = (2 + numSubEntries + numSponsoring - numSponsored) * 5_000_000 + sellingLiabilities

Known limitations

N/A

Issue that this PR addresses

N/A

Checklist

PR Structure

  • It is not possible to break this PR down into smaller PRs.
  • This PR does not mix refactoring changes with feature changes.
  • This PR's title starts with name of package that is most changed in the PR, or all if the changes are broad or impact many packages.

Thoroughness

  • This PR adds tests for the new functionality or fixes.
  • All updated queries have been tested (refer to this check if the data set returned by the updated query is expected to be same as the original one).

Release

  • This is not a breaking change.
  • This is ready to be tested in development.
  • The new functionality is gated with a feature flag if this is not ready for production.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Process account entry changes and store XLM balances all: Store native XLM balances and trustline data in PostgreSQL Jan 16, 2026
Copilot AI requested a review from aditya1702 January 16, 2026 21:21
@aditya1702 aditya1702 closed this Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants