-
Notifications
You must be signed in to change notification settings - Fork 82
Open
@mr-zwets
Description
I use a helper calculateTransactionFee quite a bit to calculate and test the fee rate for a transaction built with the SDK.
Because there are walletconnect placeholders used in the transaction builder i also use a replacePlaceholderByRandomUser to get actual signatures for the transaction
const testTransactionBuilder = replacePlaceholderByRandomUser(transactionBuilder) expect(() => testTransactionBuilder.debug()).not.toThrow(); const testDecodedTransaction = createDecodedTransaction(testTransactionBuilder); const { txFeeRate } = calculateTransactionFee(testDecodedTransaction, wcSourceOutputs) expect(txFeeRate > 1 && txFeeRate < 3).toBe(true);
where the calculateTransactionFee is implemented as
export function calculateTransactionFee( decodedTransaction: TransactionCommon, wcSourceOutputs: WcSourceOutput[], ) { const txOutputs = decodedTransaction.outputs; const totalInputAmount = wcSourceOutputs.reduce((acc, input) => acc + BigInt(input.valueSatoshis), 0n); const totalOutputAmount = txOutputs.reduce((acc, output) => acc + BigInt(output.valueSatoshis), 0n); const txFeeSats = totalInputAmount - totalOutputAmount; const encodedTx = encodeTransaction(decodedTransaction); const txFeeRate = Number(txFeeSats) / encodedTx.byteLength; return { txFeeSats, txFeeRate }; }