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
|
2023-10-24 23:42:25 +00:00
|
|
|
import ./helpers
|
2022-09-15 10:38:25 +00:00
|
|
|
|
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.} =
|
2023-10-24 23:42:25 +00:00
|
|
|
raise newException(EstimateGasError, $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.} =
|
2023-10-24 23:42:25 +00:00
|
|
|
raise newException(EstimateGasError, $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
|
|
|
|
2023-10-24 23:42:25 +00:00
|
|
|
test "reverts only checks ProviderErrors, EstimateGasErrors":
|
|
|
|
proc callProviderError() {.async.} =
|
|
|
|
raise newException(ProviderError, "test")
|
|
|
|
proc callEstimateGasError() {.async.} =
|
|
|
|
raise newException(EstimateGasError, "test")
|
|
|
|
proc callEthersError() {.async.} =
|
|
|
|
raise newException(EthersError, "test")
|
|
|
|
|
|
|
|
check await callProviderError().reverts()
|
|
|
|
check await callEstimateGasError().reverts()
|
|
|
|
expect EthersError:
|
|
|
|
check await callEthersError().reverts()
|
|
|
|
|
|
|
|
test "reverts with reason only checks ProviderErrors, EstimateGasErrors":
|
|
|
|
proc callProviderError() {.async.} =
|
|
|
|
raise newException(ProviderError, revertReason)
|
|
|
|
proc callEstimateGasError() {.async.} =
|
|
|
|
raise newException(EstimateGasError, revertReason)
|
|
|
|
proc callEthersError() {.async.} =
|
|
|
|
raise newException(EthersError, revertReason)
|
|
|
|
|
|
|
|
check await callProviderError().reverts(revertReason)
|
|
|
|
check await callEstimateGasError().reverts(revertReason)
|
|
|
|
expect EthersError:
|
|
|
|
check await callEthersError().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.} =
|
2023-10-24 23:42:25 +00:00
|
|
|
raise newException(EstimateGasError, "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.} =
|
2023-10-24 23:42:25 +00:00
|
|
|
raise newException(EstimateGasError, 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()
|
2023-10-24 23:42:25 +00:00
|
|
|
check not await call().reverts(revertReason)
|
2022-09-15 10:38:25 +00:00
|
|
|
|
|
|
|
|
2023-10-24 23:42:25 +00:00
|
|
|
suite "Testing helpers - contracts":
|
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()
|
2023-10-24 23:42:25 +00:00
|
|
|
helpersContract = TestHelpers.new(provider.getSigner())
|
2022-09-15 10:38:25 +00:00
|
|
|
|
|
|
|
teardown:
|
|
|
|
discard await provider.send("evm_revert", @[snapshot])
|
2023-06-27 14:40:29 +00:00
|
|
|
await provider.close()
|
2022-09-15 10:38:25 +00:00
|
|
|
|
2023-10-25 00:36:00 +00:00
|
|
|
test "revert reason can be retrieved when transaction fails":
|
|
|
|
let txResp = helpersContract.doRevert(
|
|
|
|
revertReason,
|
|
|
|
# override gasLimit to skip estimating gas
|
|
|
|
TransactionOverrides(gasLimit: some 10000000.u256)
|
|
|
|
)
|
|
|
|
check await txResp.confirm(1).reverts(revertReason)
|
|
|
|
|
|
|
|
test "revert reason can be retrieved when estimate gas fails":
|
|
|
|
let txResp = helpersContract.doRevert(revertReason)
|
|
|
|
check await txResp.reverts(revertReason)
|