nim-ethers/ethers/testing.nim
Eric Mastro 5fe41a76ab PR comments
1. rename helpers to testing and expose externally via `import pkg/ethers/testing`
2. Change detection of revert from `EthersError` to `JsonRpcProviderError`
3, Remove catch of `CatchableError` from revert detection as this would swallow errors. Update tests accordingly.
2022-09-20 13:15:15 +10:00

50 lines
1.3 KiB
Nim

import std/json
import std/strutils
import pkg/ethers
proc revertReason*(e: ref JsonRpcProviderError): string =
try:
let json = parseJson(e.msg)
var msg = json{"message"}.getStr
const revertPrefixes = @[
# hardhat
"Error: VM Exception while processing transaction: reverted with " &
"reason string ",
# ganache
"VM Exception while processing transaction: revert "
]
for prefix in revertPrefixes.items:
msg = msg.replace(prefix)
msg = msg.replace("\'")
return msg
except JsonParsingError:
return ""
template reverts*(body: untyped): untyped =
let asyncproc = proc(): Future[bool] {.async.} =
try:
body
return false
except JsonRpcProviderError:
return true
waitFor asyncproc()
template revertsWith*(reason: string, body: untyped): untyped =
let asyncproc = proc(): Future[bool] {.async.} =
try:
body
return false
except JsonRpcProviderError as e:
return reason == revertReason(e)
waitFor asyncproc()
template doesNotRevert*(body: untyped): untyped =
let asyncproc = proc(): Future[bool] {.async.} =
return not reverts(body)
waitFor asyncproc()
template doesNotRevertWith*(reason: string, body: untyped): untyped =
let asyncproc = proc(): Future[bool] {.async.} =
return not revertsWith(reason, body)
waitFor asyncproc()