Skip to content

Commit

Permalink
fix: should throw if funds can not cover the fee in withdraw and unbo…
Browse files Browse the repository at this point in the history
…nding (#33)
  • Loading branch information
jrwbabylonlab authored Jun 20, 2024
1 parent 6494df2 commit 92a06c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btc-staking-ts",
"version": "0.2.2",
"version": "0.2.3",
"description": "Library exposing methods for the creation and consumption of Bitcoin transactions pertaining to Babylon's Bitcoin Staking protocol. Experimental version, should not be used for production purposes or with real funds.",
"module": "dist/index.js",
"main": "dist/index.cjs",
Expand Down Expand Up @@ -45,4 +45,4 @@
"@bitcoin-js/tiny-secp256k1-asmjs": "^2.2.3",
"bitcoinjs-lib": "^6.1.5"
}
}
}
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,13 @@ function withdrawalTransaction(
}
// withdraw tx always has 1 output only
const estimatedFee = getEstimatedFee(feeRate, psbt.txInputs.length, 1);
const value = tx.outs[outputIndex].value - estimatedFee;
if (!value) {
throw new Error("Not enough funds to cover the fee for withdrawal transaction");
}
psbt.addOutput({
address: withdrawalAddress,
value: tx.outs[outputIndex].value - estimatedFee,
value,
});

return {
Expand Down Expand Up @@ -699,7 +703,10 @@ export function unbondingTransaction(
scriptTree: outputScriptTree,
network,
});

const value = stakingTx.outs[outputIndex].value - transactionFee;
if (!value) {
throw new Error("Not enough funds to cover the fee for unbonding transaction");
}
// Add the unbonding output
psbt.addOutput({
address: unbondingOutput.address!,
Expand Down

0 comments on commit 92a06c4

Please sign in to comment.