ledger-core
TezosLikeWebSocketNotificationParser.h
1 /*
2  *
3  * TezosLikeWebSocketNotificationParser
4  *
5  * Created by El Khalil Bellakrid on 27/04/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_TEZOSLIKEWEBSOCKETNOTIFICATIONPARSER_H
33 #define LEDGER_CORE_TEZOSLIKEWEBSOCKETNOTIFICATIONPARSER_H
34 
35 #include <cstdio>
36 #include <cstdint>
37 #include <rapidjson/reader.h>
38 #include <stack>
39 #include <net/HttpClient.hpp>
40 #include <collections/collections.hpp>
41 #include "../TezosLikeBlockchainExplorer.h"
42 #include "TezosLikeBlockParser.h"
43 #include "TezosLikeTransactionParser.h"
44 #include <wallet/common/explorers/api/AbstractWebSocketNotificationParser.h>
45 
46 
47 #define PROXY_PARSE_TEZOS_WS(method, ...) \
48  auto& currentObject = _currentObject; \
49  if (currentObject == "transaction") { \
50  return getTransactionParser().method(__VA_ARGS__); \
51  } else \
52 
53 namespace ledger {
54  namespace core {
56  : public AbstractWebSocketNotificationParser<TezosLikeBlockchainExplorerTransaction, TezosLikeBlockchainExplorer::Block, TezosLikeTransactionParser, TezosLikeBlockParser> {
57  public:
58 
59 
60  explicit TezosLikeWebSocketNotificationParser(std::string &lastKey) : _lastKey(lastKey),
61  _blockParser(lastKey),
62  _transactionParser(lastKey) {
63 
64  }
65 
66  bool Key(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) override {
67  _lastKey = std::string(str, length);
71  TezosLikeBlockParser>::Key(str, length, copy);
72  }
73 
74  bool StartObject() {
75  {
76  _depth += 1;
77  auto lastKey = getLastKey();
78  if (lastKey == "transaction") {
79  _currentObject = lastKey;
80  getTransactionParser().init(&_result->transaction);
81  }
82  }
83  PROXY_PARSE_TEZOS_WS(StartObject) {
84  return true;
85  }
86  };
87 
88  bool RawNumber(const rapidjson::Reader::Ch *str, rapidjson::SizeType length,
89  bool copy) {
90  PROXY_PARSE_TEZOS_WS(String, str, length, copy) {
91  auto value = std::string(str, length);
92  if (getLastKey() == "block_height") {
93  BigInt bigIntValue = BigInt::fromString(value);
94  _result->block.height = bigIntValue.toUint64();
95  }
96  }
97  return true;
98  };
99 
100  bool
101  String(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) {
102 
103  auto value = std::string(str, length);
104  if (getLastKey() == "type") {
105  _result->type = value;
106  }
107 
108  PROXY_PARSE_TEZOS_WS(String, str, length, copy) {
109  if (getLastKey() == "block_hash") {
110  _result->block.hash = value;
111  }
112  return true;
113  }
114  };
115 
116  bool EndObject(rapidjson::SizeType memberCount) {
117  _depth -= 1;
118  if (_depth == 1)
119  _currentObject = "";
120  PROXY_PARSE_WS(EndObject, memberCount) {
121  return true;
122  }
123  };
124 
125  protected:
126 
127  TezosLikeTransactionParser &getTransactionParser() override {
128  return _transactionParser;
129  };
130 
131  TezosLikeBlockParser &getBlockParser() override {
132  return _blockParser;
133  };
134 
135  std::string &getLastKey() override {
136  return _lastKey;
137  };
138 
139  private:
140  std::string &_lastKey;
141  TezosLikeBlockParser _blockParser;
142  TezosLikeTransactionParser _transactionParser;
143  };
144  }
145 }
146 
147 #endif //LEDGER_CORE_TEZOSLIKEWEBSOCKETNOTIFICATIONPARSER_H
Definition: TezosLikeBlockchainExplorer.h:68
Definition: TezosLikeBlockParser.h:40
Definition: TezosLikeTransactionParser.h:48
Definition: Account.cpp:8
Definition: AbstractWebSocketNotificationParser.h:46
Definition: BigInt.h:56
Definition: Block.h:40
Definition: TezosLikeWebSocketNotificationParser.h:55