ledger-core
TezosLikeBlockchainObserver.h
1 /*
2  *
3  * TezosLikeBlockchainObserver
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_TEZOSLIKEBLOCKCHAINOBSERVER_H
33 #define LEDGER_CORE_TEZOSLIKEBLOCKCHAINOBSERVER_H
34 
35 #include <string>
36 #include <list>
37 
38 #include <wallet/tezos/explorers/TezosLikeBlockchainExplorer.h>
39 
40 #include <utils/ConfigurationMatchable.h>
41 #include <async/DedicatedContext.hpp>
42 
43 #include <api/ExecutionContext.hpp>
44 #include <api/Currency.hpp>
45 #include <api/DynamicObject.hpp>
46 
47 #include <net/WebSocketClient.h>
48 #include <net/WebSocketConnection.h>
49 
50 #include <spdlog/logger.h>
51 #include <wallet/common/observers/AbstractBlockchainObserver.h>
52 #include <wallet/common/observers/AbstractLedgerApiBlockchainObserver.h>
53 
54 namespace ledger {
55  namespace core {
56  class TezosLikeAccount;
57 
58  using TezosBlockchainObserver = AbstractBlockchainObserver<TezosLikeAccount, TezosLikeBlockchainExplorerTransaction, TezosLikeBlockchainExplorer::Block>;
59 
62  public DedicatedContext,
64  public std::enable_shared_from_this<TezosLikeBlockchainObserver> {
65  public:
66  TezosLikeBlockchainObserver(const std::shared_ptr<api::ExecutionContext> &context,
67  const std::shared_ptr<api::DynamicObject> &configuration,
68  const std::shared_ptr<spdlog::logger> &logger,
69  const api::Currency &currency,
70  const std::vector<std::string> &matchableKeys);
71 
72  TezosLikeBlockchainObserver(const std::shared_ptr<api::ExecutionContext> &context,
73  const std::shared_ptr<WebSocketClient> &client,
74  const std::shared_ptr<api::DynamicObject> &configuration,
75  const std::shared_ptr<spdlog::logger> &logger,
76  const api::Currency &currency);
77 
78 
79  protected:
80  void putTransaction(const TezosLikeBlockchainExplorerTransaction &tx) override;
81 
82  void putBlock(const TezosLikeBlockchainExplorer::Block &block) override;
83 
84  const api::Currency &getCurrency() const {
85  return _currency;
86  };
87 
88  std::shared_ptr<api::DynamicObject> getConfiguration() const {
89  return _configuration;
90  };
91 
92  private:
93  api::Currency _currency;
94  std::shared_ptr<api::DynamicObject> _configuration;
95 
96 
97  protected:
98  void onStart() override;
99 
100  void onStop() override;
101 
102  private:
103  void connect() override;
104 
105  void reconnect() override;
106 
107  void onMessage(const std::string &message) override;
108 
109  private:
110  std::shared_ptr<spdlog::logger> logger() const override;
111 
112  std::shared_ptr<WebSocketClient> _client;
113  WebSocketEventHandler _handler;
114  };
115  }
116 }
117 #endif //LEDGER_CORE_TEZOSLIKEBLOCKCHAINOBSERVER_H
Definition: AbstractLedgerApiBlockchainObserver.h:40
Definition: AbstractBlockchainObserver.h:45
Definition: TezosLikeBlockchainExplorer.h:68
Definition: ConfigurationMatchable.h:38
Definition: Currency.hpp:23
Definition: Account.cpp:8
Definition: TezosLikeBlockchainObserver.h:60
Definition: DedicatedContext.hpp:39
Definition: Block.h:40
Definition: logger.hpp:44