From 9cb033e865bf2b51854ce2d6ee4fe73b8ca4db8d Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 20 Mar 2024 15:58:31 +0100 Subject: [PATCH] keep error data intact when replaying transaction --- ethers/provider.nim | 47 ++++++++++----------------------------------- 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/ethers/provider.nim b/ethers/provider.nim index f4faa35..4b585fe 100644 --- a/ethers/provider.nim +++ b/ethers/provider.nim @@ -202,33 +202,6 @@ proc replay*( trace "replaying transaction", gasLimit = tx.gasLimit, tx = $tx discard await provider.call(tx, BlockTag.init(blockNumber)) -method getRevertReason*( - provider: Provider, - hash: TransactionHash, - blockNumber: UInt256): Future[?string] {.base, async: (raises: [ProviderError]).} = - - without pastTx =? await provider.getTransaction(hash): - return none string - - try: - await provider.replay(pastTx.toTransaction, blockNumber) - return none string - except ProviderError as e: - # should contain the revert reason - return some e.msg - -method getRevertReason*( - provider: Provider, - receipt: TransactionReceipt): Future[?string] {.base, async: (raises: [ProviderError]).} = - - if receipt.status != TransactionStatus.Failure: - return none string - - without blockNumber =? receipt.blockNumber: - return none string - - return await provider.getRevertReason(receipt.transactionHash, blockNumber - 1) - proc ensureSuccess( provider: Provider, receipt: TransactionReceipt) {.async: (raises: [ProviderError]).} = @@ -237,18 +210,18 @@ proc ensureSuccess( ## If no revert reason was obtained # TODO: handle TransactionStatus.Invalid? - if receipt.status == TransactionStatus.Failure: - logScope: - transactionHash = receipt.transactionHash.to0xHex + if receipt.status != TransactionStatus.Failure: + return - trace "transaction failed, replaying transaction to get revert reason" + without blockNumber =? receipt.blockNumber and + pastTx =? await provider.getTransaction(receipt.transactionHash): + raiseProviderError("Transaction reverted with unknown reason") - if revertReason =? await provider.getRevertReason(receipt): - trace "transaction revert reason obtained", revertReason - raiseProviderError(revertReason) - else: - trace "transaction replay completed, no revert reason obtained" - raiseProviderError("Transaction reverted with unknown reason") + try: + await provider.replay(pastTx.toTransaction, blockNumber) + raiseProviderError("Transaction reverted with unknown reason") + except ProviderError as error: + raise error proc confirm*( tx: TransactionResponse,