2023-05-11 07:56:17 +02:00
|
|
|
import { ApprovalType } from '@metamask/controller-utils';
|
2022-09-14 16:55:31 +02:00
|
|
|
import { CHAIN_IDS } from '../../shared/constants/network';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { TransactionStatus } from '../../shared/constants/transaction';
|
2020-01-30 20:34:45 +01:00
|
|
|
import {
|
|
|
|
unapprovedMessagesSelector,
|
|
|
|
transactionsSelector,
|
|
|
|
nonceSortedTransactionsSelector,
|
|
|
|
nonceSortedPendingTransactionsSelector,
|
|
|
|
nonceSortedCompletedTransactionsSelector,
|
|
|
|
submittedPendingTransactionsSelector,
|
2023-05-11 07:56:17 +02:00
|
|
|
hasTransactionPendingApprovals,
|
2021-03-16 22:00:08 +01:00
|
|
|
} from './transactions';
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Transaction Selectors', () => {
|
|
|
|
describe('unapprovedMessagesSelector', () => {
|
|
|
|
it('returns eth sign msg from unapprovedMsgs', () => {
|
2020-01-30 20:34:45 +01:00
|
|
|
const msg = {
|
|
|
|
id: 1,
|
|
|
|
msgParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
data: '0xData',
|
|
|
|
origin: 'origin',
|
|
|
|
},
|
|
|
|
time: 1,
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.unapproved,
|
2020-01-30 20:34:45 +01:00
|
|
|
type: 'eth_sign',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const state = {
|
|
|
|
metamask: {
|
|
|
|
unapprovedMsgs: {
|
|
|
|
1: msg,
|
|
|
|
},
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2022-09-29 05:26:01 +02:00
|
|
|
chainId: '0x5',
|
2021-03-01 16:15:42 +01:00
|
|
|
},
|
2020-01-30 20:34:45 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const msgSelector = unapprovedMessagesSelector(state);
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(Array.isArray(msgSelector)).toStrictEqual(true);
|
|
|
|
expect(msgSelector).toStrictEqual([msg]);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('returns personal sign from unapprovedPersonalMsgsSelector', () => {
|
2020-01-30 20:34:45 +01:00
|
|
|
const msg = {
|
|
|
|
id: 1,
|
|
|
|
msgParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
data: '0xData',
|
|
|
|
origin: 'origin',
|
|
|
|
},
|
|
|
|
time: 1,
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.unapproved,
|
2020-01-30 20:34:45 +01:00
|
|
|
type: 'personal_sign',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const state = {
|
|
|
|
metamask: {
|
|
|
|
unapprovedPersonalMsgs: {
|
|
|
|
1: msg,
|
|
|
|
},
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2022-09-29 05:26:01 +02:00
|
|
|
chainId: '0x5',
|
2021-03-01 16:15:42 +01:00
|
|
|
},
|
2020-01-30 20:34:45 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const msgSelector = unapprovedMessagesSelector(state);
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(Array.isArray(msgSelector)).toStrictEqual(true);
|
|
|
|
expect(msgSelector).toStrictEqual([msg]);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('returns typed message from unapprovedTypedMessagesSelector', () => {
|
2020-01-30 20:34:45 +01:00
|
|
|
const msg = {
|
|
|
|
id: 1,
|
|
|
|
msgParams: {
|
|
|
|
data: '0xData',
|
|
|
|
from: '0xAddress',
|
|
|
|
version: 'V3',
|
|
|
|
origin: 'origin',
|
|
|
|
},
|
|
|
|
time: 1,
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.unapproved,
|
2020-01-30 20:34:45 +01:00
|
|
|
type: 'eth_signTypedData',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const state = {
|
|
|
|
metamask: {
|
|
|
|
unapprovedTypedMessages: {
|
|
|
|
1: msg,
|
|
|
|
},
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2022-09-29 05:26:01 +02:00
|
|
|
chainId: '0x5',
|
2021-03-01 16:15:42 +01:00
|
|
|
},
|
2020-01-30 20:34:45 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const msgSelector = unapprovedMessagesSelector(state);
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(Array.isArray(msgSelector)).toStrictEqual(true);
|
|
|
|
expect(msgSelector).toStrictEqual([msg]);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('transactionsSelector', () => {
|
|
|
|
it('selects the currentNetworkTxList', () => {
|
2020-01-30 20:34:45 +01:00
|
|
|
const state = {
|
|
|
|
metamask: {
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2020-02-26 18:34:46 +01:00
|
|
|
nickname: 'mainnet',
|
2022-09-14 16:55:31 +02:00
|
|
|
chainId: CHAIN_IDS.MAINNET,
|
2020-02-26 18:34:46 +01:00
|
|
|
},
|
2020-01-30 20:34:45 +01:00
|
|
|
featureFlags: {
|
|
|
|
showIncomingTransactions: false,
|
|
|
|
},
|
2020-03-06 22:34:56 +01:00
|
|
|
selectedAddress: '0xAddress',
|
|
|
|
currentNetworkTxList: [
|
2020-01-30 20:34:45 +01:00
|
|
|
{
|
|
|
|
id: 0,
|
|
|
|
time: 0,
|
|
|
|
txParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
to: '0xRecipient',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
time: 1,
|
|
|
|
txParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
to: '0xRecipient',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const orderedTxList = state.metamask.currentNetworkTxList.sort(
|
|
|
|
(a, b) => b.time - a.time,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const selectedTx = transactionsSelector(state);
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(Array.isArray(selectedTx)).toStrictEqual(true);
|
|
|
|
expect(selectedTx).toStrictEqual(orderedTxList);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('nonceSortedTransactionsSelector', () => {
|
|
|
|
it('returns transaction group nonce sorted tx from from selectedTxList wit', () => {
|
2020-01-30 20:34:45 +01:00
|
|
|
const tx1 = {
|
|
|
|
id: 0,
|
|
|
|
time: 0,
|
|
|
|
txParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
to: '0xRecipient',
|
|
|
|
nonce: '0x0',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const tx2 = {
|
|
|
|
id: 1,
|
|
|
|
time: 1,
|
|
|
|
txParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
to: '0xRecipient',
|
|
|
|
nonce: '0x1',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const state = {
|
|
|
|
metamask: {
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2020-02-26 18:34:46 +01:00
|
|
|
nickname: 'mainnet',
|
2022-09-14 16:55:31 +02:00
|
|
|
chainId: CHAIN_IDS.MAINNET,
|
2020-02-26 18:34:46 +01:00
|
|
|
},
|
2020-03-06 22:34:56 +01:00
|
|
|
selectedAddress: '0xAddress',
|
2020-01-30 20:34:45 +01:00
|
|
|
featureFlags: {
|
|
|
|
showIncomingTransactions: false,
|
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
currentNetworkTxList: [tx1, tx2],
|
2020-01-30 20:34:45 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const expectedResult = [
|
|
|
|
{
|
|
|
|
nonce: '0x0',
|
2020-08-19 18:27:05 +02:00
|
|
|
transactions: [tx1],
|
2020-01-30 20:34:45 +01:00
|
|
|
initialTransaction: tx1,
|
|
|
|
primaryTransaction: tx1,
|
|
|
|
hasRetried: false,
|
|
|
|
hasCancelled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nonce: '0x1',
|
2020-08-19 18:27:05 +02:00
|
|
|
transactions: [tx2],
|
2020-01-30 20:34:45 +01:00
|
|
|
initialTransaction: tx2,
|
|
|
|
primaryTransaction: tx2,
|
|
|
|
hasRetried: false,
|
|
|
|
hasCancelled: false,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(nonceSortedTransactionsSelector(state)).toStrictEqual(
|
2020-12-03 16:46:22 +01:00
|
|
|
expectedResult,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Sorting Transactions Selectors', () => {
|
2020-01-30 20:34:45 +01:00
|
|
|
const submittedTx = {
|
|
|
|
id: 0,
|
|
|
|
time: 0,
|
|
|
|
txParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
to: '0xRecipient',
|
|
|
|
nonce: '0x0',
|
|
|
|
},
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.submitted,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const unapprovedTx = {
|
|
|
|
id: 1,
|
|
|
|
time: 1,
|
|
|
|
txParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
to: '0xRecipient',
|
|
|
|
nonce: '0x1',
|
|
|
|
},
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.unapproved,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const approvedTx = {
|
|
|
|
id: 2,
|
|
|
|
time: 2,
|
|
|
|
txParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
to: '0xRecipient',
|
|
|
|
nonce: '0x2',
|
|
|
|
},
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.approved,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const confirmedTx = {
|
|
|
|
id: 3,
|
|
|
|
time: 3,
|
|
|
|
txParams: {
|
|
|
|
from: '0xAddress',
|
|
|
|
to: '0xRecipient',
|
|
|
|
nonce: '0x3',
|
|
|
|
},
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.confirmed,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const state = {
|
|
|
|
metamask: {
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2020-02-26 18:34:46 +01:00
|
|
|
nickname: 'mainnet',
|
2022-09-14 16:55:31 +02:00
|
|
|
chainId: CHAIN_IDS.MAINNET,
|
2020-02-26 18:34:46 +01:00
|
|
|
},
|
2020-03-06 22:34:56 +01:00
|
|
|
selectedAddress: '0xAddress',
|
2020-01-30 20:34:45 +01:00
|
|
|
featureFlags: {
|
|
|
|
showIncomingTransactions: false,
|
|
|
|
},
|
2020-03-06 22:34:56 +01:00
|
|
|
currentNetworkTxList: [
|
2020-01-30 20:34:45 +01:00
|
|
|
submittedTx,
|
|
|
|
unapprovedTx,
|
|
|
|
approvedTx,
|
|
|
|
confirmedTx,
|
|
|
|
],
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('nonceSortedPendingTransactionsSelector', () => {
|
2020-01-30 20:34:45 +01:00
|
|
|
const expectedResult = [
|
|
|
|
{
|
|
|
|
nonce: submittedTx.txParams.nonce,
|
2020-08-19 18:27:05 +02:00
|
|
|
transactions: [submittedTx],
|
2020-01-30 20:34:45 +01:00
|
|
|
initialTransaction: submittedTx,
|
|
|
|
primaryTransaction: submittedTx,
|
|
|
|
hasRetried: false,
|
|
|
|
hasCancelled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nonce: unapprovedTx.txParams.nonce,
|
2020-08-19 18:27:05 +02:00
|
|
|
transactions: [unapprovedTx],
|
2020-01-30 20:34:45 +01:00
|
|
|
initialTransaction: unapprovedTx,
|
|
|
|
primaryTransaction: unapprovedTx,
|
|
|
|
hasRetried: false,
|
|
|
|
hasCancelled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nonce: approvedTx.txParams.nonce,
|
2020-08-19 18:27:05 +02:00
|
|
|
transactions: [approvedTx],
|
2020-01-30 20:34:45 +01:00
|
|
|
initialTransaction: approvedTx,
|
|
|
|
primaryTransaction: approvedTx,
|
|
|
|
hasRetried: false,
|
|
|
|
hasCancelled: false,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(nonceSortedPendingTransactionsSelector(state)).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
expectedResult,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('nonceSortedCompletedTransactionsSelector', () => {
|
2020-01-30 20:34:45 +01:00
|
|
|
const expectedResult = [
|
|
|
|
{
|
|
|
|
nonce: confirmedTx.txParams.nonce,
|
2020-08-19 18:27:05 +02:00
|
|
|
transactions: [confirmedTx],
|
2020-01-30 20:34:45 +01:00
|
|
|
initialTransaction: confirmedTx,
|
|
|
|
primaryTransaction: confirmedTx,
|
|
|
|
hasRetried: false,
|
|
|
|
hasCancelled: false,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(nonceSortedCompletedTransactionsSelector(state)).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
expectedResult,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('submittedPendingTransactionsSelector', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const expectedResult = [submittedTx];
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(submittedPendingTransactionsSelector(state)).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
expectedResult,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2023-05-11 07:56:17 +02:00
|
|
|
|
|
|
|
describe('hasTransactionPendingApprovals', () => {
|
|
|
|
const mockNetworkId = 'mockNetworkId';
|
|
|
|
const mockedState = {
|
|
|
|
metamask: {
|
|
|
|
providerConfig: {
|
|
|
|
chainId: mockNetworkId,
|
|
|
|
},
|
|
|
|
pendingApprovalCount: 2,
|
|
|
|
pendingApprovals: {
|
|
|
|
1: {
|
|
|
|
id: '1',
|
|
|
|
origin: 'origin',
|
|
|
|
time: Date.now(),
|
|
|
|
type: ApprovalType.WatchAsset,
|
|
|
|
requestData: {},
|
|
|
|
requestState: null,
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
id: '2',
|
|
|
|
origin: 'origin',
|
|
|
|
time: Date.now(),
|
|
|
|
type: ApprovalType.Transaction,
|
|
|
|
requestData: {},
|
|
|
|
requestState: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
unapprovedTxs: {
|
|
|
|
2: {
|
|
|
|
id: '2',
|
|
|
|
chainId: mockNetworkId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
it('should return true if there is a pending transaction on same network', () => {
|
|
|
|
const result = hasTransactionPendingApprovals(mockedState);
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
it('should return false if there is a pending transaction on different network', () => {
|
|
|
|
mockedState.metamask.unapprovedTxs['2'].chainId = 'differentNetworkId';
|
|
|
|
const result = hasTransactionPendingApprovals(mockedState);
|
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
it.each([
|
|
|
|
[ApprovalType.EthDecrypt],
|
|
|
|
[ApprovalType.EthGetEncryptionPublicKey],
|
|
|
|
[ApprovalType.EthSign],
|
|
|
|
[ApprovalType.EthSignTypedData],
|
|
|
|
[ApprovalType.PersonalSign],
|
|
|
|
])(
|
|
|
|
'should return true if there is a pending transaction of %s type',
|
|
|
|
(type) => {
|
|
|
|
const result = hasTransactionPendingApprovals({
|
|
|
|
...mockedState,
|
|
|
|
metamask: {
|
|
|
|
...mockedState.metamask,
|
|
|
|
pendingApprovals: {
|
|
|
|
2: {
|
|
|
|
id: '2',
|
|
|
|
origin: 'origin',
|
|
|
|
time: Date.now(),
|
|
|
|
type,
|
|
|
|
requestData: {},
|
|
|
|
requestState: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(result).toBe(true);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|