From 92a06c40d19a9a1ae903353985a89311c93a3e5f Mon Sep 17 00:00:00 2001 From: Crypto Minion <154598612+jrwbabylonchain@users.noreply.github.com> Date: Thu, 20 Jun 2024 10:31:12 +1000 Subject: [PATCH] fix: should throw if funds can not cover the fee in withdraw and unbonding (#33) --- package.json | 4 ++-- src/index.ts | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 5040fdf..114b6fc 100644 --- a/package.json +++ b/package.json @@ -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", @@ -45,4 +45,4 @@ "@bitcoin-js/tiny-secp256k1-asmjs": "^2.2.3", "bitcoinjs-lib": "^6.1.5" } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 6085ead..345aa7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { @@ -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!,