Skip to content

fix(fees): avoid floating point error when converting sats/vbyte to sats/kvbyte - #1587

Merged
theborakompanioni merged 1 commit into
joinmarket-webui:develfrom
bhuvan-somisetty:fix/tx-fee-satsperkvbyte-rounding
Oct 31, 2026
Merged

theborakompanioni merged 1 commit into
joinmarket-webui:develfrom
bhuvan-somisetty:fix/tx-fee-satsperkvbyte-rounding

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown
Contributor

What does this PR do?

Mining fees entered in sats/vbyte are converted to sats/kvbyte with Math.ceil(value * 1_000). Because of floating point error, 4.03 * 1_000 === 4030.0000000000005, so the result is rounded up to 4031. This affects 2936 of the valid values (1.001–350 sat/vB), in two places:

  • Fee settings: tx_fees is saved as 4031, and the dialog shows 4.031 the next time it's opened
  • Send: a custom fee of 4.03 sat/vB is requested as txfee = 4031

Changes:

  • add satsPerVbyteToSatsPerKvbyte in src/lib/feeConfig.ts, which rounds the product to 3 decimals before Math.ceil, so real fractions still round up (4.0301 → 4031, 1 + 1/3 → 1334)

  • use it in FeeConfigDialog and collaborativeSend instead of the two inline copies

  • tests: helper cases, a check that every tx_fees value from 1001 to 350000 converts back to itself, plus one test each for the send request and the saved fee setting

  • Fixes Fees: sats/vbyte values like 4.03 are saved as 4031 sats/kvbyte (floating point rounding) #1586

Visual Demo (For contributors especially)

Image Demo (if applicable):

The new tests run against the old code (Math.ceil(value * 1_000)) and against the fix:

Before

before

After

after

…ats/kvbyte

`Math.ceil(value * 1_000)` rounds up floating point noise, e.g.
4.03 * 1_000 === 4030.0000000000005, so 4.03 sat/vB became 4031 sat/kvB.
This hit 2936 of the valid values (1.001-350 sat/vB), both for the fee
sent with a transaction and for `tx_fees` saved from the fee settings,
which then reopened showing 4.031 instead of 4.03.

Round the product to 3 decimals before rounding up, in one shared helper.

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
@theborakompanioni
theborakompanioni merged commit 1650da2 into joinmarket-webui:devel Oct 31, 2026
5 checks 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.

Fees: sats/vbyte values like 4.03 are saved as 4031 sats/kvbyte (floating point rounding)

2 participants