Skip to content

refactor: improve nbtc_deposit_addresses usage#328

Merged
robert-zaremba merged 4 commits intomasterfrom
robert/pk-nbtc-deposit-addresses2
Jan 29, 2026
Merged

refactor: improve nbtc_deposit_addresses usage#328
robert-zaremba merged 4 commits intomasterfrom
robert/pk-nbtc-deposit-addresses2

Conversation

@robert-zaremba
Copy link
Contributor

@robert-zaremba robert-zaremba commented Jan 28, 2026

Description

Superseeds: #326

Summary by Sourcery

Refine how nBTC deposit addresses are referenced across storage, tests, and seeding by looking them up directly via deposit_address instead of pre-validating or joining through setup metadata.

Enhancements:

  • Simplify nbtc_minting inserts and upserts to resolve address_id via subqueries on nbtc_deposit_addresses using deposit_address only.
  • Streamline nbtc_utxo insertion to derive address_id via an inline subquery instead of a separate lookup and error handling.
  • Relax seed-config existence checks to only verify presence of matching nbtc_deposit_addresses rows without relying on their IDs.

Tests:

  • Adjust btcindexer test helper to bind deposit addresses directly in minting inserts, removing explicit pre-validation queries against nbtc_deposit_addresses.

Signed-off-by: Robert Zaremba <robert@zaremba.ch>
@robert-zaremba robert-zaremba requested a review from a team as a code owner January 28, 2026 21:40
Copilot AI review requested due to automatic review settings January 28, 2026 21:40
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 28, 2026

Reviewer's Guide

Refactors how nbtc_deposit_addresses are referenced across BTC and SUI indexer code by removing explicit pre‑lookup queries and setup-based joins, instead using inline sub-selects by deposit address and simplifying related tests, storage, and seed script queries.

Entity relationship diagram for nbtc_deposit_addresses-based lookups

