Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ui/hooks/activity/useTransactionMeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useSelector } from 'react-redux';
import { selectLocalTransactionsByHash } from '../../selectors/activity';

/**
* Used for enriching activity items with local-only data.
* This is where `metamaskPay` and other per-transaction metadata live.
*
* @param hash - The activity item hash
*/
export function useTransactionMeta(hash: string | undefined) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted from other locally-enriched detail pages

const localTransactionsByHash = useSelector(selectLocalTransactionsByHash);
return localTransactionsByHash.get((hash ?? '').toLowerCase())
?.initialTransaction;
}
5 changes: 5 additions & 0 deletions ui/pages/details/components/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP } from '../../../../shared/const
import { formatBlockExplorerTransactionUrl } from '../../../../shared/lib/multichain/networks';
import { isValidTransactionHash } from '../../../../shared/lib/transactions.utils';

/**
* Fiat currency for `metamaskPay` is always USD
*/
export const PAY_FIAT_CURRENCY = 'usd';

export function getExplorerTxUrl({
chainId,
txHash,
Expand Down
124 changes: 124 additions & 0 deletions ui/pages/details/templates/convert-details.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import React, { useMemo } from 'react';
import { toEvmCaipChainId } from '@metamask/multichain-network-controller';
import type {
ActivityListItem,
TokenAmount,
} from '../../../../shared/lib/activity/types';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { useFormatters } from '../../../hooks/useFormatters';
import { useTransactionMeta } from '../../../hooks/activity/useTransactionMeta';
import { useTransactionQuery } from '../../../hooks/activity/useTransactionQuery';
import { Footer, PAY_FIAT_CURRENCY, Row, Section } from '../components/shared';
import { ConvertAgainButton } from '../components/convert-again-button';
import { MetadataSection, TokensSection } from '../components/sections';
// eslint-disable-next-line import-x/no-restricted-paths
import { TransactionDetailsProvider } from '../../confirmations/components/activity/transaction-details-context';
// eslint-disable-next-line import-x/no-restricted-paths
import { TransactionDetailsSummary } from '../../confirmations/components/activity/transaction-details-summary';

function useSentToken(
baseToken: TokenAmount | undefined,
transactionMeta: ReturnType<typeof useTransactionMeta>,
): TokenAmount | undefined {
const { sourceHash, tokenAddress, chainId } =
transactionMeta?.metamaskPay ?? {};
const sourceChainId = chainId ? toEvmCaipChainId(chainId) : undefined;
const userAddress = transactionMeta?.txParams?.from?.toLowerCase();

const { data: sourceTransaction } = useTransactionQuery({
chainId: sourceChainId,
txHash: sourceHash,
enabled: Boolean(sourceHash && sourceChainId),
});

return useMemo(() => {
if (!baseToken) {
return baseToken;
}
const transfers = sourceTransaction?.valueTransfers ?? [];
const payToken = tokenAddress?.toLowerCase();
const sentTransfer =
transfers.find(
(transfer) =>
transfer.contractAddress.toLowerCase() === payToken &&
transfer.from.toLowerCase() === userAddress,
) ??
transfers.find(
(transfer) => transfer.contractAddress.toLowerCase() === payToken,
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
);
return sentTransfer?.amount
? { ...baseToken, amount: sentTransfer.amount }
: baseToken;
}, [baseToken, sourceTransaction, tokenAddress, userAddress]);
}

type Props = {
item: Extract<
ActivityListItem,
{
type:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convert is currently shared in this union type

| 'swap'
| 'bridge'
| 'convert'
| 'lendingDeposit'
| 'lendingWithdrawal'
| 'wrap'
| 'unwrap';
}
>;
};

export function ConvertDetails({ item }: Props) {

Check warning on line 71 in ui/pages/details/templates/convert-details.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Mark the props of the component as read-only.

See more on https://sonarcloud.io/project/issues?id=MetaMask_metamask-extension&issues=AZ9uEivE4e9EUCVmu183&open=AZ9uEivE4e9EUCVmu183&pullRequest=44586
const t = useI18nContext();
const { formatCurrencyWithMinThreshold } = useFormatters();
const transactionMeta = useTransactionMeta(item.hash);
const { networkFeeFiat, totalFiat } = transactionMeta?.metamaskPay ?? {};
const sentToken = useSentToken(item.data.sourceToken, transactionMeta);

const formatFiat = (value?: string) =>
value
? formatCurrencyWithMinThreshold(Number(value), PAY_FIAT_CURRENCY)
: null;

if (!transactionMeta) {
return null;
}
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated

return (
<div className="flex grow flex-col">
<div className="divide-y divide-border-muted">
<TokensSection
tokens={[
{ label: t('youSent'), token: sentToken },
{ label: t('youReceived'), token: item.data.destinationToken },
]}
/>

<MetadataSection item={item} />

<Section>
<Row
label={t('networkFee')}
testId="transaction-base-fee"
value={formatFiat(networkFeeFiat)}
/>
<Row
label={t('total')}
testId="transaction-breakdown-value-amount"
value={formatFiat(totalFiat)}
/>
</Section>

<Section>
<TransactionDetailsProvider transactionMeta={transactionMeta}>
<TransactionDetailsSummary />
</TransactionDetailsProvider>
</Section>
</div>

<Footer>
<ConvertAgainButton sourceToken={item.data.sourceToken} />
</Footer>
</div>
);
}
8 changes: 1 addition & 7 deletions ui/pages/details/templates/perps-deposit-details.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { toEvmCaipChainId } from '@metamask/multichain-network-controller';
import {
Button,
Expand All @@ -12,7 +11,7 @@ import type { ActivityListItem } from '../../../../shared/lib/activity/types';
import { CHAIN_IDS } from '../../../../shared/constants/network';
import { ActivityAvatar } from '../../../components/app/activity-list-item-avatar';
import { usePerpsDepositConfirmation } from '../../../components/app/perps/hooks/usePerpsDepositConfirmation';
import { selectLocalTransactionsByHash } from '../../../selectors/activity';
import { useTransactionMeta } from '../../../hooks/activity/useTransactionMeta';
// eslint-disable-next-line import-x/no-restricted-paths
import { TransactionDetailsProvider } from '../../confirmations/components/activity/transaction-details-context';
// eslint-disable-next-line import-x/no-restricted-paths
Expand All @@ -36,11 +35,6 @@ type Props = {
>;
};

function useTransactionMeta(hash: string | undefined) {

@n3ps n3ps Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted into useLocalTransactionMeta

const localTransactions = useSelector(selectLocalTransactionsByHash);
return localTransactions.get(hash || '')?.initialTransaction;
}

export function PerpsDepositDetails({ item }: Readonly<Props>) {
const t = useI18nContext();
const { trigger: triggerDeposit } = usePerpsDepositConfirmation();
Expand Down
8 changes: 1 addition & 7 deletions ui/pages/details/templates/perps-details.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useMemo } from 'react';
import { useSelector } from 'react-redux';
import { toEvmCaipChainId } from '@metamask/multichain-network-controller';
import type {
ActivityListItem,
Expand All @@ -9,7 +8,7 @@ import { toAssetId } from '../../../../shared/lib/asset-utils';
import { AccountName } from '../../../components/app/transaction/account-name';
import { NetworkName } from '../../../components/app/transaction/network-name';
import { TransactionStatus } from '../../../components/app/transaction/transaction-status';
import { selectLocalTransactionsByHash } from '../../../selectors/activity';
import { useTransactionMeta } from '../../../hooks/activity/useTransactionMeta';
import {
ARBITRUM_USDC,
PERPS_CURRENCY,
Expand All @@ -27,11 +26,6 @@ const ARBITRUM_USDC_ASSET_ID = toAssetId(
toEvmCaipChainId(ARBITRUM_USDC.chainId),
);

function useTransactionMeta(hash: string | undefined) {

@n3ps n3ps Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted into useLocalTransactionMeta

const localTransactions = useSelector(selectLocalTransactionsByHash);
return localTransactions.get(hash || '')?.initialTransaction;
}

export function PerpsDetails({
item,
}: {
Expand Down
14 changes: 4 additions & 10 deletions ui/pages/details/templates/swap-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useI18nContext } from '../../../hooks/useI18nContext';
import { FeesRows, TotalAmountRow } from '../components/amounts-section';
import { Footer, Section } from '../components/shared';
import { BlockExplorerButton } from '../components/block-explorer-button';
import { ConvertAgainButton } from '../components/convert-again-button';
import { SwapAgainButton } from '../components/swap-again-button';
import { MetadataSection, TokensSection } from '../components/sections';

Expand Down Expand Up @@ -44,15 +43,10 @@ export function SwapDetails({
</div>
<Footer>
<BlockExplorerButton chainId={item.chainId} txHash={item.hash} />

{item.type === 'convert' ? (
<ConvertAgainButton sourceToken={item.data.sourceToken} />
) : (
<SwapAgainButton
sourceToken={item.data.sourceToken}
destinationToken={item.data.destinationToken}
/>
)}
<SwapAgainButton
sourceToken={item.data.sourceToken}
destinationToken={item.data.destinationToken}
/>
</Footer>
</div>
);
Expand Down
4 changes: 3 additions & 1 deletion ui/pages/details/templates/template-loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import type { ActivityListItem } from '../../../../shared/lib/activity/types';
import { ApprovalDetails } from './approval-details';
import { BridgeDetails } from './bridge-details/bridge-details';
import { ConvertDetails } from './convert-details';
import { DefaultDetails } from './default-details';
import { NftDetails } from './nft-details';
import { PerpsDepositDetails } from './perps-deposit-details';
Expand Down Expand Up @@ -29,8 +30,9 @@ export function TemplateLoader({ item }: Props) {
return <SendDetails item={item} />;
case 'bridge':
return <BridgeDetails item={item} />;
case 'swap':
case 'convert':
return <ConvertDetails item={item} />;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point of interest

case 'swap':
case 'lendingDeposit':
case 'lendingWithdrawal':
case 'wrap':
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/details/transaction-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
} from '../../selectors/activity';
import ErrorBoundary from '../../components/app/error-boundary/error-boundary';
import { useCachedEvmTransaction } from '../../hooks/activity/useCachedEvmTransaction';
import { useTransactionQuery } from '../../hooks/activity/useTransactionQuery';
import { Header } from './components/header';
import { TemplateLoader } from './templates/template-loader';
import { useTransactionQuery } from './useTransactionQuery';

type Props = {
chainId: string | undefined;
Expand Down
Loading