Skip to content

Commit

Permalink
fix: catch broadcast errors
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarkhanzadian committed Dec 26, 2024
1 parent e6ed779 commit 9e076d2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
42 changes: 28 additions & 14 deletions apps/mobile/src/features/psbt-signer/psbt-signer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMemo, useRef, useState } from 'react';

import { useGenerateBtcUnsignedTransactionNativeSegwit } from '@/common/transactions/bitcoin-transactions.hooks';
import { formatBalance } from '@/components/balance/balance';
import { useToastContext } from '@/components/toast/toast-context';
import { ApproverAccountCard } from '@/features/approver/components/approver-account-card';
import { BitcoinOutcome } from '@/features/approver/components/bitcoin-outcome';
import { BitcoinFeeCard } from '@/features/approver/components/fees/bitcoin-fee-card';
Expand Down Expand Up @@ -91,6 +92,7 @@ export function BasePsbtSigner({
const psbtPayers = usePsbtPayers({ psbtHex });
const psbtAddresses = psbtPayers.map(payer => payer.address);

const { displayToast } = useToastContext();
if (!psbtAccounts[0]) throw new Error('No psbt accounts');
if (psbtAccounts.length > 1)
throw new Error('Only one psbt account as input is supported right now');
Expand Down Expand Up @@ -180,20 +182,32 @@ export function BasePsbtSigner({

async function onSubmitTransaction() {
setApproverState('submitting');
const psbt = getPsbtAsTransaction(psbtHex);
const signedTx = await sign(psbt.toPSBT());
signedTx.finalize();
await broadcastTx({
// TODO: for now
skipSpendableCheckUtxoIds: 'all',
onSuccess() {
setApproverState('submitted');
setTimeout(() => {
onSuccess();
}, 1000);
},
tx: signedTx.hex,
});
try {
const psbt = getPsbtAsTransaction(psbtHex);
const signedTx = await sign(psbt.toPSBT());
signedTx.finalize();

await broadcastTx({
// TODO: for now
skipSpendableCheckUtxoIds: 'all',
tx: signedTx.hex,
onSuccess() {
setApproverState('submitted');
setTimeout(() => {
onSuccess();
}, 1000);
},
onError() {
displayToast({ title: 'Failed to broadcast transaction', type: 'error' });

Check failure on line 201 in apps/mobile/src/features/psbt-signer/psbt-signer.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

disallow literal string
setApproverState('start');
},
});
} catch {
/* eslint-disable-next-line no-console */
console.log(e);
displayToast({ title: 'Failed to broadcast transaction', type: 'error' });

Check failure on line 208 in apps/mobile/src/features/psbt-signer/psbt-signer.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

disallow literal string
setApproverState('start');
}
}

return (
Expand Down
21 changes: 14 additions & 7 deletions apps/mobile/src/features/stacks-tx-signer/stacks-tx-signer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ export function StacksTxSigner({
const newTxHex = bytesToHex(newTx.serialize());
setTxHex(newTxHex);
}
} catch (e) {
} catch {
displayToast({ title: 'Failed to change nonce', type: 'error' });

Check failure on line 158 in apps/mobile/src/features/stacks-tx-signer/stacks-tx-signer.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

disallow literal string
console.log(e);
}
}
async function onChangeMemo(memo: string) {
Expand Down Expand Up @@ -188,16 +187,24 @@ export function StacksTxSigner({
if (!signer) {
throw new Error('No signer provided');
}

try {
const signedTx = await signer?.sign(tx);
await broadcastTransaction({ tx: signedTx, stacksNetwork });
setApproverState('submitted');
setTimeout(() => {
onSuccess();
}, 1000);
await broadcastTransaction(
{ tx: signedTx, stacksNetwork },
{
onSuccess() {
setApproverState('submitted');
setTimeout(() => {
onSuccess();
}, 1000);
},
}
);
} catch (e) {
/* eslint-disable-next-line no-console */
console.log(e);
displayToast({ title: 'Failed to broadcast transaction', type: 'error' });

Check failure on line 207 in apps/mobile/src/features/stacks-tx-signer/stacks-tx-signer.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

disallow literal string
setApproverState('start');
}
}
Expand Down

0 comments on commit 9e076d2

Please sign in to comment.