Skip to content

Commit b1ac773

Browse files
committed
feat/fix: add fee checker
1 parent 0b111e7 commit b1ac773

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/hooks/useBsv.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const useBsv = () => {
7878
const amount = request.reduce((a, r) => a + r.satAmount, 0);
7979

8080
// Format in and outs
81-
const utxos = await getUtxos(fromAddress);
81+
const utxos: WocUtxo[] = await getUtxos(fromAddress);
8282

8383
const script = P2PKHAddress.from_string(fromAddress)
8484
.get_locking_script()
@@ -100,6 +100,7 @@ export const useBsv = () => {
100100
(a: number, item: UTXO) => a + item.satoshis,
101101
0
102102
);
103+
103104
if (totalSats < amount) {
104105
return { error: "insufficient-funds" };
105106
}
@@ -132,9 +133,9 @@ export const useBsv = () => {
132133
);
133134
}
134135

135-
// build txins from our UTXOs
136+
// build txins from our inputs
136137
let idx = 0;
137-
for (let u of fundingUtxos || []) {
138+
for (let u of inputs || []) {
138139
const inTx = new TxIn(
139140
Buffer.from(u.txid, "hex"),
140141
u.vout,
@@ -161,6 +162,13 @@ export const useBsv = () => {
161162
idx++;
162163
}
163164

165+
// Fee checker
166+
const finalSatsIn = tx.satoshis_in() ?? 0n;
167+
const finalSatsOut = tx.satoshis_out() ?? 0n;
168+
if (finalSatsIn - finalSatsOut > 500) {
169+
return { error: "fee-to-high" };
170+
}
171+
164172
const txhex = tx.to_hex();
165173
const txid = await broadcastRawTx(txhex);
166174

src/pages/BsvWallet.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ export const BsvWallet = (props: BsvWalletProps) => {
221221
? "Invalid Password!"
222222
: sendRes.error === "insufficient-funds"
223223
? "Insufficient Funds!"
224+
: sendRes.error === "fee-to-high"
225+
? "Miner fee to high!"
224226
: "An unknown error has occurred! Try again.";
225227

226228
addSnackbar(message, "error");

src/pages/requests/BsvSendRequest.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ export const BsvSendRequest = (props: BsvSendRequestProps) => {
124124
? "Invalid Password!"
125125
: sendRes.error === "insufficient-funds"
126126
? "Insufficient Funds!"
127+
: sendRes.error === "fee-to-high"
128+
? "Miner fee to high!"
127129
: "An unknown error has occurred! Try again.";
128130

129131
addSnackbar(message, "error");

0 commit comments

Comments
 (0)