2022-09-15 10:38:25 +00:00
|
|
|
import std/strformat
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/ethers
|
2022-09-20 01:59:39 +00:00
|
|
|
import pkg/ethers/testing
|
2022-09-15 10:38:25 +00:00
|
|
|
import ./hardhat
|
|
|
|
|
2022-09-20 01:59:39 +00:00
|
|
|
suite "Testing helpers":
|
2022-09-15 10:38:25 +00:00
|
|
|
|
|
|
|
let revertReason = "revert reason"
|
2022-09-20 10:18:01 +00:00
|
|
|
let rpcResponse = "Error: VM Exception while processing transaction: " &
|
|
|
|
fmt"reverted with reason string '{revertReason}'"
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
test "checks that call reverts":
|
|
|
|
proc call() {.async.} =
|
2022-09-20 11:24:47 +00:00
|
|
|
raise newException(ProviderError, $rpcResponse)
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
check await call().reverts()
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
test "checks reason for revert":
|
|
|
|
proc call() {.async.} =
|
2022-09-20 11:24:47 +00:00
|
|
|
raise newException(ProviderError, $rpcResponse)
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
check await call().reverts(revertReason)
|
2022-09-15 10:38:25 +00:00
|
|
|
|
|
|
|
test "correctly indicates there was no revert":
|
2022-09-20 07:53:28 +00:00
|
|
|
proc call() {.async.} = discard
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
check not await call().reverts()
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 11:24:47 +00:00
|
|
|
test "reverts only checks ProviderErrors":
|
2022-09-20 07:53:28 +00:00
|
|
|
proc call() {.async.} =
|
2022-09-20 01:59:39 +00:00
|
|
|
raise newException(ContractError, "test")
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
expect ContractError:
|
|
|
|
check await call().reverts()
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 11:24:47 +00:00
|
|
|
test "reverts with reason only checks ProviderErrors":
|
2022-09-20 07:53:28 +00:00
|
|
|
proc call() {.async.} =
|
2022-09-20 01:59:39 +00:00
|
|
|
raise newException(ContractError, "test")
|
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
expect ContractError:
|
|
|
|
check await call().reverts(revertReason)
|
2022-09-20 01:59:39 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
test "reverts with reason is false when there is no revert":
|
|
|
|
proc call() {.async.} = discard
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
check not await call().reverts(revertReason)
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
test "reverts is false when the revert reason doesn't match":
|
|
|
|
proc call() {.async.} =
|
2022-09-20 11:24:47 +00:00
|
|
|
raise newException(ProviderError, "other reason")
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
check not await call().reverts(revertReason)
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
test "revert handles non-standard revert prefix":
|
2022-09-15 10:38:25 +00:00
|
|
|
let nonStdMsg = fmt"Provider VM Exception: reverted with {revertReason}"
|
2022-09-20 07:53:28 +00:00
|
|
|
proc call() {.async.} =
|
2022-09-20 11:24:47 +00:00
|
|
|
raise newException(ProviderError, nonStdMsg)
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
check await call().reverts(nonStdMsg)
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2022-09-20 11:37:53 +00:00
|
|
|
test "works with functions that return a value":
|
|
|
|
proc call(): Future[int] {.async.} = return 42
|
|
|
|
check not await call().reverts()
|
|
|
|
check not await call().reverts("some reason")
|
|
|
|
|
2022-09-15 10:38:25 +00:00
|
|
|
type
|
|
|
|
TestHelpers* = ref object of Contract
|
|
|
|
|
|
|
|
method revertsWith*(self: TestHelpers,
|
|
|
|
revertReason: string) {.base, contract, view.}
|
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
suite "Testing helpers - provider":
|
2022-09-15 10:38:25 +00:00
|
|
|
|
|
|
|
var helpersContract: TestHelpers
|
|
|
|
var provider: JsonRpcProvider
|
|
|
|
var snapshot: JsonNode
|
|
|
|
var accounts: seq[Address]
|
|
|
|
let revertReason = "revert reason"
|
|
|
|
|
|
|
|
setup:
|
|
|
|
provider = JsonRpcProvider.new("ws://127.0.0.1:8545")
|
|
|
|
snapshot = await provider.send("evm_snapshot")
|
|
|
|
accounts = await provider.listAccounts()
|
|
|
|
let deployment = readDeployment()
|
|
|
|
helpersContract = TestHelpers.new(!deployment.address(TestHelpers), provider)
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
discard await provider.send("evm_revert", @[snapshot])
|
|
|
|
|
2022-09-20 07:53:28 +00:00
|
|
|
test "revert works with provider":
|
|
|
|
check await helpersContract.revertsWith(revertReason).reverts(revertReason)
|