From f20a07f10a260bc5f49a348893bb0b871ce10c3b Mon Sep 17 00:00:00 2001 From: doylet Date: Wed, 19 Jun 2024 16:50:28 +1000 Subject: [PATCH] Add EVM automine and mine functionality --- include/ethyl/provider.hpp | 15 ++++++++++++--- src/provider.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/include/ethyl/provider.hpp b/include/ethyl/provider.hpp index 7f20b24..1a24a4a 100644 --- a/include/ethyl/provider.hpp +++ b/include/ethyl/provider.hpp @@ -59,11 +59,20 @@ struct Provider { std::string callReadFunction(const ReadCallData& callData, std::string_view blockNumber = "latest"); std::string callReadFunction(const ReadCallData& callData, uint64_t blockNumberInt); - uint32_t getNetworkChainId(); + uint32_t getNetworkChainId(); std::string evm_snapshot(); - bool evm_revert(std::string_view snapshotId); - uint64_t evm_increaseTime(std::chrono::seconds seconds); + // Enables or disables, based on the single boolean argument, the automatic + // mining of new blocks with each new transaction submitted to the network. + // You can use hardhat_getAutomine to get the current value. + bool evm_setAutomine(bool enable); + + // Force a block to be mined. Takes no parameters. Mines a block independent + // of whether or not mining is started or stopped. + bool evm_mine(); + + bool evm_revert(std::string_view snapshotId); + uint64_t evm_increaseTime(std::chrono::seconds seconds); std::optional getTransactionByHash(std::string_view transactionHash); std::optional getTransactionReceipt(std::string_view transactionHash); diff --git a/src/provider.cpp b/src/provider.cpp index 0eea59c..371543b 100644 --- a/src/provider.cpp +++ b/src/provider.cpp @@ -163,6 +163,31 @@ std::string Provider::evm_snapshot() { throw std::runtime_error("Unable to create snapshot"); } +bool Provider::evm_setAutomine(bool enable) { + nlohmann::json params = nlohmann::json::array(); + params.push_back(enable); + cpr::Response response = makeJsonRpcRequest("evm_setAutomine", params, connectTimeout); + + if (response.status_code == 200) { + nlohmann::json responseJson = nlohmann::json::parse(response.text); + return !responseJson["result"].is_null(); + } + + throw std::runtime_error("Unable to set evm_setAutomine value"); +} + +bool Provider::evm_mine() { + nlohmann::json params = nlohmann::json::array(); + cpr::Response response = makeJsonRpcRequest("evm_mine", params, connectTimeout); + + if (response.status_code == 200) { + nlohmann::json responseJson = nlohmann::json::parse(response.text); + return !responseJson["result"].is_null(); + } + + throw std::runtime_error("Unable to evm_mine"); +} + bool Provider::evm_revert(std::string_view snapshotId) { nlohmann::json params = nlohmann::json::array(); params.push_back(snapshotId);