Skip to content

Commit 6f064e8

Browse files
committed
Add mutex to gasFeeUpdates
1 parent 86e516f commit 6f064e8

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

packages/transaction-controller/src/TransactionController.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ export class TransactionController extends BaseController<
649649

650650
private readonly gasFeeFlows: GasFeeFlow[];
651651

652+
private readonly gasFeeUpdateMutexes: Map<string, Mutex> = new Map();
653+
652654
private readonly getSavedGasFees: (chainId: Hex) => SavedGasFees | undefined;
653655

654656
private readonly getNetworkState: () => NetworkState;
@@ -915,6 +917,7 @@ export class TransactionController extends BaseController<
915917

916918
gasFeePoller.hub.on(
917919
'transaction-updated',
920+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
918921
this.#onGasFeePollerTransactionUpdate.bind(this),
919922
);
920923

@@ -3892,24 +3895,35 @@ export class TransactionController extends BaseController<
38923895
gasFeeEstimatesLoaded?: boolean;
38933896
layer1GasFee?: Hex;
38943897
}) {
3895-
const transaction = this.getTransaction(transactionId) as TransactionMeta;
3896-
const isEIP1559Compatible =
3897-
transaction?.txParams?.type !== TransactionEnvelopeType.legacy &&
3898-
(await this.getEIP1559Compatibility(transaction?.networkClientId));
3898+
if (!this.gasFeeUpdateMutexes.has(transactionId)) {
3899+
this.gasFeeUpdateMutexes.set(transactionId, new Mutex());
3900+
}
3901+
const mutex = this.gasFeeUpdateMutexes.get(transactionId)!;
38993902

3900-
this.#updateTransactionInternal(
3901-
{ transactionId, skipHistory: true },
3902-
(txMeta) => {
3903-
updateTransactionGasFees({
3904-
txMeta,
3905-
gasFeeEstimates,
3906-
gasFeeEstimatesLoaded,
3907-
isEIP1559Compatible,
3908-
isTxParamsGasFeeUpdatesEnabled: this.isTxParamsGasFeeUpdatesEnabled,
3909-
layer1GasFee,
3910-
});
3911-
},
3912-
);
3903+
const release = await mutex.acquire();
3904+
try {
3905+
const transaction = this.getTransaction(transactionId) as TransactionMeta;
3906+
const isEIP1559Compatible =
3907+
transaction?.txParams?.type !== TransactionEnvelopeType.legacy &&
3908+
(await this.getEIP1559Compatibility(transaction?.networkClientId));
3909+
3910+
this.#updateTransactionInternal(
3911+
{ transactionId, skipHistory: true },
3912+
(txMeta) => {
3913+
updateTransactionGasFees({
3914+
txMeta,
3915+
gasFeeEstimates,
3916+
gasFeeEstimatesLoaded,
3917+
isEIP1559Compatible,
3918+
isTxParamsGasFeeUpdatesEnabled: this.isTxParamsGasFeeUpdatesEnabled,
3919+
layer1GasFee,
3920+
});
3921+
},
3922+
);
3923+
} finally {
3924+
release();
3925+
this.gasFeeUpdateMutexes.delete(transactionId);
3926+
}
39133927
}
39143928

39153929
#getSelectedAccount() {

0 commit comments

Comments
 (0)