diff --git a/src/hooks/useBsv.ts b/src/hooks/useBsv.ts index e6b9e32d..278831b3 100644 --- a/src/hooks/useBsv.ts +++ b/src/hooks/useBsv.ts @@ -78,7 +78,7 @@ export const useBsv = () => { const amount = request.reduce((a, r) => a + r.satAmount, 0); // Format in and outs - const utxos = await getUtxos(fromAddress); + const utxos: WocUtxo[] = await getUtxos(fromAddress); const script = P2PKHAddress.from_string(fromAddress) .get_locking_script() @@ -100,6 +100,7 @@ export const useBsv = () => { (a: number, item: UTXO) => a + item.satoshis, 0 ); + if (totalSats < amount) { return { error: "insufficient-funds" }; } @@ -132,9 +133,9 @@ export const useBsv = () => { ); } - // build txins from our UTXOs + // build txins from our inputs let idx = 0; - for (let u of fundingUtxos || []) { + for (let u of inputs || []) { const inTx = new TxIn( Buffer.from(u.txid, "hex"), u.vout, @@ -161,6 +162,13 @@ export const useBsv = () => { idx++; } + // Fee checker + const finalSatsIn = tx.satoshis_in() ?? 0n; + const finalSatsOut = tx.satoshis_out() ?? 0n; + if (finalSatsIn - finalSatsOut > 500) { + return { error: "fee-to-high" }; + } + const txhex = tx.to_hex(); const txid = await broadcastRawTx(txhex); diff --git a/src/pages/BsvWallet.tsx b/src/pages/BsvWallet.tsx index ea5f305b..5f5833db 100644 --- a/src/pages/BsvWallet.tsx +++ b/src/pages/BsvWallet.tsx @@ -221,6 +221,8 @@ export const BsvWallet = (props: BsvWalletProps) => { ? "Invalid Password!" : sendRes.error === "insufficient-funds" ? "Insufficient Funds!" + : sendRes.error === "fee-to-high" + ? "Miner fee to high!" : "An unknown error has occurred! Try again."; addSnackbar(message, "error"); diff --git a/src/pages/requests/BsvSendRequest.tsx b/src/pages/requests/BsvSendRequest.tsx index d5b7dc39..d45bbfdb 100644 --- a/src/pages/requests/BsvSendRequest.tsx +++ b/src/pages/requests/BsvSendRequest.tsx @@ -124,6 +124,8 @@ export const BsvSendRequest = (props: BsvSendRequestProps) => { ? "Invalid Password!" : sendRes.error === "insufficient-funds" ? "Insufficient Funds!" + : sendRes.error === "fee-to-high" + ? "Miner fee to high!" : "An unknown error has occurred! Try again."; addSnackbar(message, "error");