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 ""
|
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
proc reverts*[T](call: Future[T]): Future[bool] {.async.} =
|
|
|
|
try:
|
|
|
|
when T is void:
|
|
|
|
await call
|
|
|
|
else:
|
|
|
|
discard await call # TODO test this
|
|
|
|
return false
|
|
|
|
except JsonRpcProviderError:
|
|
|
|
return true # TODO: check that error started with revert prefix
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
proc reverts*[T](call: Future[T], reason: string): Future[bool] {.async.} =
|
|
|
|
try:
|
|
|
|
when T is void:
|
|
|
|
await call
|
|
|
|
else:
|
|
|
|
discard await call # TODO test this
|
|
|
|
return false
|
|
|
|
except JsonRpcProviderError as error:
|
|
|
|
return reason == error.revertReason
|