Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows for eth calls to specify what block height is being queried #1

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/ethyl/provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class Provider {
void disconnectFromNetwork();

uint64_t getTransactionCount(const std::string& address, const std::string& blockTag);
std::string callReadFunction(const ReadCallData& callData);
std::string callReadFunction(const ReadCallData& callData, uint64_t blockNumberInt);
std::string callReadFunction(const ReadCallData& callData, const std::string& blockNumber = "latest");
uint32_t getNetworkChainId();
std::optional<nlohmann::json> getTransactionByHash(const std::string& transactionHash);
std::optional<nlohmann::json> getTransactionReceipt(const std::string& transactionHash);
Expand Down
18 changes: 10 additions & 8 deletions src/provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,29 @@ cpr::Response Provider::makeJsonRpcRequest(const std::string& method, const nloh
return session.Post();
}

std::string Provider::callReadFunction(const ReadCallData& callData) {
std::string Provider::callReadFunction(const ReadCallData& callData, uint64_t blockNumberInt) {
std::stringstream stream;
stream << "0x" << std::hex << blockNumberInt; // Convert uint64_t to hex string
std::string blockNumberHex = stream.str();

return callReadFunction(callData, blockNumberHex); // Call the original function
}

std::string Provider::callReadFunction(const ReadCallData& callData, const std::string& blockNumber) {
// Prepare the params for the eth_call request
nlohmann::json params = nlohmann::json::array();
params[0]["to"] = callData.contractAddress;
params[0]["data"] = callData.data;
params[1] = "latest"; // or the block number you want to query against

// Make the request
params[1] = blockNumber; // use the provided block number or default to "latest"
cpr::Response response = makeJsonRpcRequest("eth_call", params);

if (response.status_code == 200) {
// Parse the response
nlohmann::json responseJson = nlohmann::json::parse(response.text);

// Check if the result field is present and not null, if it exists then it contains the return value
if (!responseJson["result"].is_null()) {
return responseJson["result"];
}
}

// If we couldn't get the result, throw an exception
throw std::runtime_error("Unable to get the result of the function call");
}

Expand Down
Loading