Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mark transactions as failed for cancelled / unknown smart transactions #12664

Merged
merged 1 commit into from
Dec 12, 2024
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
297 changes: 297 additions & 0 deletions app/store/migrations/063.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
import migrate from './063';
import { merge } from 'lodash';
import { captureException } from '@sentry/react-native';
import initialRootState from '../../util/test/initial-root-state';
import { SmartTransactionStatuses } from '@metamask/smart-transactions-controller/dist/types';
import { TransactionStatus, CHAIN_IDS } from '@metamask/transaction-controller';

const expectedState = {
engine: {
backgroundState: {
TransactionController: {
transactions: [
{
chainId: CHAIN_IDS.MAINNET,
id: '1',
origin: 'test.com',
status: TransactionStatus.confirmed,
time: 1631714312,
txParams: {
from: '0x1',
},
hash: '0x2',
rawTx: '0x3',
},
{
chainId: CHAIN_IDS.LINEA_MAINNET,
id: '2',
origin: 'test.com',
status: TransactionStatus.confirmed,
time: 1631714312,
txParams: {
from: '0x1',
},
hash: '0x3',
},
{
chainId: CHAIN_IDS.MAINNET,
id: '3',
origin: 'test2.com',
status: TransactionStatus.failed,
time: 1631714313,
txParams: {
from: '0x6',
},
hash: '0x4',
rawTx: '0x5',
error: {
name: 'SmartTransactionCancelled',
message:
'Smart transaction cancelled. Previous status: submitted',
},
},
{
chainId: CHAIN_IDS.MAINNET,
id: '4',
origin: 'test2.com',
status: TransactionStatus.failed,
time: 1631714313,
txParams: {
from: '0x6',
},
hash: '0x5',
rawTx: '0x6',
error: {
name: 'SmartTransactionCancelled',
message: 'Smart transaction cancelled. Previous status: signed',
},
},
{
chainId: CHAIN_IDS.MAINNET,
id: '5',
origin: 'test2.com',
status: TransactionStatus.failed,
time: 1631714313,
txParams: {
from: '0x6',
},
hash: '0x6',
rawTx: '0x7',
error: {
name: 'SmartTransactionCancelled',
message: 'Smart transaction cancelled. Previous status: signed',
},
},
],
},
SmartTransactionsController: {
smartTransactionsState: {
smartTransactions: {
[CHAIN_IDS.MAINNET]: [
{
txHash: '0x2',
status: SmartTransactionStatuses.SUCCESS,
},
{
txHash: '0x4',
status: SmartTransactionStatuses.CANCELLED,
},
{
txHash: '0x5',
status: SmartTransactionStatuses.CANCELLED,
},
{
txHash: '0x6',
status: SmartTransactionStatuses.UNKNOWN,
},
],
},
},
},
},
},
};

jest.mock('@sentry/react-native', () => ({
captureException: jest.fn(),
}));
const mockedCaptureException = jest.mocked(captureException);

describe('Migration #63', () => {
beforeEach(() => {
jest.restoreAllMocks();
jest.resetAllMocks();
});

const invalidStates = [
{
state: null,
errorMessage: "FATAL ERROR: Migration 63: Invalid state error: 'object'",
scenario: 'state is invalid',
},
{
state: merge({}, initialRootState, {
engine: null,
}),
errorMessage:
"FATAL ERROR: Migration 63: Invalid engine state error: 'object'",
scenario: 'engine state is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: null,
},
}),
errorMessage:
"FATAL ERROR: Migration 63: Invalid engine backgroundState error: 'object'",
scenario: 'backgroundState is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: { TransactionController: null },
},
}),
errorMessage: "Migration 63: Invalid TransactionController state: 'null'",
scenario: 'transactionController is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: { SmartTransactionsController: null },
},
}),
errorMessage:
"Migration 63: Invalid SmartTransactionsController state: 'null'",
scenario: 'smartTransactionsController is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: {
SmartTransactionsController: {
smartTransactionsState: { smartTransactions: null },
},
},
},
}),
errorMessage:
"Migration 63: Missing smart transactions property from SmartTransactionsController: 'object'",
scenario:
'smartTransactionsController.smartTransactionsState.smartTransactions is invalid',
},
];
it.each(invalidStates)(
'captures exception if $scenario',
({ errorMessage, state }) => {
const newState = migrate(state);

expect(newState).toStrictEqual(state);
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error));
expect(mockedCaptureException.mock.calls[0][0].message).toBe(
errorMessage,
);
},
);

