ledger-core
NodeRippleLikeBlockchainExplorer.h
1 /*
2  *
3  * NodeRippleLikeBlockchainExplorer
4  *
5  * Created by El Khalil Bellakrid on 09/01/2019.
6  *
7  * The MIT License (MIT)
8  *
9  * Copyright (c) 2019 Ledger
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in all
19  * copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  */
30 
31 
32 #ifndef LEDGER_CORE_NODERIPPLELIKEBLOCKCHAINEXPLORER_H
33 #define LEDGER_CORE_NODERIPPLELIKEBLOCKCHAINEXPLORER_H
34 
35 
36 #include <wallet/common/explorers/AbstractLedgerApiBlockchainExplorer.h>
37 #include <wallet/ripple/explorers/RippleLikeBlockchainExplorer.h>
38 #include <wallet/ripple/explorers/api/RippleLikeTransactionsParser.h>
39 #include <wallet/ripple/explorers/api/RippleLikeTransactionsBulkParser.h>
40 #include <wallet/ripple/explorers/api/RippleLikeBlockParser.h>
41 #include <api/RippleLikeNetworkParameters.hpp>
42 
43 namespace ledger {
44  namespace core {
45  using LedgerApiBlockchainExplorer = AbstractLedgerApiBlockchainExplorer<RippleLikeBlockchainExplorerTransaction, RippleLikeBlockchainExplorer::TransactionsBulk, RippleLikeTransactionsParser, RippleLikeTransactionsBulkParser, RippleLikeBlockParser, api::RippleLikeNetworkParameters>;
46 
47  // Fields we are interested into are numbers or strings
48  enum FieldTypes {
49  NumberType,
50  StringType
51  };
52 
54 
55  public:
57  //Document should be defined as object
58  _document.SetObject();
59  _params = rapidjson::Value(rapidjson::kObjectType);
60  };
61 
62  NodeRippleLikeBodyRequest &setMethod(const std::string &method) {
63  //In case need to allocate more memory
64  rapidjson::Document::AllocatorType &allocator = _document.GetAllocator();
65  //Field with method
66  rapidjson::Value vMethod(rapidjson::kStringType);
67  vMethod.SetString(method.c_str(), static_cast<rapidjson::SizeType>(method.length()), allocator);
68  _document.AddMember("method", vMethod, allocator);
69  return *this;
70  };
71 
72  NodeRippleLikeBodyRequest &pushParameter(const std::string &key, const std::string &value) {
73  rapidjson::Document::AllocatorType &allocator = _document.GetAllocator();
74  rapidjson::Value vKeyParam(rapidjson::kStringType);
75  vKeyParam.SetString(key.c_str(), static_cast<rapidjson::SizeType>(key.length()), allocator);
76  rapidjson::Value vParam(rapidjson::kStringType);
77  vParam.SetString(value.c_str(), static_cast<rapidjson::SizeType>(value.length()), allocator);
78  _params.AddMember(vKeyParam, vParam, allocator);
79  return *this;
80  };
81 
82  NodeRippleLikeBodyRequest &pushParameter(const std::string &key, int64_t value) {
83  rapidjson::Document::AllocatorType &allocator = _document.GetAllocator();
84  rapidjson::Value vKeyParam(rapidjson::kStringType);
85  vKeyParam.SetString(key.c_str(), static_cast<rapidjson::SizeType>(key.length()), allocator);
86  rapidjson::Value vParam(rapidjson::kNumberType);
87  vParam.SetInt64(value);
88  _params.AddMember(vKeyParam, vParam, allocator);
89  return *this;
90  };
91 
92  std::string getString() {
93  rapidjson::Document::AllocatorType &allocator = _document.GetAllocator();
94  rapidjson::Value container(rapidjson::kArrayType);
95  container.PushBack(_params, allocator);
96  _document.AddMember("params", container, allocator);
97  //Stream to string buffer
98  rapidjson::StringBuffer buffer;
99  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
100  _document.Accept(writer);
101  return buffer.GetString();
102  };
103 
104  private:
105  rapidjson::Document _document;
106  rapidjson::Value _params;
107  };
108 
111  public DedicatedContext,
112  public std::enable_shared_from_this<NodeRippleLikeBlockchainExplorer> {
113  public:
114  NodeRippleLikeBlockchainExplorer(const std::shared_ptr<api::ExecutionContext> &context,
115  const std::shared_ptr<HttpClient> &http,
116  const api::RippleLikeNetworkParameters &parameters,
117  const std::shared_ptr<api::DynamicObject> &configuration);
118 
120  getBalance(const std::vector<RippleLikeKeychain::Address> &addresses) override;
121 
123  getSequence(const std::string &address) override;
124 
126  getFees() override;
127 
129  getBaseReserve() override;
130 
132  getLedgerSequence() override;
133 
135  getServerState(const std::string &field);
136 
137  Future<String> pushLedgerApiTransaction(const std::vector<uint8_t> &transaction) override;
138 
139  Future<void *> startSession() override;
140 
141  Future<Unit> killSession(void *session) override;
142 
143  Future<Bytes> getRawTransaction(const String &transactionHash) override;
144 
145  Future<String> pushTransaction(const std::vector<uint8_t> &transaction) override;
146 
148  getTransactions(const std::vector<std::string> &addresses,
149  Option<std::string> fromBlockHash = Option<std::string>(),
150  Option<void *> session = Option<void *>()) override;
151 
152  FuturePtr<Block> getCurrentBlock() const override;
153 
155  getTransactionByHash(const String &transactionHash) const override;
156 
157  Future<int64_t> getTimestamp() const override;
158 
159  std::shared_ptr<api::ExecutionContext> getExplorerContext() const override;
160 
161  api::RippleLikeNetworkParameters getNetworkParameters() const override;
162 
163  std::string getExplorerVersion() const override;
164 
165  private:
167  getAccountInfo(const std::string &address,
168  const std::string &key,
169  const BigInt &defaultValue);
170 
172  };
173  }
174 }
175 
176 
177 #endif //LEDGER_CORE_NODERIPPLELIKEBLOCKCHAINEXPLORER_H
Definition: RippleLikeNetworkParameters.hpp:15
Definition: NodeRippleLikeBlockchainExplorer.h:109
Definition: Deffered.hpp:49
Definition: AbstractLedgerApiBlockchainExplorer.h:51
Definition: Account.cpp:8
Definition: DedicatedContext.hpp:39
Definition: RippleLikeBlockchainExplorer.h:89
Definition: NodeRippleLikeBlockchainExplorer.h:53
Definition: BigInt.h:56