Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions packages/tron-wallet-snap/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Allow transaction confirmation after remote simulation failures while retaining security warnings and checks for locally invalid transactions. ([#552](https://github.com/MetaMask/internal-snaps/pull/552))

## [4.3.0]

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ export const TRANSACTION_TAPOS_EXPIRED = 'TransactionTaposExpired';
*
* Used only to select the friendly banner copy in the confirmation dialog's
* `TransactionAlert` (via `getErrorMessage`). It does NOT control the submit
* button: the button is disabled for every failed simulation, including the
* expired/TAPOS-expired case, so the user is blocked from confirming a
* transaction that won't broadcast.
* button: locally detected TAPOS expiry blocks confirmation, while remote
* simulation failures, including contract deadline failures, do not.
*
* @param error - The transaction scan error, if any.
* @returns Whether the error is an expired/deadline failure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('ConfirmSignTransaction', () => {
);
});

it('disables the confirm button when the scan fails on a deadline (expired)', () => {
it('allows confirmation when the remote simulation reports a contract deadline failure', () => {
expect(
isConfirmDisabled(
buildContext({
Expand All @@ -200,21 +200,63 @@ describe('ConfirmSignTransaction', () => {
}),
}),
),
).toBe(true);
).toBe(false);
});

it('disables the confirm button for other failed simulations', () => {
it.each([FetchStatus.Fetched, FetchStatus.Fetching, FetchStatus.Error])(
'allows confirmation after a remote simulation failure when the scan is %s',
(scanFetchStatus) => {
expect(
isConfirmDisabled(
buildContext({
scanFetchStatus,
scan: buildScanResult({
status: 'ERROR',
simulationStatus: SimulationStatus.Failed,
error: otherError,
}),
}),
),
).toBe(false);
},
);

it.each([TRANSACTION_TAPOS_EXPIRED, 'MALFORMED_TRANSACTION'])(
'keeps confirm disabled for local validation failure %s with security alerts off',
(type) => {
expect(
isConfirmDisabled(
buildContext({
preferences: { ...mockPreferences, useSecurityAlerts: false },
scan: buildScanResult({
status: 'ERROR',
simulationStatus: SimulationStatus.Failed,
error: { type, code: null, message: null },
}),
}),
),
).toBe(true);
},
);

it('keeps the initial scan load and local insufficient balance blocking after a remote failure', () => {
const scan = buildScanResult({
status: 'ERROR',
simulationStatus: SimulationStatus.Failed,
error: otherError,
});

expect(
isConfirmDisabled(
buildContext({
scan: buildScanResult({
status: 'ERROR',
simulationStatus: SimulationStatus.Failed,
error: otherError,
}),
}),
buildContext({ scan, scanFetchStatus: FetchStatus.Loading }),
),
).toBe(true);
expect(
isConfirmDisabled(buildContext({ scan, isInsufficientBalance: true })),
).toBe(true);
expect(renderTexts(buildContext({ scan }))).toContain(
SIMULATION_ERROR_TITLE,
);
});

it('renders the expiry banner with the friendly message even when security alerts are off', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@metamask/snaps-sdk/jsx';

import { Networks } from '../../../../constants';
import { TRANSACTION_TAPOS_EXPIRED } from '../../../../services/transaction-scan/isTransactionDeadlinePassedError';
import { SimulationStatus } from '../../../../services/transaction-scan/types';
import { TRX_IMAGE_SVG } from '../../../../static/tron-logo';
import { FetchStatus } from '../../../../types/snap';
Expand Down Expand Up @@ -47,14 +48,13 @@ export const ConfirmSignTransaction = ({
} = context;

/**
* Only disable the confirm button on the first load (FetchStatus.Loading),
* not during subsequent cron re-scans (FetchStatus.Fetching), and whenever
* the simulation has failed — including the expired/TAPOS-expired case, so
* the user is blocked from confirming a transaction that won't broadcast.
* Block the initial scan load and local validation failures. Remote
* simulation failures and background refreshes do not block confirmation.
*/
const shouldDisableConfirmButton =
scanFetchStatus === FetchStatus.Loading ||
scan?.simulationStatus === SimulationStatus.Failed ||
scan?.error?.type === TRANSACTION_TAPOS_EXPIRED ||
scan?.error?.type === 'MALFORMED_TRANSACTION' ||
isInsufficientBalance;

const addressCaip10 = account ? `${scope}:${account.address}` : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DEFAULT_METAMASK_ORIGIN } from '@metamask/snap-networks-utils';

import { Network } from '../../../../constants';
import { TRANSACTION_TAPOS_EXPIRED } from '../../../../services/transaction-scan/isTransactionDeadlinePassedError';
import { SimulationStatus } from '../../../../services/transaction-scan/types';
import type { TransactionScanResult } from '../../../../services/transaction-scan/types';
import { FetchStatus } from '../../../../types/snap';
Expand Down Expand Up @@ -178,25 +179,60 @@ describe('ConfirmTransactionRequest', () => {
expect(serialized).toContain('"disabled":true');
});

it('disables confirm button when scan status is ERROR', () => {
const errorScanResult: TransactionScanResult = {
...mockScanResult,
status: 'ERROR',
simulationStatus: SimulationStatus.Failed,
};
it.each([
[FetchStatus.Fetched, 'confirmation.simulationErrorTitle'],
[FetchStatus.Fetching, 'confirmation.securityScanInProgressTitle'],
[FetchStatus.Error, 'confirmation.simulationTitleAPIError'],
])(
'enables confirm after a remote simulation failure when the scan is %s',
(scanFetchStatus, bannerTitle) => {
const errorScanResult: TransactionScanResult = {
...mockScanResult,
status: 'ERROR',
simulationStatus: SimulationStatus.Failed,
error: {
type: 'Revert',
code: 'INSUFFICIENT_BALANCE',
message: 'Reverted: insufficient balance',
},
};

const context: ConfirmTransactionRequestContext = {
...baseContext,
scan: errorScanResult,
scanFetchStatus: FetchStatus.Fetched,
};
const context: ConfirmTransactionRequestContext = {
...baseContext,
scan: errorScanResult,
scanFetchStatus,
};

const result = ConfirmTransactionRequest({ context });
const serialized = JSON.stringify(result);
const result = ConfirmTransactionRequest({ context });
const serialized = JSON.stringify(result);

// The confirm button should have disabled=true
expect(serialized).toContain('"disabled":true');
});
expect(serialized).not.toContain('"disabled":true');
expect(serialized).toContain(bannerTitle);
},
);

it.each([TRANSACTION_TAPOS_EXPIRED, 'MALFORMED_TRANSACTION'])(
'shows an error banner and keeps confirm disabled for local validation failure %s with security alerts off',
(type) => {
const context: ConfirmTransactionRequestContext = {
...baseContext,
preferences: { ...mockPreferences, useSecurityAlerts: false },
scan: {
...mockScanResult,
status: 'ERROR',
simulationStatus: SimulationStatus.Failed,
error: { type, code: null, message: null },
},
};

const serialized = JSON.stringify(ConfirmTransactionRequest({ context }));

expect(serialized).toContain('"disabled":true');
expect(serialized).toContain('confirmation.simulationErrorTitle');
expect(serialized).toContain('confirmation.simulationErrorSubtitle');
expect(serialized).toContain('"severity":"warning"');
},
);

it('enables confirm button when scan is successful', () => {
const result = ConfirmTransactionRequest({ context: baseContext });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@metamask/snaps-sdk/jsx';

import { Networks } from '../../../../constants';
import { TRANSACTION_TAPOS_EXPIRED } from '../../../../services/transaction-scan/isTransactionDeadlinePassedError';
import { SimulationStatus } from '../../../../services/transaction-scan/types';
import { TRX_IMAGE_SVG } from '../../../../static/tron-logo';
import { FetchStatus } from '../../../../types/snap';
Expand Down Expand Up @@ -47,12 +48,13 @@ export const ConfirmTransactionRequest = ({
const translate = i18n(preferences.locale);

/**
* Only disable confirm button upon first load (FetchStatus.Loading)
* as opposed to subsequent loads (FetchStatus.Fetching)
* Block the initial scan load and locally invalid transactions. Remote
* simulation failures and background refreshes do not block confirmation.
*/
const shouldDisableConfirmButton =
scanFetchStatus === FetchStatus.Loading ||
scan?.simulationStatus === SimulationStatus.Failed;
scan?.error?.type === TRANSACTION_TAPOS_EXPIRED ||
scan?.error?.type === 'MALFORMED_TRANSACTION';

let estimatedChangesSection: ComponentOrElement | null = null;
if (preferences.simulateOnChainActions) {
Expand Down Expand Up @@ -90,7 +92,7 @@ export const ConfirmTransactionRequest = ({
<Container>
<Box>
{/* Security Alert */}
{preferences.useSecurityAlerts ? (
{preferences.useSecurityAlerts || scan?.error ? (
<TransactionAlert
{...getTransactionAlertProps({
preferences,
Expand Down
Loading