Skip to content

Commit

Permalink
new function to get a deployed contract address (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
darcys22 authored Nov 21, 2023
1 parent 6a269a2 commit 3c6904c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/ethyl/provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Provider {
bool transactionSuccessful(const std::string& txHash, int64_t timeout = 320000);
uint64_t gasUsed(const std::string& txHash, int64_t timeout = 320000);
mpz_class getBalance(const std::string& address);
std::string getContractDeployedInLatestBlock();

FeeData getFeeData();

Expand Down
19 changes: 19 additions & 0 deletions src/provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,25 @@ mpz_class Provider::getBalance(const std::string& address) {
}
}

std::string Provider::getContractDeployedInLatestBlock() {
nlohmann::json params = nlohmann::json::array();
params.push_back("latest");
params.push_back(true);
auto blockResponse = makeJsonRpcRequest("eth_getBlockByNumber", params);
if (blockResponse.status_code != 200)
throw std::runtime_error("Failed to get the latest block");
nlohmann::json blockJson = nlohmann::json::parse(blockResponse.text);

for (const auto& tx : blockJson["result"]["transactions"]) {
std::optional<nlohmann::json> transactionReceipt = getTransactionReceipt(tx["hash"].get<std::string>());
if (transactionReceipt.has_value())
return transactionReceipt->at("contractAddress").get<std::string>();
}

throw std::runtime_error("No contracts deployed in latest block");
}


FeeData Provider::getFeeData() {
// Get latest block
nlohmann::json params = nlohmann::json::array();
Expand Down

0 comments on commit 3c6904c

Please sign in to comment.