Understanding Bitcoin transaction fees and how they work in the BSV TypeScript SDK.
Transaction fees are payments to miners for including transactions in blocks:
import { Transaction } from '@bsv/sdk'
// Calculate transaction fee
const tx = new Transaction()
const feeRequired = tx.getFee()
// Set custom fee rate
const customFee = tx.getFee(feePerKB)
Fees are calculated based on transaction size:
The SDK calculates fees automatically:
// Wallet handles fees automatically
const wallet = new WalletClient()
const action = await wallet.createAction({
outputs: [{
satoshis: 1000,
lockingScript: script
}]
})
// Fee calculated and deducted automatically
For advanced use cases:
// Calculate fee manually
const tx = new Transaction()
tx.addInput(/* input */)
tx.addOutput(/* output */)
const estimatedSize = tx.getSerializedSize()
const feeRequired = estimatedSize * feePerByte
BSV typically uses low, predictable fees:
// Get current network fee rates
const feeRates = await chainTracker.getFeeRates()
const recommendedFee = feeRates.standard
Each input adds to transaction size:
Each output adds to transaction size:
Fixed overhead for every transaction:
Choose UTXOs efficiently:
// Prefer fewer, larger UTXOs to reduce fees
const utxos = await wallet.getUTXOs()
const selected = selectOptimalUTXOs(utxos, targetAmount)
Combine multiple payments:
// Batch multiple outputs in one transaction
const outputs = [
{ satoshis: 1000, lockingScript: script1 },
{ satoshis: 2000, lockingScript: script2 },
{ satoshis: 1500, lockingScript: script3 }
]
Use efficient script templates:
// P2PKH is more efficient than complex scripts
const p2pkh = new P2PKH()
const efficientScript = p2pkh.lock(publicKeyHash)
Estimate transaction size before creation:
// Estimate size for fee calculation
const estimatedInputs = 2
const estimatedOutputs = 3
const estimatedSize = estimateTransactionSize(estimatedInputs, estimatedOutputs)
const estimatedFee = estimatedSize * feeRate
// Use script templates for accurate estimation
const template = new P2PKH()
const scriptSize = template.estimateLength([publicKeyHash])
Common fee-related issues:
try {
const action = await wallet.createAction({
outputs: outputs
})
} catch (error) {
if (error.message.includes('Insufficient funds')) {
// Handle insufficient balance for fees
console.log('Not enough funds to cover transaction and fees')
}
}
Most applications rely on wallets for fee handling:
// Wallet manages fees automatically
const wallet = new WalletClient()
// Fees are calculated and deducted automatically
const result = await wallet.createAction({
description: 'Payment with automatic fees',
outputs: [/* outputs */]
})
Increase fees for faster confirmation:
// Create transaction with RBF enabled
const tx = new Transaction()
tx.setRBF(true) // Enable replace-by-fee
Use dependent transactions to increase effective fee rate:
// Create child transaction with higher fee
const childTx = new Transaction()
childTx.addInput(/* from parent transaction */)
// Higher fee rate pulls parent transaction along