32 #ifndef LEDGER_CORE_ABSTRACTLEDGERAPIBLOCKCHAINEXPLORER_H 33 #define LEDGER_CORE_ABSTRACTLEDGERAPIBLOCKCHAINEXPLORER_H 35 #include <fmt/format.h> 36 #include <rapidjson/stringbuffer.h> 37 #include <rapidjson/writer.h> 38 #include <api/Configuration.hpp> 39 #include <utils/hex.h> 40 #include <net/HttpClient.hpp> 41 #include <wallet/common/explorers/LedgerApiParser.hpp> 42 #include <wallet/common/Block.h> 43 #include <utils/JSONUtils.h> 50 template <
typename BlockchainExplorerTransaction,
typename TransactionsBulk,
typename TransactionsParser,
typename TransactionsBulkParser,
typename BlockParser,
typename NetworkParameters>
54 getLedgerApiTransactions(
const std::vector<std::string> &addresses,
57 bool isSnakeCase =
false) {
58 auto joinedAddresses =
Array<std::string>(addresses).join(strings::mkString(
",")).getValueOr(
"");
60 std::unordered_map<std::string, std::string> headers;
62 if (session.isEmpty()) {
63 params = isSnakeCase ?
"?no_token=true" :
"?noToken=true";
65 headers[
"X-LedgerWallet-SyncToken"] = *((std::string *)session.getValue());
67 if (!fromBlockHash.isEmpty()) {
68 if (params.size() > 0) {
69 params = params +
"&";
71 params = params +
"?";
73 auto blockHash = isSnakeCase ?
"block_hash=" :
"blockHash=";
74 params = params + blockHash + fromBlockHash.getValue();
76 return _http->GET(fmt::format(
"/blockchain/{}/{}/addresses/{}/transactions{}", getExplorerVersion(), getNetworkParameters().Identifier, joinedAddresses, params), headers)
79 if (result.isLeft()) {
80 if (fromBlockHash.isEmpty()) {
81 throw result.getLeft();
86 return result.getRight();
92 getLedgerApiCurrentBlock()
const {
93 return _http->GET(fmt::format(
"/blockchain/{}/{}/blocks/current", getExplorerVersion(), getNetworkParameters().Identifier))
96 if (result.isLeft()) {
97 throw result.getLeft();
99 return result.getRight();
105 getLedgerApiTransactionByHash(
const String &transactionHash)
const {
106 return _http->GET(fmt::format(
"/blockchain/{}/{}/transactions/{}", getExplorerVersion(), getNetworkParameters().Identifier, transactionHash.str()))
109 if (result.isLeft()) {
110 throw result.getLeft();
111 }
else if (result.getRight()->size() == 0) {
112 throw make_exception(api::ErrorCode::TRANSACTION_NOT_FOUND,
"Transaction '{}' not found", transactionHash.str());
114 auto tx = (*result.getRight())[0];
115 auto transaction = std::make_shared<BlockchainExplorerTransaction>(tx);
122 return _http->GET(fmt::format(
"/blockchain/{}/{}/syncToken", getExplorerVersion(), getNetworkParameters().Identifier))
123 .json().template map<void *>(getExplorerContext(), [] (
const HttpRequest::JsonResult& result) {
124 auto&
json = *std::get<1>(result);
125 return new std::string(
json[
"token"].GetString(),
json[
"token"].GetStringLength());
129 Future<Unit> killLedgerApiSession(
void *session)
const {
130 return _http->addHeader(
"X-LedgerWallet-SyncToken", *((std::string *)session))
131 .DEL(fmt::format(
"/blockchain/{}/{}/syncToken", getExplorerVersion(), getNetworkParameters().Identifier))
132 .json().template map<Unit>(getExplorerContext(), [] (
const HttpRequest::JsonResult& result) {
139 return _http->GET(fmt::format(
"/blockchain/{}/{}/transactions/{}/hex", getExplorerVersion(), getNetworkParameters().Identifier, transactionHash.str()))
140 .json().template map<Bytes>(getExplorerContext(), [transactionHash] (
const HttpRequest::JsonResult& result) {
141 auto&
json = *std::get<1>(result);
142 if (
json.GetArray().Size() == 0) {
143 throw make_exception(api::ErrorCode::RAW_TRANSACTION_NOT_FOUND,
"Unable to retrieve {}", transactionHash.str());
145 auto& hex =
json[0].GetObject()[
"hex"];
152 auto delay = 60*getNetworkParameters().TimestampDelay;
153 return _http->GET(fmt::format(
"/timestamp"))
154 .json().template map<int64_t>(getExplorerContext(), [delay] (
const HttpRequest::JsonResult& result) {
155 auto&
json = *std::get<1>(result);
156 return json[
"timestamp"].GetInt64() - delay;
160 virtual Future<String> pushLedgerApiTransaction(
const std::vector<uint8_t> &transaction) = 0 ;
162 virtual std::shared_ptr<api::ExecutionContext> getExplorerContext()
const = 0;
163 virtual NetworkParameters getNetworkParameters()
const = 0;
164 virtual std::string getExplorerVersion()
const = 0;
165 std::shared_ptr<HttpClient> _http;
171 #endif //LEDGER_CORE_ABSTRACTLEDGERAPIBLOCKCHAINEXPLORER_H Definition: Either.hpp:43
a class to store JSON values
Definition: json.hpp:231
Definition: TransactionsParser.hpp:40
Definition: Deffered.hpp:49
Definition: AbstractLedgerApiBlockchainExplorer.h:51
Definition: Account.cpp:8
std::vector< uint8_t > toByteArray(const std::string &str)
Definition: hex.cpp:39
Definition: LedgerApiParser.hpp:42
Definition: Sequence.hpp:42
Definition: Exception.hpp:45