api to get raw tx and block data added

master
moneroexamples 8 years ago
parent bb213e3692
commit 67b48ccc93

@ -363,6 +363,14 @@ int main(int ac, const char* av[]) {
return r; return r;
}); });
CROW_ROUTE(app, "/api/rawtransaction/<string>")
([&](const crow::request &req, string tx_hash) {
myxmr::jsonresponse r{xmrblocks.json_rawtransaction(tx_hash)};
return r;
});
CROW_ROUTE(app, "/api/block/<string>") CROW_ROUTE(app, "/api/block/<string>")
([&](const crow::request &req, string block_no_or_hash) { ([&](const crow::request &req, string block_no_or_hash) {
@ -371,6 +379,13 @@ int main(int ac, const char* av[]) {
return r; return r;
}); });
CROW_ROUTE(app, "/api/rawblock/<string>")
([&](const crow::request &req, string block_no_or_hash) {
myxmr::jsonresponse r{xmrblocks.json_rawblock(block_no_or_hash)};
return r;
});
CROW_ROUTE(app, "/api/transactions").methods("GET"_method) CROW_ROUTE(app, "/api/transactions").methods("GET"_method)
([&](const crow::request &req) { ([&](const crow::request &req) {

@ -3834,6 +3834,91 @@ namespace xmreg
return j_response; return j_response;
} }
/*
* Lets use this json api convention for success and error
* https://labs.omniti.com/labs/jsend
*/
json
json_rawtransaction(string tx_hash_str)
{
json j_response {
{"status", "fail"},
{"data" , json {}}
};
json& j_data = j_response["data"];
// parse tx hash string to hash object
crypto::hash tx_hash;
if (!xmreg::parse_str_secret_key(tx_hash_str, tx_hash))
{
j_data["title"] = fmt::format("Cant parse tx hash: {:s}", tx_hash_str);
return j_response;
}
// get transaction
transaction tx;
// flag to indicate if tx is in mempool
bool found_in_mempool {false};
// for tx in blocks we get block timestamp
// for tx in mempool we get recievive time
uint64_t tx_timestamp {0};
if (!find_tx(tx_hash, tx, found_in_mempool, tx_timestamp))
{
j_data["title"] = fmt::format("Cant find tx hash: {:s}", tx_hash_str);
return j_response;
}
if (found_in_mempool == false)
{
block blk;
try
{
// get block cointaining this tx
uint64_t block_height = core_storage->get_db().get_tx_block_height(tx_hash);
if (!mcore->get_block_by_height(block_height, blk))
{
j_data["title"] = fmt::format("Cant get block: {:d}", block_height);
return j_response;
}
}
catch (const exception& e)
{
j_response["status"] = "error";
j_response["message"] = fmt::format("Tx does not exist in blockchain, "
"but was there before: {:s}",
tx_hash_str);
return j_response;
}
}
// get raw tx json as in monero
try
{
j_data = json::parse(obj_to_json_str(tx));
}
catch (std::invalid_argument& e)
{
j_response["status"] = "error";
j_response["message"] = "Faild parsing raw tx data into json";
return j_response;
}
j_response["status"] = "success";
return j_response;
}
/* /*
* Lets use this json api convention for success and error * Lets use this json api convention for success and error
* https://labs.omniti.com/labs/jsend * https://labs.omniti.com/labs/jsend
@ -3977,6 +4062,102 @@ namespace xmreg
/*
* Lets use this json api convention for success and error
* https://labs.omniti.com/labs/jsend
*/
json
json_rawblock(string block_no_or_hash)
{
json j_response {
{"status", "fail"},
{"data" , json {}}
};
json& j_data = j_response["data"];
uint64_t current_blockchain_height
= core_storage->get_current_blockchain_height();
uint64_t block_height {0};
crypto::hash blk_hash;
block blk;
if (block_no_or_hash.length() <= 8)
{
// we have something that seems to be a block number
try
{
block_height = boost::lexical_cast<uint64_t>(block_no_or_hash);
}
catch (const boost::bad_lexical_cast& e)
{
j_data["title"] = fmt::format(
"Cant parse block number: {:s}", block_no_or_hash);
return j_response;
}
if (block_height > current_blockchain_height)
{
j_data["title"] = fmt::format(
"Requested block is higher than blockchain:"
" {:d}, {:d}", block_height,current_blockchain_height);
return j_response;
}
if (!mcore->get_block_by_height(block_height, blk))
{
j_data["title"] = fmt::format("Cant get block: {:d}", block_height);
return j_response;
}
blk_hash = core_storage->get_block_id_by_height(block_height);
}
else if (block_no_or_hash.length() == 64)
{
// this seems to be block hash
if (!xmreg::parse_str_secret_key(block_no_or_hash, blk_hash))
{
j_data["title"] = fmt::format("Cant parse blk hash: {:s}", block_no_or_hash);
return j_response;
}
if (!core_storage->get_block_by_hash(blk_hash, blk))
{
j_data["title"] = fmt::format("Cant get block: {:s}", blk_hash);
return j_response;
}
block_height = core_storage->get_db().get_block_height(blk_hash);
}
else
{
j_data["title"] = fmt::format("Cant find blk using search string: {:s}", block_no_or_hash);
return j_response;
}
// get raw tx json as in monero
try
{
j_data = json::parse(obj_to_json_str(blk));
}
catch (std::invalid_argument& e)
{
j_response["status"] = "error";
j_response["message"] = "Faild parsing raw blk data into json";
return j_response;
}
j_response["status"] = "success";
return j_response;
}
/* /*
* Lets use this json api convention for success and error * Lets use this json api convention for success and error
* https://labs.omniti.com/labs/jsend * https://labs.omniti.com/labs/jsend

Loading…
Cancel
Save