Skip to content

Commit

Permalink
fix(fee-randomization): fix fee range in PaymentConfirmModal
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni committed Sep 13, 2023
1 parent 313c077 commit 314eb03
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/components/PaymentConfirmModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import { AmountSats } from '../libs/JmWalletApi'
import { jarInitial } from './jars/Jar'
import { isValidNumber } from '../utils'

const feeRange: (feeValues: FeeValues) => [number, number] = (feeValues) => {
const feeTargetInSatsPerVByte = feeValues.tx_fees! / 1_000
const minFeeSatsPerVByte = Math.max(1, feeTargetInSatsPerVByte)
const maxFeeSatsPerVByte = feeTargetInSatsPerVByte * (1 + feeValues.tx_fees_factor!)
return [minFeeSatsPerVByte, maxFeeSatsPerVByte]
}

const useMiningFeeText = ({ feeConfigValues }: { feeConfigValues?: FeeValues }) => {
const { t } = useTranslation()

Expand All @@ -24,24 +31,23 @@ const useMiningFeeText = ({ feeConfigValues }: { feeConfigValues?: FeeValues })
} else if (unit === 'blocks') {
return t('send.confirm_send_modal.text_miner_fee_in_targeted_blocks', { count: feeConfigValues.tx_fees })
} else {
const feeTargetInSatsPerVByte = feeConfigValues.tx_fees! / 1_000
if (feeConfigValues.tx_fees_factor === 0) {
const [minFeeSatsPerVByte, maxFeeSatsPerVByte] = feeRange(feeConfigValues)
const fractionDigits = 2

if (minFeeSatsPerVByte.toFixed(fractionDigits) === maxFeeSatsPerVByte.toFixed(fractionDigits)) {
return t('send.confirm_send_modal.text_miner_fee_in_satspervbyte_exact', {
value: feeTargetInSatsPerVByte.toLocaleString(undefined, {
value: minFeeSatsPerVByte.toLocaleString(undefined, {
maximumFractionDigits: Math.log10(1_000),
}),
})
}

const minFeeSatsPerVByte = Math.max(1, feeTargetInSatsPerVByte * (1 - feeConfigValues.tx_fees_factor!))
const maxFeeSatsPerVByte = feeTargetInSatsPerVByte * (1 + feeConfigValues.tx_fees_factor!)

return t('send.confirm_send_modal.text_miner_fee_in_satspervbyte_randomized', {
min: minFeeSatsPerVByte.toLocaleString(undefined, {
maximumFractionDigits: 1,
maximumFractionDigits: fractionDigits,
}),
max: maxFeeSatsPerVByte.toLocaleString(undefined, {
maximumFractionDigits: 1,
maximumFractionDigits: fractionDigits,
}),
})
}
Expand Down

0 comments on commit 314eb03

Please sign in to comment.