Skip to content

Commit

Permalink
evm snapshots and setTime
Browse files Browse the repository at this point in the history
  • Loading branch information
darcys22 committed Nov 28, 2023
1 parent b1f79d8 commit 80b287c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/ethyl/provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nlohmann::json> getTransactionByHash(const std::string& transactionHash);
std::optional<nlohmann::json> getTransactionReceipt(const std::string& transactionHash);

Expand Down
45 changes: 45 additions & 0 deletions src/provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 80b287c

Please sign in to comment.