From ebe4bb77651311fa1061163a24a3c7eb1d79a118 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Sun, 17 Jun 2018 08:39:29 +0800 Subject: [PATCH] started work on detailed info --- main.cpp | 8 ++++++ src/page.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/main.cpp b/main.cpp index d7a169b..b240db5 100644 --- a/main.cpp +++ b/main.cpp @@ -603,6 +603,14 @@ main(int ac, const char* av[]) return r; }); + CROW_ROUTE(app, "/api/detailedtransaction/") + ([&](const crow::request &req, string tx_hash) { + + myxmr::jsonresponse r{xmrblocks.json_detailedtransaction(remove_bad_chars(tx_hash))}; + + return r; + }); + CROW_ROUTE(app, "/api/block/") ([&](const crow::request &req, string block_no_or_hash) { diff --git a/src/page.h b/src/page.h index 787c734..7e846ee 100644 --- a/src/page.h +++ b/src/page.h @@ -30,6 +30,7 @@ #include #include #include +#include #define TMPL_DIR "./templates" @@ -3971,6 +3972,8 @@ public: } + + /* * Lets use this json api convention for success and error * https://labs.omniti.com/labs/jsend @@ -4220,6 +4223,55 @@ public: return j_response; } + + json + json_detailedtransaction(string tx_hash_str) + { + json j_response { + {"status", "fail"}, + {"data" , json {}} + }; + + json& j_data = j_response["data"]; + + transaction tx; + + bool found_in_mempool {false}; + uint64_t tx_timestamp {0}; + string error_message; + + if (!find_tx_for_json(tx_hash_str, tx, found_in_mempool, tx_timestamp, error_message)) + { + j_data["title"] = error_message; + return j_response; + } + + mstch::map tx_context = construct_tx_context(tx, 1 /*full detailed */); + + std::string view{"{{#bold}}{{yay}} :){{/bold}}"}; + + + + + for (auto const& kv: tx_context) + { + //j_data[kv.first] = boost::apply_visitor(render_node2(), kv.second); + + mstch::map context{ + {"yay", kv.second}, + {"bold", mstch::lambda{[](const std::string& text) -> mstch::node { + return "" + text + ""; + }}} + }; + + cout << mstch::render(view, context) << endl; + } + + j_response["status"] = "success"; + + return j_response; + } + /* * Lets use this json api convention for success and error * https://labs.omniti.com/labs/jsend @@ -6146,6 +6198,32 @@ private: } + bool + find_tx_for_json( + string const& tx_hash_str, + transaction& tx, + bool& found_in_mempool, + uint64_t& tx_timestamp, + string& error_message) + { + // parse tx hash string to hash object + crypto::hash tx_hash; + + if (!xmreg::parse_str_secret_key(tx_hash_str, tx_hash)) + { + error_message = fmt::format("Cant parse tx hash: {:s}", tx_hash_str); + return false; + } + + if (!find_tx(tx_hash, tx, found_in_mempool, tx_timestamp)) + { + error_message = fmt::format("Cant find tx hash: {:s}", tx_hash_str); + return false; + } + + return true; + } + bool search_mempool(crypto::hash tx_hash, vector& found_txs)