Skip to content

feat: post nbtc endpoint#61

Merged
sczembor merged 2 commits intomasterfrom
stan/post_nbtc_tx
Aug 28, 2025
Merged

feat: post nbtc endpoint#61
sczembor merged 2 commits intomasterfrom
stan/post_nbtc_tx

Conversation

@sczembor
Copy link
Contributor

@sczembor sczembor commented Aug 26, 2025

Description

Closes: #13


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • added appropriate labels to the PR
  • provided a link to the relevant issue or specification
  • added a changelog entry to CHANGELOG.md
  • included doc comments for public functions
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary

Summary by Sourcery

Introduce a new POST nbtc endpoint backed by registerBroadcastedNbtcTx, update the nbtc_minting schema to use composite keys with timestamp tracking and upsert logic, and add tests for the new registration flow.

New Features:

  • Add HTTP POST /nbtcTx endpoint to register broadcasted nBTC transactions
  • Implement registerBroadcastedNbtcTx in Indexer to parse tx hex and insert or ignore nBTC deposit records with 'broadcasting' status
  • Use INSERT...ON CONFLICT upsert for nbtc_minting entries to update confirming status and timestamps

Enhancements:

  • Add created_at and updated_at timestamps to nbtc_minting operations and standardize timestamp retrieval via Date.now()
  • Change nbtc_minting primary key to composite (tx_id, vout) and make block_hash and height nullable until confirmation
  • Replace multiple status update methods with unified upsert logic for confirming entries

Documentation:

  • Introduce PostNbtcTxRequest model interface

Tests:

  • Add unit tests for registerBroadcastedNbtcTx covering success and error cases

Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
@sczembor sczembor requested a review from a team as a code owner August 26, 2025 19:38
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 26, 2025

Reviewer's Guide

Implements a new POST /nbtcTx endpoint that registers broadcasting nBTC transactions via a new registerBroadcastedNbtcTx method, updates the nbtc_minting schema to support composite keys and timestamps, replaces raw inserts with upserts, standardizes timestamp usage, integrates router handling with JSON validation and error responses, and adds corresponding tests and request model.

Sequence diagram for POST /nbtcTx request handling

sequenceDiagram
  actor User
  participant Router
  participant Indexer
  participant DB
  User->>Router: POST /nbtcTx { txHex }
  Router->>Indexer: registerBroadcastedNbtcTx(txHex)
  Indexer->>DB: INSERT OR IGNORE INTO nbtc_minting
  DB-->>Indexer: Insert result
  Indexer-->>Router: { tx_id, registered_deposits }
  Router-->>User: Success response
Loading

File-Level Changes

Change Details Files
Replace raw inserts with UPSERT and add created_at/updated_at tracking
  • Replaced insert-only statement with insertOrUpdateNbtcTxStmt using ON CONFLICT for upserts
  • Added created_at and updated_at parameters to insert and update statements
packages/btcindexer/src/btcindexer.ts
Standardize timestamp generation
  • Replaced instances of +new Date() with Date.now() across update methods
packages/btcindexer/src/btcindexer.ts
Add registerBroadcastedNbtcTx method
  • Parse txHex, extract nBTC deposits, throw on empty
  • Batch insert deposits with status 'broadcasting' and timestamps
  • Return tx_id and count of registered deposits
packages/btcindexer/src/btcindexer.ts
Expose POST /nbtcTx in HTTP router
  • Changed route from PUT to POST and renamed handler to postNbtcTx
  • validated JSON body and returned 400 on invalid input
  • Invoked registerBroadcastedNbtcTx and handled errors with proper status codes
packages/btcindexer/src/router.ts
Update nbtc_minting table schema
  • Converted primary key to composite (tx_id, vout)
  • Added nullable block_hash and block_height, and required created_at and updated_at columns
  • Fixed index on nbtc_minting status
packages/btcindexer/db/migrations/0001_initial_schema.sql
Introduce PostNbtcTxRequest model
  • Defined PostNbtcTxRequest interface with txHex property
packages/btcindexer/src/models.ts
Add tests for registerBroadcastedNbtcTx
  • Mocked d1.batch in test setup
  • Added success case for single deposit registration
  • Added error case for transactions with no deposits
packages/btcindexer/src/btcindexer.test.ts

Possibly linked issues

  • EPIC: nbtc indexing #1: PR adds a new /nbtcTx endpoint to register broadcasted nBTC transactions, a key component for the nBTC indexer to monitor transactions.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and found some issues that need to be addressed.

  • The migration creates an index on nbtc_txs but the table was renamed to nbtc_minting, so please correct the index to reference nbtc_minting.
  • Remove the console.log in registerBroadcastedNbtcTx and use the existing structured logger to keep logging consistent.
  • Instead of inlining updated_at timestamps directly into SQL strings, bind them as parameters to avoid injection risks and keep queries consistent.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The migration creates an index on `nbtc_txs` but the table was renamed to `nbtc_minting`, so please correct the index to reference `nbtc_minting`.
- Remove the `console.log` in `registerBroadcastedNbtcTx` and use the existing structured logger to keep logging consistent.
- Instead of inlining `updated_at` timestamps directly into SQL strings, bind them as parameters to avoid injection risks and keep queries consistent.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
@sczembor sczembor merged commit af26953 into master Aug 28, 2025
11 checks passed
@sczembor sczembor deleted the stan/post_nbtc_tx branch August 28, 2025 10:10
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.

nbtc indexing: implement putNbtcTx endpoint

2 participants