Skip to content

Commit 2232933

Browse files
committed
Update tests, remove unused code
Signed-off-by: dan437 <80175477+dan437@users.noreply.github.com>
1 parent b05a549 commit 2232933

3 files changed

Lines changed: 152 additions & 87 deletions

File tree

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
collectCoverage: true,
33
// Ensures that we collect coverage from all source files, not just tested
44
// ones.
5-
collectCoverageFrom: ['./src/**.ts'],
5+
collectCoverageFrom: ['./src/**.ts', '!./src/index.ts'],
66
coverageReporters: ['text', 'html'],
77
coverageThreshold: {
88
global: {

src/SmartTransactionsController.test.ts

Lines changed: 151 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import type {
2424
import {
2525
type TransactionParams,
2626
TransactionStatus,
27-
TransactionType,
2827
} from '@metamask/transaction-controller';
2928
import nock from 'nock';
3029
import * as sinon from 'sinon';
@@ -55,15 +54,21 @@ type RootMessenger = Messenger<MockAnyNamespace, AllActions, AllEvents>;
5554
jest.mock('@metamask/eth-query', () => {
5655
const EthQuery = jest.requireActual('@metamask/eth-query');
5756
return class FakeEthQuery extends EthQuery {
58-
sendAsync = jest.fn(({ method }, callback) => {
57+
sendAsync = jest.fn(({ method, params }, callback) => {
5958
switch (method) {
6059
case 'eth_getBalance': {
6160
callback(null, '0x1000');
6261
break;
6362
}
6463

6564
case 'eth_getTransactionReceipt': {
66-
callback(null, { blockNumber: '123' });
65+
// Return null if txHash is empty/falsy
66+
const txHash = params?.[0];
67+
if (txHash) {
68+
callback(null, { blockNumber: '123' });
69+
} else {
70+
callback(null, null);
71+
}
6772
break;
6873
}
6974

@@ -307,37 +312,6 @@ const testHistory = [
307312
},
308313
];
309314

310-
const createTransactionMeta = (
311-
status: TransactionStatus = TransactionStatus.signed,
312-
) => {
313-
return {
314-
hash: txHash,
315-
status,
316-
id: '1',
317-
txParams: {
318-
from: addressFrom,
319-
to: '0x1678a085c290ebd122dc42cba69373b5953b831d',
320-
gasPrice: '0x77359400',
321-
gas: '0x7b0d',
322-
nonce: '0x4b',
323-
},
324-
type: TransactionType.simpleSend,
325-
chainId: ChainId.mainnet,
326-
time: 1624408066355,
327-
defaultGasEstimates: {
328-
gas: '0x7b0d',
329-
gasPrice: '0x77359400',
330-
},
331-
error: {
332-
name: 'Error',
333-
message: 'Details of the error',
334-
},
335-
securityProviderResponse: {
336-
flagAsDangerous: 0,
337-
},
338-
};
339-
};
340-
341315
const ethereumChainIdDec = parseInt(ChainId.mainnet, 16);
342316
const sepoliaChainIdDec = parseInt(ChainId.sepolia, 16);
343317

@@ -459,6 +433,7 @@ describe('SmartTransactionsController', () => {
459433

460434
controller.timeoutHandle = setTimeout(() => ({}));
461435

436+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
462437
controller.poll(1000);
463438

464439
expect(updateSmartTransactionsSpy).toHaveBeenCalled();
@@ -1395,6 +1370,147 @@ describe('SmartTransactionsController', () => {
13951370
},
13961371
);
13971372
});
1373+
1374+
it('confirms a smart transaction with SUCCESS status', async () => {
1375+
const { smartTransactionsState } =
1376+
getDefaultSmartTransactionsControllerState();
1377+
const pendingStx = {
1378+
...createStateAfterPending()[0],
1379+
history: testHistory,
1380+
};
1381+
await withController(
1382+
{
1383+
options: {
1384+
state: {
1385+
smartTransactionsState: {
1386+
...smartTransactionsState,
1387+
smartTransactions: {
1388+
[ChainId.mainnet]: [pendingStx] as SmartTransaction[],
1389+
},
1390+
},
1391+
},
1392+
},
1393+
},
1394+
async ({ controller }) => {
1395+
const updateTransaction = {
1396+
...pendingStx,
1397+
statusMetadata: {
1398+
...pendingStx.statusMetadata,
1399+
minedHash: txHash,
1400+
},
1401+
status: SmartTransactionStatuses.SUCCESS,
1402+
};
1403+
1404+
controller.updateSmartTransaction(
1405+
updateTransaction as SmartTransaction,
1406+
{
1407+
networkClientId: NetworkType.mainnet,
1408+
},
1409+
);
1410+
await flushPromises();
1411+
1412+
expect(
1413+
controller.state.smartTransactionsState.smartTransactions[
1414+
ChainId.mainnet
1415+
][0].confirmed,
1416+
).toBe(true);
1417+
},
1418+
);
1419+
});
1420+
1421+
it('confirms a smart transaction with REVERTED status', async () => {
1422+
const { smartTransactionsState } =
1423+
getDefaultSmartTransactionsControllerState();
1424+
const pendingStx = {
1425+
...createStateAfterPending()[0],
1426+
history: testHistory,
1427+
};
1428+
await withController(
1429+
{
1430+
options: {
1431+
state: {
1432+
smartTransactionsState: {
1433+
...smartTransactionsState,
1434+
smartTransactions: {
1435+
[ChainId.mainnet]: [pendingStx] as SmartTransaction[],
1436+
},
1437+
},
1438+
},
1439+
},
1440+
},
1441+
async ({ controller }) => {
1442+
const updateTransaction = {
1443+
...pendingStx,
1444+
statusMetadata: {
1445+
...pendingStx.statusMetadata,
1446+
minedHash: txHash,
1447+
},
1448+
status: SmartTransactionStatuses.REVERTED,
1449+
};
1450+
1451+
controller.updateSmartTransaction(
1452+
updateTransaction as SmartTransaction,
1453+
{
1454+
networkClientId: NetworkType.mainnet,
1455+
},
1456+
);
1457+
await flushPromises();
1458+
1459+
expect(
1460+
controller.state.smartTransactionsState.smartTransactions[
1461+
ChainId.mainnet
1462+
][0].confirmed,
1463+
).toBe(true);
1464+
},
1465+
);
1466+
});
1467+
1468+
it('does not confirm a smart transaction that does not have a minedHash', async () => {
1469+
const { smartTransactionsState } =
1470+
getDefaultSmartTransactionsControllerState();
1471+
const pendingStx = {
1472+
...createStateAfterPending()[0],
1473+
history: testHistory,
1474+
};
1475+
await withController(
1476+
{
1477+
options: {
1478+
state: {
1479+
smartTransactionsState: {
1480+
...smartTransactionsState,
1481+
smartTransactions: {
1482+
[ChainId.mainnet]: [pendingStx] as SmartTransaction[],
1483+
},
1484+
},
1485+
},
1486+
},
1487+
},
1488+
async ({ controller }) => {
1489+
const updateTransaction = {
1490+
...pendingStx,
1491+
statusMetadata: {
1492+
...pendingStx.statusMetadata,
1493+
minedHash: '',
1494+
},
1495+
status: SmartTransactionStatuses.SUCCESS,
1496+
};
1497+
1498+
controller.updateSmartTransaction(
1499+
updateTransaction as SmartTransaction,
1500+
{
1501+
networkClientId: NetworkType.mainnet,
1502+
},
1503+
);
1504+
await flushPromises();
1505+
1506+
expect(
1507+
controller.state.smartTransactionsState.smartTransactions[
1508+
ChainId.mainnet
1509+
][0].confirmed,
1510+
).toBeUndefined();
1511+
},
1512+
);
1513+
});
13981514
});
13991515

14001516
describe('cancelSmartTransaction', () => {
@@ -2577,6 +2693,7 @@ async function withController<ReturnValue>(
25772693
triggerNetworStateChange,
25782694
});
25792695
} finally {
2696+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
25802697
controller.stop();
25812698
controller.stopAllPolling();
25822699
}

src/utils.ts

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
} from '@metamask/transaction-controller';
99
import { TransactionStatus } from '@metamask/transaction-controller';
1010
import { BigNumber } from 'bignumber.js';
11-
import jsonDiffer from 'fast-json-patch';
1211
import _ from 'lodash';
1312

1413
// Ignoring TypeScript errors here because this import is disallowed for production builds, because
@@ -114,57 +113,6 @@ export const calculateStatus = (stxStatus: SmartTransactionsStatus) => {
114113
return SmartTransactionStatuses.UNKNOWN;
115114
};
116115

117-
/**
118-
Generates an array of history objects sense the previous state.
119-
The object has the keys
120-
op (the operation performed),
121-
path (the key and if a nested object then each key will be separated with a `/`)
122-
value
123-
with the first entry having the note and a timestamp when the change took place
124-
@param previousState - the previous state of the object
125-
@param newState - the update object
126-
@param [note] - a optional note for the state change
127-
@returns
128-
*/
129-
export function generateHistoryEntry(
130-
previousState: any,
131-
newState: any,
132-
note: string,
133-
) {
134-
const entry: any = jsonDiffer.compare(previousState, newState);
135-
// Add a note to the first op, since it breaks if we append it to the entry
136-
if (entry[0]) {
137-
if (note) {
138-
entry[0].note = note;
139-
}
140-
141-
entry[0].timestamp = Date.now();
142-
}
143-
return entry;
144-
}
145-
146-
/**
147-
Recovers previous txMeta state obj
148-
@returns
149-
*/
150-
export function replayHistory(_shortHistory: any) {
151-
const shortHistory = _.cloneDeep(_shortHistory);
152-
return shortHistory.reduce(
153-
(val: any, entry: any) => jsonDiffer.applyPatch(val, entry).newDocument,
154-
);
155-
}
156-
157-
/**
158-
* Snapshot {@code txMeta}
159-
* @param txMeta - the tx metadata object
160-
* @returns a deep clone without history
161-
*/
162-
export function snapshotFromTxMeta(txMeta: any) {
163-
const shallow = { ...txMeta };
164-
delete shallow.history;
165-
return _.cloneDeep(shallow);
166-
}
167-
168116
/**
169117
* Returns processing time for an STX in seconds.
170118
* @param smartTransactionSubmittedtime

0 commit comments

Comments
 (0)