Remove dependency on json-rpc provider for `reverts`

This commit is contained in:
Mark Spanbroek 2022-09-20 13:24:47 +02:00 committed by markspanbroek
parent f545169331
commit c5a40e5f9d
4 changed files with 11 additions and 11 deletions

View File

@ -10,6 +10,7 @@ push: {.upraises: [].}
type type
Provider* = ref object of RootObj Provider* = ref object of RootObj
ProviderError* = object of EthersError
Subscription* = ref object of RootObj Subscription* = ref object of RootObj
Filter* = object Filter* = object
address*: Address address*: Address

View File

@ -25,7 +25,7 @@ type
JsonRpcSigner* = ref object of Signer JsonRpcSigner* = ref object of Signer
provider: JsonRpcProvider provider: JsonRpcProvider
address: ?Address address: ?Address
JsonRpcProviderError* = object of EthersError JsonRpcProviderError* = object of ProviderError
SubscriptionHandler = proc(id, arguments: JsonNode): Future[void] {.gcsafe, upraises:[].} SubscriptionHandler = proc(id, arguments: JsonNode): Future[void] {.gcsafe, upraises:[].}
proc raiseProviderError(message: string) {.upraises: [JsonRpcProviderError].} = proc raiseProviderError(message: string) {.upraises: [JsonRpcProviderError].} =

View File

@ -1,8 +1,7 @@
import std/json
import std/strutils import std/strutils
import pkg/ethers import pkg/ethers
proc revertReason*(e: ref JsonRpcProviderError): string = proc revertReason*(e: ref ProviderError): string =
try: try:
var msg = e.msg var msg = e.msg
const revertPrefixes = @[ const revertPrefixes = @[
@ -26,7 +25,7 @@ proc reverts*[T](call: Future[T]): Future[bool] {.async.} =
else: else:
discard await call # TODO test this discard await call # TODO test this
return false return false
except JsonRpcProviderError: except ProviderError:
return true return true
proc reverts*[T](call: Future[T], reason: string): Future[bool] {.async.} = proc reverts*[T](call: Future[T], reason: string): Future[bool] {.async.} =
@ -36,5 +35,5 @@ proc reverts*[T](call: Future[T], reason: string): Future[bool] {.async.} =
else: else:
discard await call # TODO test this discard await call # TODO test this
return false return false
except JsonRpcProviderError as error: except ProviderError as error:
return reason == error.revertReason return reason == error.revertReason

View File

@ -13,13 +13,13 @@ suite "Testing helpers":
test "checks that call reverts": test "checks that call reverts":
proc call() {.async.} = proc call() {.async.} =
raise newException(JsonRpcProviderError, $rpcResponse) raise newException(ProviderError, $rpcResponse)
check await call().reverts() check await call().reverts()
test "checks reason for revert": test "checks reason for revert":
proc call() {.async.} = proc call() {.async.} =
raise newException(JsonRpcProviderError, $rpcResponse) raise newException(ProviderError, $rpcResponse)
check await call().reverts(revertReason) check await call().reverts(revertReason)
@ -28,14 +28,14 @@ suite "Testing helpers":
check not await call().reverts() check not await call().reverts()
test "reverts only checks JsonRpcProviderErrors": test "reverts only checks ProviderErrors":
proc call() {.async.} = proc call() {.async.} =
raise newException(ContractError, "test") raise newException(ContractError, "test")
expect ContractError: expect ContractError:
check await call().reverts() check await call().reverts()
test "reverts with reason only checks JsonRpcProviderErrors": test "reverts with reason only checks ProviderErrors":
proc call() {.async.} = proc call() {.async.} =
raise newException(ContractError, "test") raise newException(ContractError, "test")
@ -49,14 +49,14 @@ suite "Testing helpers":
test "reverts is false when the revert reason doesn't match": test "reverts is false when the revert reason doesn't match":
proc call() {.async.} = proc call() {.async.} =
raise newException(JsonRpcProviderError, "other reason") raise newException(ProviderError, "other reason")
check not await call().reverts(revertReason) check not await call().reverts(revertReason)
test "revert handles non-standard revert prefix": test "revert handles non-standard revert prefix":
let nonStdMsg = fmt"Provider VM Exception: reverted with {revertReason}" let nonStdMsg = fmt"Provider VM Exception: reverted with {revertReason}"
proc call() {.async.} = proc call() {.async.} =
raise newException(JsonRpcProviderError, nonStdMsg) raise newException(ProviderError, nonStdMsg)
check await call().reverts(nonStdMsg) check await call().reverts(nonStdMsg)