Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/components/send/collaborativeSend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ describe('buildCollaborativeSendRequest', () => {
expect(request.txfee).toBe(1_334)
})

it('passes txfee in sats/kvbyte without floating point rounding errors', () => {
const request = buildCollaborativeSendRequest({
...baseValues(),
txFee: {
txFeeUnit: TX_FEE_UNITS.SATS_PER_VBYTE,
txFeeInSatsPerVbyte: 4.03,
},
})

expect(request.txfee).toBe(4_030)
})

it('throws for invalid destination address', () => {
expect(() =>
buildCollaborativeSendRequest({
Expand Down
4 changes: 2 additions & 2 deletions src/components/send/collaborativeSend.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DirectSendRequest, DoCoinjoinRequest } from '@joinmarket-webui/joinmarket-api-ts/jm'
import { TX_FEE_UNITS } from '@/lib/feeConfig'
import { satsPerVbyteToSatsPerKvbyte, TX_FEE_UNITS } from '@/lib/feeConfig'
import { isValidAddress, normalizeBitcoinAddress } from '@/lib/formValidation'
import { isValidInteger, isValidNumber } from '@/lib/utils'
import type { SendFormValues } from './types'
Expand All @@ -15,7 +15,7 @@ const txFeeValueForRequest = (data: SendFormValues): number | undefined => {
if (data.txFee.txFeeUnit === TX_FEE_UNITS.BLOCKS && data.txFee.txFeeInBlocks !== undefined) {
return data.txFee.txFeeInBlocks
} else if (data.txFee.txFeeUnit === TX_FEE_UNITS.SATS_PER_VBYTE && data.txFee.txFeeInSatsPerVbyte !== undefined) {
return Math.ceil(data.txFee.txFeeInSatsPerVbyte * 1_000)
return satsPerVbyteToSatsPerKvbyte(data.txFee.txFeeInSatsPerVbyte)
}
return undefined
}
Expand Down
31 changes: 31 additions & 0 deletions src/components/settings/fees/FeeConfigDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ describe('FeeConfigDialog', () => {
})
})

it('saves the sats per vbyte value unchanged', async () => {
h.mutateAsync = vi.fn().mockResolvedValue({})
const refetchAll = vi.fn().mockResolvedValue(undefined)
renderDialog(
makeValidValidation({
refetchAll,
feeConfigValues: {
txFeeFactor: 0.1,
maxSweepFeeChangeFactor: 0.8,
maxCjAbsoluteFee: 100,
maxCjRelativeFee: 0.001,
txFee: {
txFeeUnit: 'sats/vbyte',
txFeeInBlocks: undefined,
txFeeInSatsPerVbyte: 4.03,
},
},
}),
)

fireEvent.click(screen.getByText('settings.fees.text_button_submit'))

await waitFor(() => {
expect(h.toastSuccess).toHaveBeenCalled()
})
expect(h.mutateAsync).toHaveBeenCalledWith({
path: { walletname: 'test.jmdat' },
body: { section: 'POLICY', field: 'tx_fees', value: '4030' },
})
})

it('skips validation when form validation is disabled in developer mode', async () => {
h.developerModeEnabled = true
h.mutateAsync = vi.fn().mockResolvedValue({})
Expand Down
4 changes: 2 additions & 2 deletions src/components/settings/fees/FeeConfigDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { FEE_CONFIG_KEYS, type FeeConfigName } from '@/constants/jm'
import { useApiClient } from '@/hooks/useApiClient'
import { useFeeConfigValidation } from '@/hooks/useFeeConfigValidation'
import { getErrorReason } from '@/lib/errorReason'
import { TX_FEE_UNITS } from '@/lib/feeConfig'
import { satsPerVbyteToSatsPerKvbyte, TX_FEE_UNITS } from '@/lib/feeConfig'
import { cn, factorToPercentage, isValidInteger, isValidNumber, percentageToFactorString } from '@/lib/utils'
import type { WalletFileName } from '@/lib/utils'
import { useDeveloperMode } from '@/store/jamSettingsStore'
Expand Down Expand Up @@ -136,7 +136,7 @@ export const FeeConfigDialog = ({

const txFeesSatsPerKvByteValue =
miningData.txFee.txFeeInSatsPerVbyte !== undefined && isValidNumber(miningData.txFee.txFeeInSatsPerVbyte)
? String(Math.ceil(miningData.txFee.txFeeInSatsPerVbyte * 1_000))
? String(satsPerVbyteToSatsPerKvbyte(miningData.txFee.txFeeInSatsPerVbyte))
: ''
const txFeesValue =
miningData.txFee.txFeeUnit === TX_FEE_UNITS.BLOCKS ? txFeesBlocksValue : txFeesSatsPerKvByteValue
Expand Down
32 changes: 31 additions & 1 deletion src/lib/feeConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest'
import { MAX_TX_FEE_IN_BLOCKS, MIN_TX_FEE_IN_SATS_PER_VBYTE } from '@/components/send/TxFeeForm.schema'
import { isMaxFeesConfigMissing, toJamFeeConfigValues, TX_FEE_UNITS } from './feeConfig'
import { isMaxFeesConfigMissing, satsPerVbyteToSatsPerKvbyte, toJamFeeConfigValues, TX_FEE_UNITS } from './feeConfig'

describe('toJamFeeConfigValues', () => {
it('should parse raw values successfully', () => {
Expand Down Expand Up @@ -202,3 +202,33 @@ describe('isMaxFeesConfigMissing', () => {
expect(isMaxFeesConfigMissing(values)).toBe(false)
})
})

describe('satsPerVbyteToSatsPerKvbyte', () => {
it('should convert sats/vbyte to sats/kvbyte', () => {
expect(satsPerVbyteToSatsPerKvbyte(1.001)).toBe(1_001)
expect(satsPerVbyteToSatsPerKvbyte(5)).toBe(5_000)
expect(satsPerVbyteToSatsPerKvbyte(350)).toBe(350_000)
})

it('should not round up because of floating point noise', () => {
// e.g. 4.03 * 1_000 === 4030.0000000000005
expect(satsPerVbyteToSatsPerKvbyte(2.007)).toBe(2_007)
expect(satsPerVbyteToSatsPerKvbyte(4.03)).toBe(4_030)
expect(satsPerVbyteToSatsPerKvbyte(4.001)).toBe(4_001)
})

it('should round up fractions of a sat/kvbyte', () => {
expect(satsPerVbyteToSatsPerKvbyte(1 + 1 / 3)).toBe(1_334)
expect(satsPerVbyteToSatsPerKvbyte(4.0301)).toBe(4_031)
})

it('should return the value of "tx_fees" it was parsed from', () => {
const mismatches: string[] = []
for (let txFees = 1_001; txFees <= 350_000; txFees++) {
const { txFee } = toJamFeeConfigValues({ tx_fees: String(txFees) })
const result = satsPerVbyteToSatsPerKvbyte(txFee.txFeeInSatsPerVbyte!)
if (result !== txFees) mismatches.push(`${txFees} -> ${result}`)
}
expect(mismatches).toStrictEqual([])
})
})
8 changes: 8 additions & 0 deletions src/lib/feeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export interface JamFeeConfigValues {
}
}

/**
* Converts a fee rate in sats/vbyte to the integer sats/kvbyte value used for `tx_fees`.
*
* The product is rounded to 3 decimals before rounding up, so floating point noise
* (e.g. `4.03 * 1_000 === 4030.0000000000005`) doesn't bump the result by 1 sat/kvbyte.
*/
export const satsPerVbyteToSatsPerKvbyte = (value: number): number => Math.ceil(Number((value * 1_000).toFixed(3)))

export const isMaxFeesConfigMissing = (values: JamFeeConfigValues | undefined): boolean => {
return !!values && (values.maxCjAbsoluteFee === undefined || values.maxCjRelativeFee === undefined)
}
Expand Down
Loading