fix: replace broken PSBT-to-vbytes estimation with input/output parsing - #17
Open
Alicepoltora wants to merge 1 commit into
Open
Alicepoltora wants to merge 1 commit into
Alicepoltora wants to merge 1 commit into
Conversation
The previous fee estimation used signedPsbt.length * 3/4 * 0.4 which severely underestimates vbytes. PSBT is a container with metadata (derivation paths, witness UTXO, sig hashes) that inflates its size well beyond the final transaction. The 0.4 coefficient was arbitrary and produced estimates ~60% too low. New approach: parse the raw PSBT binary to count inputs and outputs from the embedded unsigned transaction, then estimate vbytes using P2TR key-path constants (58 vbytes/input, 43 vbytes/output). Fixes both: - BareRgbLibBinding.estimateFee() used by fee display - WalletAccountRgb.transfer() used by transferMaxFee guard Without this fix, transactions could get stuck in the mempool due to underestimated fees, or the transferMaxFee guard could fail to catch transactions exceeding the configured fee limit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
estimateFee()usessignedPsbt.length * 3/4 * 0.4to estimate transaction vbytes. This is wrong.PSBT is a container format with metadata (derivation paths, witness UTXO descriptors, sig hashes, proprietary fields) that is not present in the final Bitcoin transaction. The raw PSBT is typically 2-3x larger than the transaction it produces.
The coefficient
0.4is arbitrary and produces estimates ~60% too low. Example:Impact
This affects two critical code paths:
BareRgbLibBinding.estimateFee()— fee displayed to users before confirming transactionsWalletAccountRgb.transfer()— thetransferMaxFeeguard that should prevent excessive feesWhat can go wrong:
transferMaxFeeguard uses the same formula → transactions exceeding the limit pass through undetectedFix
Replace the naive size-based formula with PSBT binary parsing:
This is accurate (±5 vbytes) for the common case of P2TR key-path spending that RGB wallets use.
New
src/fee-utils.jsmodule providesestimateVbytesFromPsbt()used by both call sites.