Skip to content

feat: persist anchors, liquidity, and settlements in PostgreSQL - #239

Merged
Jagadeeshftw merged 1 commit into
AnchorNet-Org:mainfrom
Baskarayelu:feat/issue-220-durable-persistence
Aug 30, 2026
Merged

feat: persist anchors, liquidity, and settlements in PostgreSQL#239
Jagadeeshftw merged 1 commit into
AnchorNet-Org:mainfrom
Baskarayelu:feat/issue-220-durable-persistence

Conversation

@Baskarayelu

Copy link
Copy Markdown
Contributor

Summary

Implements the proposed durable persistence solution for issue #220. The service now has a PostgreSQL-backed production runtime instead of losing network state on restart.

  • Adds PostgreSQL persistence for anchors, liquidity entries, and settlements.
  • Hydrates all durable state before the HTTP server binds its port.
  • Requires DATABASE_URL in production and fails fast when PostgreSQL is unreachable.
  • Preserves bigint amounts with PostgreSQL numeric(78,0) and string-to-bigint conversion at the repository boundary.
  • Keeps the in-memory repositories for development and Jest when DATABASE_URL is omitted.
  • Adds ordered write flushing and graceful database shutdown.

Settlement concurrency and integrity

Settlement opening is performed in one PostgreSQL transaction. It locks all liquidity rows for the requested asset and all pending or executed settlement rows for that asset before calculating availability and inserting the pending settlement. This prevents concurrent API instances from reserving the same liquidity twice. Execute and cancel use conditional pending-state transitions so a settlement can only move out of pending once.

The schema includes foreign keys, non-negative and positive amount checks, valid status checks, cancellation reasons, and indexes for anchor, asset, status, and ordered settlement queries.

Migrations and deployment

The checked-in node-pg-migrate migration creates the complete initial schema. Deployments should run:

DATABASE_URL=postgres://user:password@host:5432/anchornet npm run migrate:up
DATABASE_URL=postgres://user:password@host:5432/anchornet npm start

The implementation follows the proposal posted on the issue: #220 (comment)

Verification

  • npm run typecheck
  • npm run build
  • npm run lint
  • npm test -- --runInBand --silent
  • 47 test suites passing, 515 tests passing
  • Migration module load check passing

Closes #220

@Jagadeeshftw
Jagadeeshftw merged commit cb78865 into AnchorNet-Org:main Aug 30, 2026
1 check passed
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.

The service has no database — anchors, liquidity and settlements live in in-memory Maps and are destroyed on every restart

2 participants