erDiagram
  nbtc_deposit_addresses {
    int id PK
    int setup_id FK
    string deposit_address
  }

  setups {
    int id PK
    string btc_network
    string sui_network
    string nbtc_pkg
  }

  nbtc_minting {
    string tx_id PK
    int address_id FK
    string sender
    int vout
    string block_hash
    int block_height
    string sui_recipient
    int amount
    string status
    int retry_count
    string sui_tx_id
    int created_at
    int updated_at
  }

  nbtc_utxos {
    string nbtc_utxo_id PK
    int address_id FK
    string dwallet_id
    string txid
    int vout
    int amount
    string script_pubkey
    string status
    int locked_until
  }

  setups ||--o{ nbtc_deposit_addresses : has
  nbtc_deposit_addresses ||--o{ nbtc_minting : referenced_by
  nbtc_deposit_addresses ||--o{ nbtc_utxos : referenced_by
Loading

Flow diagram for inserting nbtc_minting rows using inline sub-selects

flowchart TD
  A["Receive BTC or deposit transaction data"] --> B["Derive depositAddress"]
  B --> C["INSERT into nbtc_minting<br/>VALUES (<br/>  tx_id,<br/>  (SELECT id FROM nbtc_deposit_addresses<br/>   WHERE deposit_address = depositAddress),<br/>  other_fields...<br/>)"]
  C --> D{"Sub-select finds matching<br/>nbtc_deposit_addresses row?"}
  D -- "Yes" --> E["Row inserted or updated<br/>(address_id set from sub-select)"]
  D -- "No" --> F["INSERT fails due to missing<br/>matching deposit address<br/>(DB-level integrity)"]
Loading

File-Level Changes

Change Details Files
Use inline sub-selects on nbtc_deposit_addresses by deposit_address instead of prefetching address IDs or joining via setups.
  • In BTC indexer tests, remove explicit SELECT/validation of deposit address, and change nbtc_minting INSERT to use (SELECT id FROM nbtc_deposit_addresses WHERE deposit_address = ?) for address_id
  • In CFStorage.insertOrUpdateNbtcTxs, simplify INSERT ... ON CONFLICT to select address_id solely by deposit_address, dropping btc_network, sui_network, and nbtc_pkg join predicates
  • In CFStorage.insertDeposits, simplify INSERT OR IGNORE to use a sub-select of address_id by deposit_address and remove network/pkg binding parameters
  • In SUI indexer D1Storage.insertOrUpdateUtxo, remove pre-query for address_id and error on missing row, instead use sub-select on nbtc_deposit_addresses by deposit_address in the nbtc_utxos INSERT
packages/btcindexer/src/btcindexer.helpers.test.ts
packages/btcindexer/src/cf-storage.ts
packages/sui-indexer/src/storage.ts
Clarify schema/comment and slightly adjust seed check query for nbtc_deposit_addresses.
  • Update nbtc_minting.address_id comment to explicitly point to nbtc_deposit_addresses.setup_id linkage
  • Change seed-config address existence check to SELECT 1 instead of SELECT id from nbtc_deposit_addresses for a given setup_id and address
packages/btcindexer/db/migrations/0001_initial_schema.sql
packages/btcindexer/scripts/seed-config.ts

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 - I've found 1 issue, and left some high level feedback:

  • Several queries were simplified to look up nbtc_deposit_addresses only by deposit_address (dropping setup_id/network/nbtc_pkg filters); please confirm that deposit_address is globally unique or reintroduce appropriate scoping to avoid ambiguous matches across setups/networks.
  • In the test helper insertTx, the explicit pre-check that a deposit address exists was removed in favor of an inline subquery; consider restoring an explicit validation or clearer error handling so test failures are easier to diagnose when the address is missing.
  • In seed-config.ts, the query was changed from SELECT id FROM nbtc_deposit_addresses... to SELECT 1... but executeQuery<number>(..., "id") still appears to expect a column named id; you may need to alias the constant (SELECT 1 AS id) or adjust the result access.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Several queries were simplified to look up `nbtc_deposit_addresses` only by `deposit_address` (dropping `setup_id`/network/nbtc_pkg filters); please confirm that `deposit_address` is globally unique or reintroduce appropriate scoping to avoid ambiguous matches across setups/networks.
- In the test helper `insertTx`, the explicit pre-check that a deposit address exists was removed in favor of an inline subquery; consider restoring an explicit validation or clearer error handling so test failures are easier to diagnose when the address is missing.
- In `seed-config.ts`, the query was changed from `SELECT id FROM nbtc_deposit_addresses...` to `SELECT 1...` but `executeQuery<number>(..., "id")` still appears to expect a column named `id`; you may need to alias the constant (`SELECT 1 AS id`) or adjust the result access.

## Individual Comments

### Comment 1
<location> `packages/btcindexer/scripts/seed-config.ts:70-71` </location>
<code_context>
 		}

-		const checkAddrQuery = `SELECT id FROM nbtc_deposit_addresses WHERE setup_id = ${setupId} AND deposit_address = '${entry.btc_address}'`;
+		const checkAddrQuery = `SELECT 1 FROM nbtc_deposit_addresses WHERE setup_id = ${setupId} AND deposit_address = '${entry.btc_address}'`;
 		const existingAddrId = await executeQuery<number>(checkAddrQuery, DB_NAME, local, "id");

 		if (existingAddrId) {
</code_context>

<issue_to_address>
**issue (bug_risk):** Column alias in SELECT no longer matches the column name requested from executeQuery, which likely breaks the existing check.

`executeQuery<number>(..., "id")` still expects an `id` column, but the query now returns only a constant (`SELECT 1 ...`). This will likely return `undefined` or throw, so `existingAddrId` will not work as intended. Please either keep `SELECT id ...` or alias the constant as `id` (e.g. `SELECT 1 AS id ...`) to preserve the expected column name.
</issue_to_address>

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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors how nbtc_deposit_addresses are used throughout the indexers, centralizing address resolution via SQL subqueries and slightly clarifying schema intent.

Changes:

  • Replace manual lookups of nbtc_deposit_addresses.id with inline SELECT id FROM nbtc_deposit_addresses WHERE deposit_address = ? subqueries in both the Sui indexer and BTC indexer storage layers.
  • Simplify minting-related insert statements in CFStorage and the BTC indexer test helpers to rely solely on the globally-unique deposit_address instead of joining via setups and networks.
  • Adjust the seeding script and the initial migration comment for nbtc_minting.address_id to better reflect the intended relationship to nbtc_deposit_addresses.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/sui-indexer/src/storage.ts Simplifies UTXO insertion by resolving address_id via a subquery on deposit_address instead of a prior explicit select.
packages/btcindexer/src/cf-storage.ts Refactors minting insert/UPSERT queries to derive address_id from deposit_address only, removing redundant joins on setups.
packages/btcindexer/src/btcindexer.helpers.test.ts Updates test helper insertTx to match the new minting insert pattern using an inline subquery on deposit_address.
packages/btcindexer/scripts/seed-config.ts Changes the existence check query for deposit addresses to select a constant rather than the id.
packages/btcindexer/db/migrations/0001_initial_schema.sql Clarifies the comment on nbtc_minting.address_id to describe how it links back to nbtc_deposit_addresses.setup_id.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 70 to 72
const checkAddrQuery = `SELECT 1 FROM nbtc_deposit_addresses WHERE setup_id = ${setupId} AND deposit_address = '${entry.btc_address}'`;
const existingAddrId = await executeQuery<number>(checkAddrQuery, DB_NAME, local, "id");

Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

The change from SELECT id FROM nbtc_deposit_addresses ... to SELECT 1 FROM ... means the result set no longer has an id column, but executeQuery is still called with field: "id". As a result, executeQuery will always return null for existingAddrId, so the script will try to insert the address every time and can hit the UNIQUE(deposit_address) constraint. Either keep selecting id (so the field lookup remains valid) or drop the field argument and treat a non-empty result as existence.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should be fixed now, please recheck

Copy link
Contributor

@sczembor sczembor left a comment

Choose a reason for hiding this comment

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

pre-approve

robert-zaremba and others added 2 commits January 29, 2026 08:45
Co-authored-by: sczembor <43810037+sczembor@users.noreply.github.com>
Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Robert Zaremba <robert@zaremba.ch>
@robert-zaremba robert-zaremba merged commit 45d9c7a into master Jan 29, 2026
12 checks passed
@robert-zaremba robert-zaremba deleted the robert/pk-nbtc-deposit-addresses2 branch January 29, 2026 08:27
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.

3 participants