Skip to content

Commit

Permalink
feat: why did my tx fail
Browse files Browse the repository at this point in the history
  • Loading branch information
He1DAr committed Dec 4, 2024
1 parent 9b33055 commit b6080b6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/app/txid/[txId]/Alert.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import * as React from 'react';
import { ReactNode } from 'react';

import { Alert, AlertProps } from '../../../ui/Alert';
import { AlertDescription } from '../../../ui/AlertDescription';
Expand All @@ -11,9 +12,15 @@ interface AlertError {
message?: string;
}

export function AlertBase({ status, message }: { status: AlertProps['status']; message: string }) {
export function AlertBase({

Check warning on line 15 in src/app/txid/[txId]/Alert.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/Alert.tsx#L15

Added line #L15 was not covered by tests
status,
message,
}: {
status: AlertProps['status'];
message: string | ReactNode;
}) {

Check warning on line 21 in src/app/txid/[txId]/Alert.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/Alert.tsx#L21

Added line #L21 was not covered by tests
return (
<Alert status={status} rounded={'lg'}>
<Alert status={status} rounded={'lg'} alignItems={'flexStart'}>
<AlertIcon />
<AlertDescription fontSize={'sm'}>{message}</AlertDescription>
</Alert>
Expand Down
36 changes: 32 additions & 4 deletions src/app/txid/[txId]/TxAlerts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import { MempoolTransaction, Transaction } from '@stacks/stacks-blockchain-api-t
import { TransactionStatus } from '../../../common/constants/constants';
import { useGlobalContext } from '../../../common/context/useGlobalContext';
import { getTransactionStatus } from '../../../common/utils/transactions';
import { ListItem } from '../../../ui/ListItem';
import { Text } from '../../../ui/Text';
import { UnorderedList } from '../../../ui/UnorderedList';

Check warning on line 12 in src/app/txid/[txId]/TxAlerts.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/TxAlerts.tsx#L10-L12

Added lines #L10 - L12 were not covered by tests
import { ExplorerErrorBoundary } from '../../_components/ErrorBoundary';
import { AlertBase } from './Alert';
import { useWhyDidMyTxFail } from './useWhyDidMyTxFail';

Check warning on line 15 in src/app/txid/[txId]/TxAlerts.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/TxAlerts.tsx#L15

Added line #L15 was not covered by tests

interface TxAlertsBaseProps {
tx: Transaction | MempoolTransaction;
}

function TxAlertsBase({ tx }: TxAlertsBaseProps) {
const networkMode = useGlobalContext().activeNetwork.mode;
// for testnet, show after 4 hours. for mainnet, show after 24 hours
Expand All @@ -26,16 +31,39 @@ function TxAlertsBase({ tx }: TxAlertsBaseProps) {
(networkMode === 'testnet' ? HOURS_NOTICE_TESTNET : HOURS_NOTICE_MAINNET);
const isNonCanonical = txStatus === TransactionStatus.NON_CANONICAL;

const { data } = useWhyDidMyTxFail(tx.tx_id);

Check warning on line 34 in src/app/txid/[txId]/TxAlerts.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/TxAlerts.tsx#L34

Added line #L34 was not covered by tests
const messages = (
data?.results?.map((result: { message?: string }) => result.message) || []
)?.filter((message: string) => !!message);

Check warning on line 37 in src/app/txid/[txId]/TxAlerts.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/TxAlerts.tsx#L37

Added line #L37 was not covered by tests

const failedMessage =
tx.tx_status === 'abort_by_response'
? 'This transaction did not succeed because the transaction was aborted during its execution.'
: 'This transaction would have succeeded, but was rolled back by a supplied post-condition.';
? 'This transaction did not succeed because the transaction was aborted during its execution'
: 'This transaction would have succeeded, but was rolled back by a supplied post-condition';

Check warning on line 42 in src/app/txid/[txId]/TxAlerts.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/TxAlerts.tsx#L41-L42

Added lines #L41 - L42 were not covered by tests

const longPendingMessage =
'Transactions that cannot be confirmed within 256 blocks are eventually canceled automatically.';
'Transactions that cannot be confirmed within 256 blocks are eventually canceled automatically';

Check warning on line 45 in src/app/txid/[txId]/TxAlerts.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/TxAlerts.tsx#L45

Added line #L45 was not covered by tests

const nonCanonicalMessage =
'This transaction is in a non-canonical fork. It is not in the canonical Stacks chain.';
'This transaction is in a non-canonical fork. It is not in the canonical Stacks chain';

Check warning on line 48 in src/app/txid/[txId]/TxAlerts.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/TxAlerts.tsx#L48

Added line #L48 was not covered by tests

if (messages?.length) {
return (
<AlertBase
status={'warning'}
message={
<>
<Text>{failedMessage}:</Text>
<UnorderedList mt={2}>
{messages.map((message: string) => (
<ListItem>{message}</ListItem>
))}
</UnorderedList>
</>
}
/>
);
}

return (
<>
Expand Down
15 changes: 15 additions & 0 deletions src/app/txid/[txId]/useWhyDidMyTxFail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useQuery } from '@tanstack/react-query';

Check warning on line 1 in src/app/txid/[txId]/useWhyDidMyTxFail.ts

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/useWhyDidMyTxFail.ts#L1

Added line #L1 was not covered by tests

import { useGlobalContext } from '../../../common/context/useGlobalContext';

Check warning on line 3 in src/app/txid/[txId]/useWhyDidMyTxFail.ts

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/useWhyDidMyTxFail.ts#L3

Added line #L3 was not covered by tests

export function useWhyDidMyTxFail(txId: string) {
const { url: activeNetworkUrl } = useGlobalContext().activeNetwork;
const txIdWithoutPrefix = txId.replace('0x', '');
return useQuery({

Check warning on line 8 in src/app/txid/[txId]/useWhyDidMyTxFail.ts

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/useWhyDidMyTxFail.ts#L5-L8

Added lines #L5 - L8 were not covered by tests
queryKey: ['whyDidMyTxFail', txIdWithoutPrefix],
queryFn: () =>
fetch(`${activeNetworkUrl}/whydidmytxfail/tx/${txIdWithoutPrefix}/logs`).then(res =>
res.json()

Check warning on line 12 in src/app/txid/[txId]/useWhyDidMyTxFail.ts

View check run for this annotation

Codecov / codecov/patch

src/app/txid/[txId]/useWhyDidMyTxFail.ts#L11-L12

Added lines #L11 - L12 were not covered by tests
),
});
}

0 comments on commit b6080b6

Please sign in to comment.