From adc6743ef0e2379d3d244f7dd2bec9470ede13ff Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Mon, 22 Aug 2022 10:43:53 +0300 Subject: [PATCH] 1) get_nonce 2) get_gas_price 3) get_gas_limit --- .../sidechain_net_handler_ethereum.hpp | 9 +++- .../sidechain_net_handler_ethereum.cpp | 44 ++++++++++++++++--- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp index af9812d0..d96d7bd9 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp @@ -19,12 +19,17 @@ public: std::string eth_get_block_by_number(std::string block_number, bool full_block); std::string eth_get_logs(std::string wallet_contract_address); std::string net_version(); + std::string eth_get_transaction_count(const std::string& params); + std::string eth_gas_price(); std::string get_chain_id(); std::string get_network_id(); + std::string get_nonce(const std::string& address); + std::string get_gas_price(); + std::string get_gas_limit(); - std::string send_transaction(const std::string& params); - std::string get_transaction_receipt(const std::string& params); + std::string eth_send_transaction(const std::string& params); + std::string eth_get_transaction_receipt(const std::string& params); }; class sidechain_net_handler_ethereum : public sidechain_net_handler { diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp index c01ab57d..36dd2ad3 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp @@ -44,6 +44,14 @@ std::string ethereum_rpc_client::net_version() { return send_post_request("net_version", "", debug_rpc_calls); } +std::string ethereum_rpc_client::eth_get_transaction_count(const std::string& params) { + return send_post_request("eth_getTransactionCount", params, debug_rpc_calls); +} + +std::string ethereum_rpc_client::eth_gas_price() { + return send_post_request("eth_gasPrice", "", debug_rpc_calls); +} + std::string ethereum_rpc_client::get_chain_id() { std::string reply_str = net_version(); return retrieve_value_from_reply(reply_str, ""); @@ -54,11 +62,35 @@ std::string ethereum_rpc_client::get_network_id() { return retrieve_value_from_reply(reply_str, "protocols.eth.network"); } -std::string ethereum_rpc_client::send_transaction(const std::string& params) { +std::string ethereum_rpc_client::get_nonce(const std::string& address) { + std::string reply_str = eth_get_transaction_count("[\"" + address + "\", \"latest\"]"); + return retrieve_value_from_reply(reply_str, ""); +} + +std::string ethereum_rpc_client::get_gas_price() { + std::string reply_str = eth_gas_price(); + return retrieve_value_from_reply(reply_str, ""); +} + +std::string ethereum_rpc_client::get_gas_limit() { + std::string reply_str = eth_get_block_by_number("latest", false); + if (!reply_str.empty()) { + std::stringstream ss(reply_str); + boost::property_tree::ptree json; + boost::property_tree::read_json(ss, json); + if (json.count("result")) { + std::string gas_limit_s = json.get("result.gasLimit"); + return gas_limit_s; + } + } + return std::string{}; +} + +std::string ethereum_rpc_client::eth_send_transaction(const std::string& params) { return send_post_request("eth_sendTransaction", "[" + params + "]", debug_rpc_calls); } -std::string ethereum_rpc_client::get_transaction_receipt(const std::string& params) { +std::string ethereum_rpc_client::eth_get_transaction_receipt(const std::string& params) { return send_post_request("eth_getTransactionReceipt", "[\"" + params + "\"]", debug_rpc_calls); } @@ -504,7 +536,7 @@ std::string sidechain_net_handler_ethereum::send_sidechain_transaction(const sid boost::property_tree::ptree pt_array; for(const auto& signature : sto.signatures) { const auto& transaction = signature.second; - const std::string sidechain_transaction = rpc_client->send_transaction(transaction); + const std::string sidechain_transaction = rpc_client->eth_send_transaction(transaction); std::stringstream ss_tx(sidechain_transaction); boost::property_tree::ptree tx_json; @@ -517,8 +549,8 @@ std::string sidechain_net_handler_ethereum::send_sidechain_transaction(const sid } else { //! Fixme - //! How should we proceed with error in send_transaction - elog("Error in send_transaction for transaction ${id}, transaction ${transaction}", ("id", sto.id) ("transaction", transaction)); + //! How should we proceed with error in eth_send_transaction + elog("Error in eth_send_transaction for transaction ${id}, transaction ${transaction}", ("id", sto.id) ("transaction", transaction)); } } pt.add_child("result_array", pt_array); @@ -539,7 +571,7 @@ bool sidechain_net_handler_ethereum::settle_sidechain_transaction(const sidechai size_t count = 0; for(const auto &entry : json.get_child("result_array")) { - const std::string receipt = rpc_client->get_transaction_receipt( entry.second.get("transaction_receipt") ); + const std::string receipt = rpc_client->eth_get_transaction_receipt( entry.second.get("transaction_receipt") ); std::stringstream ss_receipt(receipt); boost::property_tree::ptree json_receipt;