parse output of rpc commands

This commit is contained in:
Pavel Baykov 2022-05-26 09:17:26 -03:00
parent 367a203007
commit cff3bec507
2 changed files with 70 additions and 5 deletions

View file

@ -35,6 +35,20 @@ class eth_rpc_client {
public:
typedef eth_rpc_client type;
enum req_t {
ETH_CHAIN_ID,
ETH_GET_TRANSACTION_RECEIPT,
ETH_CALL,
ETH_SEND_TRANSACTION,
ETH_SEND_RAW_TRANSACTION,
ETH_GET_CODE,
ETH_GET_BALANCE,
ETH_SIGN,
GET_LIST_OWNERS,
ADD_OWNER,
REMOVE_OWNER
};
enum class multi_type {
script,
address
@ -107,10 +121,12 @@ public:
private:
std::string geth_url;
uint64_t t_id;
std::string transaction_id;
std::string safe_account_addr;
std::vector<std::string> owners;
uint32_t threshold;
std::unordered_map<uint64_t, std::string> m_messages;
std::unordered_map<uint64_t, req_t> m_requests;
std::string chain_id;//256 bit value
std::string ip;
uint32_t rpc_port;

View file

@ -94,6 +94,46 @@ void eth_rpc_client::on_message(websocketpp::connection_hdl hdl, message_ptr msg
fc::variants list = fc::json::variants_from_string( msg->get_payload() );
ilog("json reposnse: ${list}", ("list", list));
const auto& b_obj = list[0].get_object().find( "id" );
std::string result_str = list[0].get_object().find( "result" )->value().as<std::string>(1);
uint32_t num_owners = 0;
uint32_t i = 0;
fc::variant v;
switch(b_obj->value().as<uint32_t>(1)){
case ETH_CHAIN_ID:
chain_id = result_str;
break;
case ETH_GET_TRANSACTION_RECEIPT:
list = fc::json::variants_from_string( result_str );
v = list[0].get_object().find( "logs" )->value();
safe_account_addr = v.get_object().find( "address" )->value().as<std::string>(1);
break;
case ETH_CALL:
break;
case ETH_SEND_TRANSACTION:
transaction_id = result_str;
break;
case ETH_SEND_RAW_TRANSACTION:
break;
case ETH_GET_CODE:
break;
case ETH_GET_BALANCE:
break;
case ETH_SIGN:
break;
case GET_LIST_OWNERS:
num_owners = (uint32_t)strtol(result_str.substr(2 + 32 + 32 - 4, 4).c_str(), NULL, 16);
owners.clear();
for (i = 0; i < num_owners; ++i){
owners.push_back("0x" + result_str.substr(2 + 32 + 32 + 12 + 32 * i,20));
}
break;
case ADD_OWNER:
break;
case REMOVE_OWNER:
break;
}
}
void eth_rpc_client::on_close(websocketpp::connection_hdl) {
@ -103,26 +143,29 @@ void eth_rpc_client::on_close(websocketpp::connection_hdl) {
uint64_t eth_rpc_client::get_chain_id() {
std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\",\"params\":[],\"id\":%1%}") % t_id);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
m_requests[t_id] = req_t::ETH_CHAIN_ID;
return t_id++;
}
uint64_t eth_rpc_client::eth_getTransactionReceipt(const std::string& tx_id) {
std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionReceipt\",\"params\":[\"%1%\"],\"id\":%2%}") % tx_id.c_str() % t_id);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
m_requests[t_id] = req_t::ETH_GET_TRANSACTION_RECEIPT;
return t_id++;
}
uint64_t eth_rpc_client::eth_call(const std::string& to, const std::string& data) {
std::string req = str(boost::format("{\"jsonrpc\": \"2.0\", \"method\": \"eth_call\", \"params\": [{\"to\": \"%1%\", \"data\": \"%2%\"}, \"latest\"], \"id\": %3%}") % to.c_str() % data.c_str() % t_id);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
m_requests[t_id] = req_t::ETH_CALL;
return t_id++;
}
uint64_t eth_rpc_client::eth_sendTransaction(const std::string& from, const std::string& to, const std::string& data) {
std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\": \"eth_sendTransaction\", \"params\": [{\"from\": \"%1%\", \"to\": \"%2%\", \"data\": \"%3%\"}], \"id\": %4%}") % from.c_str() % to.c_str() % data.c_str() % t_id);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
m_requests[t_id] = req_t::ETH_SEND_TRANSACTION;
return t_id++;
}
@ -130,26 +173,29 @@ uint64_t eth_rpc_client::eth_sendTransaction(const std::string& from, const std:
uint64_t eth_rpc_client::eth_sendRawTransaction(const std::string& params) {
std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\":\"eth_sendRawTransaction\",\"params\":[\"%1%\"],\"id\":%2%}") % params.c_str() % t_id);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
m_requests[t_id] = req_t::ETH_SEND_RAW_TRANSACTION;
return t_id++;
}
uint64_t eth_rpc_client::eth_getCode(const std::string& addr) {
std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\": \"eth_getCode\", \"params\": [\"%1%\",\"latest\"], \"id\": %2%}") % addr.c_str() % t_id);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
m_requests[t_id] = req_t::ETH_GET_CODE;
return t_id++;
}
uint64_t eth_rpc_client::eth_getBalance(const std::string& addr) {
std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\": \"eth_getBalance\", \"params\": [\"%1%\",\"latest\"], \"id\": %2%}") % addr.c_str() % t_id);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
m_requests[t_id] = req_t::ETH_GET_BALANCE;
return t_id++;
}
uint64_t eth_rpc_client::eth_sign(const string& addr, const string& message) {
std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\": \"eth_sign\", \"params\": [\"%1%\",\"%2%\"], \"id\": %2%}") % addr.c_str() % message.c_str() % t_id);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
m_requests[t_id] = req_t::ETH_SIGN;
return t_id++;
}
@ -176,6 +222,8 @@ uint64_t eth_rpc_client::add_owner(const std::string& addr ) {
std::string gasPrice = str(boost::format("%032u") % 0);
std::string gasToken = "0000000000000000000000000000000000000000";
std::string refundReceiver = "0000000000000000000000000000000000000000";
m_requests[t_id] = req_t::ADD_OWNER;
}
uint64_t eth_rpc_client::remove_owner(const std::string& addr, uint32_t threshold) {
@ -201,6 +249,7 @@ uint64_t eth_rpc_client::remove_owner(const std::string& addr, uint32_t threshol
std::string gasToken = "0000000000000000000000000000000000000000";
std::string refundReceiver = "0000000000000000000000000000000000000000";
m_requests[t_id] = req_t::REMOVE_OWNER;
}
std::string eth_rpc_client::addmultisigaddress(const uint32_t nrequired, const std::vector<std::string> public_keys) {