2022-09-15 10:38:25 +00:00
|
|
|
import std/json
|
|
|
|
import std/strutils
|
|
|
|
import pkg/ethers
|
|
|
|
|
2022-09-20 01:59:39 +00:00
|
|
|
proc revertReason*(e: ref JsonRpcProviderError): string =
|
2022-09-15 10:38:25 +00:00
|
|
|
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
|
2022-09-20 01:59:39 +00:00
|
|
|
except JsonRpcProviderError:
|
2022-09-15 10:38:25 +00:00
|
|
|
return true
|
|
|
|
waitFor asyncproc()
|
|
|
|
|
|
|
|
template revertsWith*(reason: string, body: untyped): untyped =
|
|
|
|
let asyncproc = proc(): Future[bool] {.async.} =
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
return false
|
2022-09-20 01:59:39 +00:00
|
|
|
except JsonRpcProviderError as e:
|
2022-09-15 10:38:25 +00:00
|
|
|
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)
|
2022-09-20 01:59:39 +00:00
|
|
|
waitFor asyncproc()
|