Update retreival of revert reason to send a populated transaction, add working test

This commit is contained in:
Eric 2023-09-22 20:00:21 +10:00
parent affaa2e621
commit 24f1f86dc9
No known key found for this signature in database
5 changed files with 24 additions and 8 deletions

View File

@ -79,6 +79,15 @@ logScope:
template raiseProviderError(message: string) =
raise newException(ProviderError, message)
func toTransaction*(past: PastTransaction): Transaction =
Transaction(
sender: some past.sender,
gasPrice: some past.gasPrice,
data: past.input,
nonce: some past.nonce,
to: past.to
)
method getBlockNumber*(provider: Provider): Future[UInt256] {.base, gcsafe.} =
doAssert false, "not implemented"
@ -144,7 +153,7 @@ method subscribe*(provider: Provider,
method unsubscribe*(subscription: Subscription) {.base, async.} =
doAssert false, "not implemented"
proc replay*(provider: Provider, tx: PastTransaction, blockNumber: UInt256) {.async.} =
proc replay*(provider: Provider, tx: Transaction, blockNumber: UInt256) {.async.} =
# Replay transaction at block. Useful for fetching revert reasons, which will
# be present in the raised error message. The replayed block number should
# include the state of the chain in the block previous to the block in which
@ -160,11 +169,11 @@ method getRevertReason*(
blockNumber: UInt256
): Future[?string] {.base, async.} =
without transaction =? await provider.getTransaction(hash):
without pastTx =? await provider.getTransaction(hash):
return none string
try:
await provider.replay(transaction, blockNumber)
await provider.replay(pastTx.toTransaction, blockNumber)
return none string
except ProviderError as e:
# should contain the revert reason

View File

@ -16,6 +16,11 @@ type JsonSerializationError = object of EthersError
template raiseSerializationError(message: string) =
raise newException(JsonSerializationError, message)
proc expectFields(json: JsonNode, expectedFields: varargs[string]) =
for fieldName in expectedFields:
if not json.hasKey(fieldName):
raiseSerializationError(fmt"'{fieldName}' field not found in ${json}")
func fromJson*(T: type, json: JsonNode, name = ""): T =
fromJson(json, name, result)
@ -97,11 +102,6 @@ func `%`*(status: TransactionStatus): JsonNode =
# PastTransaction
proc expectFields(json: JsonNode, expectedFields: varargs[string]) =
for fieldName in expectedFields:
if not json.hasKey(fieldName):
raiseSerializationError(fmt"'{fieldName}' field not found in ${json}")
func fromJson*(json: JsonNode, name: string, result: var PastTransaction) =
# Deserializes a past transaction, eg eth_getTransactionByHash.
# Spec: https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_gettransactionbyhash

View File

@ -8,6 +8,7 @@ import ./wallet/signing
export keys
export WalletError
export signing
var rng {.threadvar.}: ref HmacDrbgContext

View File

@ -1,8 +1,10 @@
import pkg/eth/keys
import pkg/eth/rlp
import pkg/eth/common/transaction as eth
import pkg/eth/common/eth_hash
import ../basics
import ../transaction as ethers
import ../provider
import ./error
type
@ -62,3 +64,6 @@ func sign(key: PrivateKey, transaction: SignableTransaction): seq[byte] =
func sign*(key: PrivateKey, transaction: Transaction): seq[byte] =
key.sign(transaction.toSignableTransaction())
func toTransactionHash*(bytes: seq[byte]): TransactionHash =
TransactionHash(bytes.keccakHash.data)

View File

@ -7,6 +7,7 @@ import pkg/stint
import pkg/ethers
import pkg/ethers/erc20
import pkg/ethers/testing
import pkg/ethers/wallet
import ./hardhat
import ./helpers
import ./miner