Skip to content

Commit

Permalink
feat: add dust output filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Jan 1, 2025
1 parent 920547f commit cf3e113
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/functions/bitcoin/psbt-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
} from '../bitcoin/bitcoin-functions.js';
import { fetchBitcoinTransaction } from './bitcoin-request-functions.js';

const DUST_LIMIT = 546n;

/**
* Creates a Funding Transaction to deposit Bitcoin into an empty Vault.
* Uses the UTXOs of the User to create the Funding Transaction.
Expand Down Expand Up @@ -58,19 +60,16 @@ export async function createFundingTransaction(

const psbtOutputs = [
{ address: multisigAddress, amount: depositAmount },
{
address: feeAddress,
amount: BigInt(feeAmount),
},
];
{ address: feeAddress, amount: BigInt(feeAmount) },
].filter(output => output.amount >= DUST_LIMIT);

const selected = selectUTXO(userUTXOs, psbtOutputs, 'default', {
changeAddress: depositAddress,
feePerByte: feeRate,
bip69: false,
createTx: true,
network: bitcoinNetwork,
dust: 546n as unknown as number,
dust: DUST_LIMIT as unknown as number,
});

if (!selected) {
Expand Down Expand Up @@ -166,7 +165,7 @@ export async function createDepositTransaction(
bip69: false,
createTx: false,
network: bitcoinNetwork,
dust: 546n as unknown as number,
dust: DUST_LIMIT as unknown as number,
});

if (!additionalDepositSelected) {
Expand Down Expand Up @@ -207,15 +206,15 @@ export async function createDepositTransaction(
address: multisigAddress,
amount: BigInt(depositAmount) + BigInt(vaultTransactionOutputValue),
},
];
].filter(output => output.amount >= DUST_LIMIT);

const depositSelected = selectUTXO(depositInputs, depositOutputs, 'all', {
changeAddress: depositAddress,
feePerByte: feeRate,
bip69: false,
createTx: true,
network: bitcoinNetwork,
dust: 546n as unknown as number,
dust: DUST_LIMIT as unknown as number,
});

if (!depositSelected) {
Expand Down Expand Up @@ -327,13 +326,15 @@ export async function createWithdrawTransaction(
});
}

const selected = selectUTXO(inputs, outputs, 'default', {
const filteredOutputs = outputs.filter(output => output.amount >= DUST_LIMIT);

const selected = selectUTXO(inputs, filteredOutputs, 'default', {
changeAddress: withdrawAddress,
feePerByte: feeRate,
bip69: false,
createTx: true,
network: bitcoinNetwork,
dust: 546n as unknown as number,
dust: DUST_LIMIT as unknown as number,
});

if (!selected) {
Expand Down

0 comments on commit cf3e113

Please sign in to comment.