2022-09-15 20:38:25 +10:00
|
|
|
import std/strutils
|
2022-09-20 13:45:59 +02:00
|
|
|
import ./provider
|
2023-09-28 16:52:45 +10:00
|
|
|
import ./signer
|
2022-09-15 20:38:25 +10:00
|
|
|
|
2023-09-28 16:52:45 +10:00
|
|
|
proc revertReason*(e: ref EthersError): string =
|
2022-09-20 15:36:42 +02: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 20:38:25 +10:00
|
|
|
|
2022-09-20 09:53:28 +02:00
|
|
|
proc reverts*[T](call: Future[T]): Future[bool] {.async.} =
|
|
|
|
|
try:
|
|
|
|
|
when T is void:
|
|
|
|
|
await call
|
|
|
|
|
else:
|
2022-09-20 13:37:53 +02:00
|
|
|
discard await call
|
2022-09-20 09:53:28 +02:00
|
|
|
return false
|
2023-09-28 16:52:45 +10:00
|
|
|
except ProviderError, SignerError:
|
2022-09-20 12:18:01 +02:00
|
|
|
return true
|
2022-09-15 20:38:25 +10:00
|
|
|
|
2022-09-20 09:53:28 +02:00
|
|
|
proc reverts*[T](call: Future[T], reason: string): Future[bool] {.async.} =
|
|
|
|
|
try:
|
|
|
|
|
when T is void:
|
|
|
|
|
await call
|
|
|
|
|
else:
|
2022-09-20 13:37:53 +02:00
|
|
|
discard await call
|
2022-09-20 09:53:28 +02:00
|
|
|
return false
|
2023-09-28 16:52:45 +10:00
|
|
|
except EthersError as error:
|
|
|
|
|
var passed = reason == error.revertReason
|
|
|
|
|
if not passed and
|
|
|
|
|
not error.parent.isNil and
|
|
|
|
|
error.parent of (ref EthersError):
|
|
|
|
|
let revertReason = (ref EthersError)(error.parent).revertReason
|
|
|
|
|
passed = reason == revertReason
|
|
|
|
|
return passed
|