Skip to content

Fix fee rate calculation and add fee display to verify-psbt#3936

Merged
jolestar merged 7 commits into
mainfrom
fix_fee_rate
Jan 25, 2026
Merged

Fix fee rate calculation and add fee display to verify-psbt#3936
jolestar merged 7 commits into
mainfrom
fix_fee_rate

Conversation

@jolestar

Copy link
Copy Markdown
Contributor

Summary

Fixes two issues related to Bitcoin transaction fee calculation:

  1. Fix inaccurate vsize estimation in build-tx when using --max-inputs
  2. Add fee, fee rate, and vsize display to verify-psbt command

Problem

When using --max-inputs parameter in rooch bitcoin build-tx, the fee calculation used a simplified hardcoded formula:

let estimated_vsize = 100 + utxo_count * 60 + 2 * 43;

This resulted in severely underestimated vsize (e.g., 7,026 bytes vs actual 18,990 bytes for 114 inputs), causing transactions to have only 19 satoshis fee instead of the expected ~19,000 satoshis for a 1 sat/vbyte rate.

Changes

build_tx.rs

  • Replace hardcoded formula with accurate TransactionBuilder::estimate_vbytes_with() method
  • Pre-resolve change address outside async map closure
  • Remove unnecessary 1000 satoshi deduction from fee calculation

transaction_builder.rs

  • Make estimate_vbytes_with() public to allow reuse in build_tx.rs

verify_psbt.rs

  • Add total_input_sat and total_output_sat fields to track transaction amounts
  • Calculate and display fee information (fee, fee rate, vsize)
  • Add appropriate handling for cases where witness_utxo is missing

Testing

Verified the fix by:

  1. Building transactions with the new code shows correct vsize estimation
  2. verify-psbt now displays fee information:
    Fee: XXXXX satoshi (0.XXXXXXXX BTC)
    Fee rate: X.XX sat/vbyte
    Transaction vsize: XXXXX bytes
    

Fixes #XXX

- Fix inaccurate vsize estimation in build-tx when using --max-inputs
- Replace hardcoded formula with accurate TransactionBuilder::estimate_vbytes_with()
- Remove unnecessary 1000 satoshi deduction from fee calculation
- Add fee, fee rate, and vsize display to verify-psbt command

This fixes the issue where transactions built with --max-inputs had only 19 satoshis
fee instead of the expected ~19,000 satoshis for 1 sat/vbyte rate.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 23, 2026 11:01
@vercel

vercel Bot commented Jan 23, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
rooch-portal-v2.1 Ready Ready Preview, Comment Jan 25, 2026 2:21am
test-portal Ready Ready Preview, Comment Jan 25, 2026 2:21am
1 Skipped Deployment
Project Deployment Review Updated (UTC)
rooch Ignored Ignored Preview Jan 25, 2026 2:21am

Request Review

@github-actions

github-actions Bot commented Jan 23, 2026

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 fixes Bitcoin transaction fee calculation issues in the build-tx and verify-psbt commands. The vsize estimation was previously using an inaccurate hardcoded formula, causing severely underestimated fees (e.g., 19 satoshis instead of ~19,000 satoshis for a 114-input transaction). The PR replaces the hardcoded calculation with the accurate TransactionBuilder::estimate_vbytes_with() method and adds fee display functionality to verify-psbt.

Changes:

  • Fixed vsize estimation in build-tx by replacing hardcoded formula with TransactionBuilder's accurate estimation method
  • Made estimate_vbytes_with() public to enable reuse across modules
  • Added fee, fee rate, and vsize display to verify-psbt command

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
crates/rooch/src/commands/bitcoin/transaction_builder.rs Made estimate_vbytes_with() method public and added documentation
crates/rooch/src/commands/bitcoin/build_tx.rs Replaced hardcoded vsize formula with TransactionBuilder::estimate_vbytes_with() and removed unnecessary 1000 satoshi deduction
crates/rooch/src/commands/bitcoin/verify_psbt.rs Added total_input_sat and total_output_sat fields to calculate and display fee information

Comment thread crates/rooch/src/commands/bitcoin/verify_psbt.rs Outdated
Comment thread crates/rooch/src/commands/bitcoin/verify_psbt.rs Outdated
Comment thread crates/rooch/src/commands/bitcoin/build_tx.rs
Comment thread crates/rooch/src/commands/bitcoin/verify_psbt.rs Outdated
- Use saturating_add to prevent overflow on large transaction amounts
- Fix fee display condition to only show when input data is available
- Add documentation explaining conservative vsize estimation (assumes 2 outputs)
- Add support for non_witness_utxo in fee calculation

These changes improve the robustness and accuracy of the fee calculation
and display in verify-psbt command.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@jolestar jolestar merged commit 87bcb96 into main Jan 25, 2026
29 checks passed
@jolestar jolestar deleted the fix_fee_rate branch January 25, 2026 02:57
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