From 80b287c9209a7640046dbb86d7b24864f28b702a Mon Sep 17 00:00:00 2001 From: Sean Darcy Date: Tue, 28 Nov 2023 10:56:43 +1100 Subject: [PATCH] evm snapshots and setTime --- include/ethyl/provider.hpp | 5 +++++ src/provider.cpp | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/ethyl/provider.hpp b/include/ethyl/provider.hpp index 94ba9c1..a249ca6 100644 --- a/include/ethyl/provider.hpp +++ b/include/ethyl/provider.hpp @@ -40,7 +40,12 @@ class Provider { uint64_t getTransactionCount(const std::string& address, const std::string& blockTag); std::string callReadFunction(const ReadCallData& callData, uint64_t blockNumberInt); std::string callReadFunction(const ReadCallData& callData, const std::string& blockNumber = "latest"); + uint32_t getNetworkChainId(); + std::string evm_snapshot(); + bool evm_revert(const std::string& snapshotId); + uint64_t evm_setTime(const std::string& time); + std::optional getTransactionByHash(const std::string& transactionHash); std::optional getTransactionReceipt(const std::string& transactionHash); diff --git a/src/provider.cpp b/src/provider.cpp index 750c3b5..7daa965 100644 --- a/src/provider.cpp +++ b/src/provider.cpp @@ -99,6 +99,51 @@ uint32_t Provider::getNetworkChainId() { throw std::runtime_error("Unable to get Network ID"); } +std::string Provider::evm_snapshot() { + nlohmann::json params = nlohmann::json::array(); + cpr::Response response = makeJsonRpcRequest("evm_snapshot", params); + + if (response.status_code == 200) { + nlohmann::json responseJson = nlohmann::json::parse(response.text); + if (!responseJson["result"].is_null()) { + return responseJson["result"]; + } + } + + throw std::runtime_error("Unable to create snapshot"); +} + +bool Provider::evm_revert(const std::string& snapshotId) { + nlohmann::json params = nlohmann::json::array(); + params.push_back(snapshotId); + + cpr::Response response = makeJsonRpcRequest("evm_revert", params); + + if (response.status_code == 200) { + nlohmann::json responseJson = nlohmann::json::parse(response.text); + return !responseJson["result"].is_null(); + } + + throw std::runtime_error("Unable to revert to snapshot"); +} + +uint64_t Provider::evm_setTime(const std::string& time) { + nlohmann::json params = nlohmann::json::array(); + params.push_back(time); + + cpr::Response response = makeJsonRpcRequest("evm_setTime", params); + + if (response.status_code == 200) { + nlohmann::json responseJson = nlohmann::json::parse(response.text); + if (!responseJson["result"].is_null()) { + std::string secondsHex = responseJson["result"]; + return std::stoull(secondsHex, nullptr, 16); + } + } + + throw std::runtime_error("Unable to set time"); +} + uint64_t Provider::getTransactionCount(const std::string& address, const std::string& blockTag) { nlohmann::json params = nlohmann::json::array(); params.push_back(address);