Skip to content

Commit 6238b6a

Browse files
committed
wrap try/finally around populateTransaction logic to ensure the lock is always released in the case of an error
1 parent 2428b75 commit 6238b6a

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

ethers/signer.nim

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,35 @@ method populateTransaction*(signer: Signer,
103103

104104
var populated = transaction
105105

106-
if transaction.sender.isNone:
107-
populated.sender = some(await signer.getAddress())
108-
if transaction.chainId.isNone:
109-
populated.chainId = some(await signer.getChainId())
110-
if transaction.gasPrice.isNone and (transaction.maxFee.isNone or transaction.maxPriorityFee.isNone):
111-
populated.gasPrice = some(await signer.getGasPrice())
112-
113-
if transaction.nonce.isNone and transaction.gasLimit.isNone:
114-
# when both nonce and gasLimit are not populated, we must ensure getNonce is
115-
# followed by an estimateGas so we can determine if there was an error. If
116-
# there is an error, the nonce must be deprecated to prevent nonce gaps and
117-
# stuck transactions
118-
try:
119-
populated.nonce = some(await signer.getNonce())
120-
populated.gasLimit = some(await signer.estimateGas(populated))
121-
except ProviderError, EstimateGasError:
122-
let e = getCurrentException()
123-
signer.decreaseNonce()
124-
raise e
125-
finally:
126-
signer.populateLock.release()
127-
128-
else:
129-
if transaction.nonce.isNone:
130-
populated.nonce = some(await signer.getNonce())
131-
if transaction.gasLimit.isNone:
132-
populated.gasLimit = some(await signer.estimateGas(populated))
106+
try:
107+
if transaction.sender.isNone:
108+
populated.sender = some(await signer.getAddress())
109+
if transaction.chainId.isNone:
110+
populated.chainId = some(await signer.getChainId())
111+
if transaction.gasPrice.isNone and (transaction.maxFee.isNone or transaction.maxPriorityFee.isNone):
112+
populated.gasPrice = some(await signer.getGasPrice())
113+
114+
if transaction.nonce.isNone and transaction.gasLimit.isNone:
115+
# when both nonce and gasLimit are not populated, we must ensure getNonce is
116+
# followed by an estimateGas so we can determine if there was an error. If
117+
# there is an error, the nonce must be deprecated to prevent nonce gaps and
118+
# stuck transactions
119+
try:
120+
populated.nonce = some(await signer.getNonce())
121+
populated.gasLimit = some(await signer.estimateGas(populated))
122+
except ProviderError, EstimateGasError:
123+
let e = getCurrentException()
124+
signer.decreaseNonce()
125+
raise e
126+
127+
else:
128+
if transaction.nonce.isNone:
129+
populated.nonce = some(await signer.getNonce())
130+
if transaction.gasLimit.isNone:
131+
populated.gasLimit = some(await signer.estimateGas(populated))
132+
133+
finally:
134+
signer.populateLock.release()
133135

134136
return populated
135137

0 commit comments

Comments
 (0)