Skip to content

Commit

Permalink
Ensure the nonce lock is always released (#108)
Browse files Browse the repository at this point in the history
A `try...finally` block has been added to ensure that the nonce is
always released, even when an error is thrown.
  • Loading branch information
Gudahtt authored Nov 15, 2022
1 parent 57515ae commit 7529fc2
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,25 +568,28 @@ export default class SmartTransactionsController extends BaseController<
console.error('ethers error', e);
}
const nonceLock = await this.getNonceLock(txParams?.from);
const nonce = hexlify(nonceLock.nextNonce);
if (txParams && !txParams?.nonce) {
txParams.nonce = nonce;
}
const { nonceDetails } = nonceLock;
try {
const nonce = hexlify(nonceLock.nextNonce);
if (txParams && !txParams?.nonce) {
txParams.nonce = nonce;
}
const { nonceDetails } = nonceLock;

this.updateSmartTransaction({
chainId,
nonceDetails,
metamaskNetworkId,
preTxBalance,
status: SmartTransactionStatuses.PENDING,
time,
txParams,
uuid: data.uuid,
cancellable: true,
});
this.updateSmartTransaction({
chainId,
nonceDetails,
metamaskNetworkId,
preTxBalance,
status: SmartTransactionStatuses.PENDING,
time,
txParams,
uuid: data.uuid,
cancellable: true,
});
} finally {
nonceLock.releaseLock();
}

nonceLock.releaseLock();
return data;
}

Expand Down

0 comments on commit 7529fc2

Please sign in to comment.