Skip to content

Commit c0492f0

Browse files
authored
Update the "doesTransactionNeedConfirmation" fn, add additional tests (#307)
1 parent 075e1a5 commit c0492f0

File tree

2 files changed

+106
-12
lines changed

2 files changed

+106
-12
lines changed

src/SmartTransactionsController.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,106 @@ describe('SmartTransactionsController', () => {
925925
]);
926926
});
927927

928+
it('confirms a smart transaction that was not found in the list of regular transactions', async () => {
929+
const { smartTransactionsState } = smartTransactionsController.state;
930+
const pendingStx = {
931+
...createStateAfterPending()[0],
932+
history: testHistory,
933+
};
934+
935+
jest
936+
.spyOn(smartTransactionsController, 'getRegularTransactions')
937+
.mockImplementation(() => {
938+
return [];
939+
});
940+
smartTransactionsController.update({
941+
smartTransactionsState: {
942+
...smartTransactionsState,
943+
smartTransactions: {
944+
[CHAIN_IDS.ETHEREUM]: [pendingStx] as SmartTransaction[],
945+
},
946+
},
947+
});
948+
const updateTransaction = {
949+
...pendingStx,
950+
statusMetadata: {
951+
...pendingStx.statusMetadata,
952+
minedHash: txHash,
953+
},
954+
status: SmartTransactionStatuses.SUCCESS,
955+
};
956+
957+
smartTransactionsController.updateSmartTransaction(
958+
updateTransaction as SmartTransaction,
959+
{
960+
networkClientId: 'mainnet',
961+
},
962+
);
963+
await flushPromises();
964+
expect(
965+
smartTransactionsController.confirmExternalTransaction,
966+
).toHaveBeenCalledTimes(1);
967+
expect(
968+
smartTransactionsController.state.smartTransactionsState
969+
.smartTransactions[CHAIN_IDS.ETHEREUM],
970+
).toStrictEqual([
971+
{
972+
...updateTransaction,
973+
confirmed: true,
974+
},
975+
]);
976+
});
977+
978+
it('confirms a smart transaction that does not have a minedHash', async () => {
979+
const { smartTransactionsState } = smartTransactionsController.state;
980+
const pendingStx = {
981+
...createStateAfterPending()[0],
982+
history: testHistory,
983+
};
984+
985+
jest
986+
.spyOn(smartTransactionsController, 'getRegularTransactions')
987+
.mockImplementation(() => {
988+
return [createTransactionMeta(TransactionStatus.confirmed)];
989+
});
990+
smartTransactionsController.update({
991+
smartTransactionsState: {
992+
...smartTransactionsState,
993+
smartTransactions: {
994+
[CHAIN_IDS.ETHEREUM]: [pendingStx] as SmartTransaction[],
995+
},
996+
},
997+
});
998+
const updateTransaction = {
999+
...pendingStx,
1000+
statusMetadata: {
1001+
...pendingStx.statusMetadata,
1002+
minedHash: '',
1003+
},
1004+
status: SmartTransactionStatuses.SUCCESS,
1005+
};
1006+
1007+
smartTransactionsController.updateSmartTransaction(
1008+
updateTransaction as SmartTransaction,
1009+
{
1010+
networkClientId: 'mainnet',
1011+
},
1012+
);
1013+
await flushPromises();
1014+
expect(
1015+
smartTransactionsController.confirmExternalTransaction,
1016+
).toHaveBeenCalledTimes(1);
1017+
expect(
1018+
smartTransactionsController.state.smartTransactionsState
1019+
.smartTransactions[CHAIN_IDS.ETHEREUM],
1020+
).toStrictEqual([
1021+
{
1022+
...updateTransaction,
1023+
confirmed: true,
1024+
},
1025+
]);
1026+
});
1027+
9281028
it('does not call the "confirmExternalTransaction" fn if a tx is already confirmed', async () => {
9291029
const { smartTransactionsState } = smartTransactionsController.state;
9301030
const pendingStx = {

src/SmartTransactionsController.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -457,22 +457,16 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
457457
}
458458
}
459459

460-
#doesTransactionNeedConfirmation(
461-
smartTransaction: SmartTransaction,
462-
): boolean {
463-
const smartTransactionMinedTxHash =
464-
smartTransaction.statusMetadata?.minedHash;
465-
if (!smartTransactionMinedTxHash) {
466-
return false;
460+
#doesTransactionNeedConfirmation(txHash: string | undefined): boolean {
461+
if (!txHash) {
462+
return true;
467463
}
468464
const transactions = this.getRegularTransactions();
469465
const foundTransaction = transactions?.find((tx) => {
470-
return (
471-
tx.hash?.toLowerCase() === smartTransactionMinedTxHash.toLowerCase()
472-
);
466+
return tx.hash?.toLowerCase() === txHash.toLowerCase();
473467
});
474468
if (!foundTransaction) {
475-
return false;
469+
return true;
476470
}
477471
// If a found transaction is either confirmed or submitted, it doesn't need confirmation from the STX controller.
478472
// When it's in the submitted state, the TransactionController checks its status and confirms it,
@@ -547,7 +541,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
547541
}
548542
: originalTxMeta;
549543

550-
if (this.#doesTransactionNeedConfirmation(smartTransaction)) {
544+
if (this.#doesTransactionNeedConfirmation(txHash)) {
551545
this.confirmExternalTransaction(
552546
txMeta,
553547
transactionReceipt,

0 commit comments

Comments
 (0)