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
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,9 @@ const BLOCK_TRACKER_MOCK = {
removeListener: jest.fn(),
} as unknown as jest.Mocked<BlockTracker>;

const SELECTED_ACCOUNT_MOCK = {
address: '0x123',
};

const createMessengerMock = (): jest.Mocked<TransactionControllerMessenger> =>
({
call: jest.fn().mockImplementation((action: string) => {
if (action === 'AccountsController:getSelectedAccount') {
return SELECTED_ACCOUNT_MOCK;
}
return {};
}),
call: jest.fn(),
subscribe: jest.fn(),
unsubscribe: jest.fn(),
}) as unknown as jest.Mocked<TransactionControllerMessenger>;
Expand Down Expand Up @@ -385,7 +376,7 @@ describe('TransactionPoller', () => {
});

it.each(['confirmed', 'dropped', 'failed'] as const)(
'triggers interval when transaction with matching chainId and %s status is received',
'triggers interval when transaction with %s status is received',
async (status) => {
const poller = new TransactionPoller({
blockTracker: BLOCK_TRACKER_MOCK,
Expand Down Expand Up @@ -425,38 +416,6 @@ describe('TransactionPoller', () => {
},
);

it('does not trigger interval when transaction with non-matching chainId is received', async () => {
const poller = new TransactionPoller({
blockTracker: BLOCK_TRACKER_MOCK,
messenger: MESSENGER_MOCK,
chainId: CHAIN_ID_MOCK,
});

const listener = jest.fn();
poller.start(listener);

const subscribeCall = MESSENGER_MOCK.subscribe.mock.calls.find(
(call) => call[0] === 'AccountActivityService:transactionUpdated',
);
const eventHandler = subscribeCall?.[1] as (
transaction: Transaction,
) => void;

const transaction: Transaction = {
id: '0xabc',
chain: 'eip155:137', // Different chain
status: 'confirmed',
timestamp: Date.now(),
from: '0x123',
to: '0x456',
};

eventHandler(transaction);
await flushPromises();

expect(listener).not.toHaveBeenCalled();
});

it('does not trigger interval when transaction with pending status is received', async () => {
const poller = new TransactionPoller({
blockTracker: BLOCK_TRACKER_MOCK,
Expand Down Expand Up @@ -489,76 +448,6 @@ describe('TransactionPoller', () => {
expect(listener).not.toHaveBeenCalled();
});

it('does not trigger interval when transaction is from a different address than the selected account', async () => {
const poller = new TransactionPoller({
blockTracker: BLOCK_TRACKER_MOCK,
messenger: MESSENGER_MOCK,
chainId: CHAIN_ID_MOCK,
});

const listener = jest.fn();
poller.start(listener);

const subscribeCall = MESSENGER_MOCK.subscribe.mock.calls.find(
(call) => call[0] === 'AccountActivityService:transactionUpdated',
);
const eventHandler = subscribeCall?.[1] as (
transaction: Transaction,
) => void;

const transaction: Transaction = {
id: '0xabc',
chain: 'eip155:1',
status: 'confirmed',
timestamp: Date.now(),
from: '0x999',
to: '0x456',
};

eventHandler(transaction);
await flushPromises();

expect(listener).not.toHaveBeenCalled();
});

it('triggers interval when transaction from address matches selected account (case-insensitive)', async () => {
const poller = new TransactionPoller({
blockTracker: BLOCK_TRACKER_MOCK,
messenger: MESSENGER_MOCK,
chainId: CHAIN_ID_MOCK,
});

BLOCK_TRACKER_MOCK.getLatestBlock.mockResolvedValue(BLOCK_NUMBER_MOCK);

poller.setPendingTransactions([
createTransactionMetaMock('tx1', '0xabc'),
]);

const listener = jest.fn();
poller.start(listener);

const subscribeCall = MESSENGER_MOCK.subscribe.mock.calls.find(
(call) => call[0] === 'AccountActivityService:transactionUpdated',
);
const eventHandler = subscribeCall?.[1] as (
transaction: Transaction,
) => void;

const transaction: Transaction = {
id: '0xabc',
chain: 'eip155:1',
status: 'confirmed',
timestamp: Date.now(),
from: '0X123',
to: '0x456',
};

eventHandler(transaction);
await flushPromises();

expect(listener).toHaveBeenCalledTimes(1);
});

it('does not trigger interval when poller is stopped', async () => {
const poller = new TransactionPoller({
blockTracker: BLOCK_TRACKER_MOCK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { projectLogger } from '../logger';
import type { TransactionControllerMessenger } from '../TransactionController';
import type { TransactionMeta } from '../types';
import { getAcceleratedPollingParams } from '../utils/feature-flags';
import { caip2ToHex } from '../utils/utils';

const log = createModuleLogger(projectLogger, 'transaction-poller');

Expand Down Expand Up @@ -214,11 +213,6 @@ export class TransactionPoller {
return;
}

const hexChainId = caip2ToHex(transaction.chain);
if (hexChainId !== this.#chainId) {
return;
}

if (
transaction.status !== 'confirmed' &&
transaction.status !== 'dropped' &&
Expand All @@ -227,15 +221,6 @@ export class TransactionPoller {
return;
}

const selectedAccount = this.#messenger.call(
'AccountsController:getSelectedAccount',
);
if (
selectedAccount.address.toLowerCase() !== transaction.from.toLowerCase()
) {
return;
}

const isPendingTransaction = this.#pendingTransactions?.some(
(tx) => tx.hash?.toLowerCase() === transaction.id.toLowerCase(),
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we are checking for pending transactions, other check are redundant.

Expand Down
Loading