it('applies migration, changes transaction status to failed if a smart transaction was cancelled or unknown', () => {
const oldState = {
engine: {
backgroundState: {
TransactionController: {
transactions: [
{
chainId: CHAIN_IDS.MAINNET,
id: '1',
origin: 'test.com',
status: TransactionStatus.confirmed,
time: 1631714312,
txParams: {
from: '0x1',
},
hash: '0x2',
rawTx: '0x3',
},
{
chainId: CHAIN_IDS.LINEA_MAINNET,
id: '2',
origin: 'test.com',
status: TransactionStatus.confirmed,
time: 1631714312,
txParams: {
from: '0x1',
},
hash: '0x3',
},
{
chainId: CHAIN_IDS.MAINNET,
id: '3',
origin: 'test2.com',
status: TransactionStatus.submitted,
time: 1631714313,
txParams: {
from: '0x6',
},
hash: '0x4',
rawTx: '0x5',
},
{
chainId: CHAIN_IDS.MAINNET,
id: '4',
origin: 'test2.com',
status: TransactionStatus.signed,
time: 1631714313,
txParams: {
from: '0x6',
},
hash: '0x5',
rawTx: '0x6',
},
{
dan437 marked this conversation as resolved.
Show resolved Hide resolved
chainId: CHAIN_IDS.MAINNET,
id: '5',
origin: 'test2.com',
status: TransactionStatus.signed,
time: 1631714313,
txParams: {
from: '0x6',
},
hash: '0x6',
rawTx: '0x7',
},
],
},
SmartTransactionsController: {
smartTransactionsState: {
smartTransactions: {
[CHAIN_IDS.MAINNET]: [
{
txHash: '0x2',
status: SmartTransactionStatuses.SUCCESS,
},
{
txHash: '0x4',
status: SmartTransactionStatuses.CANCELLED,
},
{
txHash: '0x5',
status: SmartTransactionStatuses.CANCELLED,
},
{
txHash: '0x6',
status: SmartTransactionStatuses.UNKNOWN,
},
],
},
},
},
},
},
};

const newState = migrate(oldState);

expect(newState).toStrictEqual(expectedState);
});
});
92 changes: 92 additions & 0 deletions app/store/migrations/063.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { isObject } from '@metamask/utils';
import { captureException } from '@sentry/react-native';
import { ensureValidState } from './util';
import { SmartTransactionStatuses } from '@metamask/smart-transactions-controller/dist/types';
import { TransactionStatus, CHAIN_IDS } from '@metamask/transaction-controller';

export default function migrate(state: unknown) {
if (!ensureValidState(state, 63)) {
return state;
}

if (!isObject(state.engine.backgroundState.TransactionController)) {
captureException(
new Error(
`Migration 63: Invalid TransactionController state: '${state.engine.backgroundState.TransactionController}'`,
dan437 marked this conversation as resolved.
Show resolved Hide resolved
),
);
return state;
}

if (!isObject(state.engine.backgroundState.SmartTransactionsController)) {
captureException(
new Error(
`Migration 63: Invalid SmartTransactionsController state: '${state.engine.backgroundState.SmartTransactionsController}'`,
),
);
return state;
}

const transactionControllerState =
state.engine.backgroundState.TransactionController;
Copy link
Member

Choose a reason for hiding this comment

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

Minor, could we assign these at the start to avoid the duplicate property chains?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for feedback, Matt! I just copied the 039.ts TransactionController migration and slightly modified it to get this fix out of the door asap. Will keep your comments in mind for another migration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI, I've resolved your feedback in a follow up PR: #12694

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const smartTransactionsControllerState = state.engine.backgroundState.SmartTransactionsController as any;

if (!Array.isArray(transactionControllerState.transactions)) {
captureException(
new Error(
`Migration 63: Missing transactions property from TransactionController: '${typeof state
.engine.backgroundState.TransactionController}'`,
),
);
return state;
}
const smartTransactions =
smartTransactionsControllerState?.smartTransactionsState?.smartTransactions;
if (!isObject(smartTransactions)) {
captureException(
new Error(
`Migration 63: Missing smart transactions property from SmartTransactionsController: '${typeof state
.engine.backgroundState.SmartTransactionsController?.smartTransactionsState}'`,
),
);
return state;
}

const ethereumMainnetSmartTransactions = smartTransactions[CHAIN_IDS.MAINNET];
if (
!Array.isArray(ethereumMainnetSmartTransactions) ||
ethereumMainnetSmartTransactions.length === 0
) {
// If there are no smart transactions, we can skip this migration.
return state;
dan437 marked this conversation as resolved.
Show resolved Hide resolved
}

const smartTransactionTxHashesForUpdate: Record<string, boolean> = {};
ethereumMainnetSmartTransactions.forEach((smartTransaction) => {
if (
smartTransaction.txHash &&
(smartTransaction.status === SmartTransactionStatuses.CANCELLED ||
dan437 marked this conversation as resolved.
Show resolved Hide resolved
smartTransaction.status === SmartTransactionStatuses.UNKNOWN)
) {
smartTransactionTxHashesForUpdate[smartTransaction.txHash.toLowerCase()] = true;
dan437 marked this conversation as resolved.
Show resolved Hide resolved
}
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
transactionControllerState.transactions.forEach((transaction: any) => {
if (!transaction.hash || transaction.status === TransactionStatus.failed) {
dan437 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
const previousStatus = transaction.status;
if (smartTransactionTxHashesForUpdate[transaction.hash.toLowerCase()]) {
transaction.status = TransactionStatus.failed;
transaction.error = {
name: 'SmartTransactionCancelled',
message: `Smart transaction cancelled. Previous status: ${previousStatus}`,
};
}
});

return state;
}
Loading
Loading