Skip to content

Commit

Permalink
feat/fix: add fee checker
Browse files Browse the repository at this point in the history
  • Loading branch information
danwag06 committed Oct 12, 2023
1 parent 0b111e7 commit b1ac773
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/hooks/useBsv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -100,6 +100,7 @@ export const useBsv = () => {
(a: number, item: UTXO) => a + item.satoshis,
0
);

if (totalSats < amount) {
return { error: "insufficient-funds" };
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/pages/BsvWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions src/pages/requests/BsvSendRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit b1ac773

Please sign in to comment.