2022-09-15 10:38:25 +00:00
|
|
|
import std/strutils
|
2022-09-20 11:45:59 +00:00
|
|
|
import ./provider
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 11:24:47 +00:00
|
|
|
proc revertReason*(e: ref ProviderError): string =
|
2022-09-20 13:36:42 +00:00
|
|
|
var msg = e.msg
|
|
|
|
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
|
2022-09-15 10:38:25 +00:00
|
|
|
|
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:
|
2022-09-20 11:37:53 +00:00
|
|
|
discard await call
|
2022-09-20 07:53:28 +00:00
|
|
|
return false
|
2022-09-20 11:24:47 +00:00
|
|
|
except ProviderError:
|
2022-09-20 10:18:01 +00:00
|
|
|
return true
|
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:
|
2022-09-20 11:37:53 +00:00
|
|
|
discard await call
|
2022-09-20 07:53:28 +00:00
|
|
|
return false
|
2022-09-20 11:24:47 +00:00
|
|
|
except ProviderError as error:
|
2022-09-20 07:53:28 +00:00
|
|
|
return reason == error.revertReason
|